Springside4—CRUD
转载 https://github.com/springside/springside4/wiki/Tutorial
CRUD页面教程
1. 数据库设计
在src/resouce/sql/schema 中手工编写创建表的sqlcreate table acct_user ( id bigint generated by default as identity, email varchar(255), login_name varchar(255) not null unique, name varchar(255), password varchar(255), primary key (id) ) ;
2. Entity
2.1 手工编写Entity,利用默认大于配置原理,写尽量少的注释, 一般只有带前缀的表名,Cache和关联属性需要注释。@Entity @Table(name = "acct_user") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class User extends IdEntity { private String loginName; private String password; private String name; private String email; //... getter and setter }
3. DAO
3.1 写一个简单的DAO接口,日后再根据需要添加方法定义。public interface UserDao extends PagingAndSortingRepository<User, Long> { User findByLoginName(String loginName); }
4.Service
4.1 按业务划分新建或使用已有Service(与DAO不应是一一对应关系),根据需求添加方法定义。 注意事务定义。@Component @Transactional(readOnly = true) public class AccountManager { private UserDao userDao; public User getUser(Long id) { return userDao.findOne(id); } @Transactional(readOnly = false) public void saveUser(User entity) { userDao.save(entity); } }
Web Controller
4.1 写一个Controller负责List, Create, Delete。@Controller @RequestMapping(value = "/account/user") public class UserController { private AccountManager accountManager; @RequestMapping(value = { "list", "" }) public String list(Model model) { List<User> users = accountManager.getAllUser(); model.addAttribute("users", users); return "account/userList"; } @RequestMapping(value = "create") public String createForm(Model model) { model.addAttribute("user", new User()); return "account/userForm"; } @RequestMapping(value = "save") public String save(User user, RedirectAttributes redirectAttributes) { accountManager.saveUser(user); redirectAttributes.addFlashAttribute("message", "Create user " + user.getLoginName() + " success"); return "redirect:/account/user/"; } }
@Controller @RequestMapping(value = "/account/user/") public class UserDetailController { private AccountManager accountManager; @RequestMapping(value = "update/{id}") public String updateForm(Model model) { return "account/userForm"; } @RequestMapping(value = "save/{id}") public String save(@ModelAttribute("user") User user, RedirectAttributes redirectAttributes) { accountManager.saveUser(user); redirectAttributes.addFlashAttribute("message", "Modify user " + user.getLoginName() + " success"); return "redirect:/account/user/"; } @ModelAttribute("user") public User getAccount(@PathVariable("id") Long id) { return accountManager.getUser(id); } }
5. JSP
5.1 写一个List页面,一个Form页面, 参见mini-web。5.2 对关键页面流程应该编写Selenium功能测试用例。
作者:Kei
出处:http://www.cnblogs.com/ikei/
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.