上一章已经讲解了如何使用mybatis实现数据库的操作,本章将注重业务处理逻辑的实现,即Service层开发,一般SpringBoot框架下Service层都会加上@Service标注,以便由框架统一管理。
为了简单描述这里不做太过于复杂的处理逻辑,用户处理就仅仅是用户的增删改查基本操作。
@Service public class UserManagerService { @Autowired UserDao userDao; public Boolean IsExitedUser(String username){ User user=userDao.selectByUsername(username); if (user!=null){ return true; }else { return false; } } public Boolean IsExitedUser(int id){ User user=userDao.selectByPrimaryKey(id); if (user!=null){ return true; }else { return false; } } public int addUser(User user){ if(!IsExitedUser(user.getName())) { userDao.insert(user); return 1; }else { return 0; } } public int delUser(int id){ if(IsExitedUser(id)){ userDao.deleteByPrimaryKey(id); return 1; }else { return 0; } } public int updateUser(User user){ if(IsExitedUser(user.getId())) { userDao.updateByPrimaryKey(user); return 1; }else{ return 0; } } public int getUserId(String username){ if(IsExitedUser(username)) { return userDao.selectByUsername(username).getId(); }else { return 0; } } public User getUserById(int id){ return userDao.selectByPrimaryKey(id); } }系统角色处理除了角色的增删改查外还包含了给用户授权处理,如下:
@Service public class RoleManagerService { @Autowired RoleDao roleDao; @Autowired UserManagerService userManagerService; public boolean isExitedRole(String role){ if(roleDao.selectByRole(role)!=null){ return true; }else{ return false; } } public int addNewRole(String rName,String role){ if(isExitedRole(role)){ return 0; }else{ Role role1=new Role(); role1.setRole(role); role1.setrName(rName); role1.setPage("1"); roleDao.insert(role1); return 1; } } public int delRole(String role){ if(!isExitedRole(role)){ return 0; }else{ roleDao.deleteByPrimaryKey(getRoleId(role)); return 1; } } public int getRoleId(String role){ if(isExitedRole(role)){ return roleDao.selectByRole(role).getId(); }else { return 0; } } public int addRoleToUser(String role,String username){ if(userManagerService.IsExitedUser(username)) { if (roleDao.selectByRole(role) != null) { User user = userManagerService.getUserById(userManagerService.getUserId(username)); if (!user.getRole().contains(role)) { String tmp = user.getRole() + "," + role; user.setRole(tmp); userManagerService.updateUser(user); return 1; }else { return 0; } } else { return 0; } }else { return 0; } } public int rmRoleFromUser(String role,String username){ if(userManagerService.IsExitedUser(username)) { if (roleDao.selectByRole(role) != null) { User user = userManagerService.getUserById(userManagerService.getUserId(username)); if (user.getRole().contains(role)) { String tmp = user.getRole().replace(","+role, ""); user.setRole(tmp); userManagerService.updateUser(user); return 1; }else { return 0; } } else { return 0; } }else { return 0; } } }愿望管理主要维护两件事情:愿望清单的维护以及愿望待办清单的维护,如下:
@Service public class WillingManager { @Autowired WillinglistDao willinglistDao; @Autowired WaitingtodoDao waitingtodoDao; public List<Willinglist> getWillingList(){ return willinglistDao.selectAll(); } public int addNewWilling(String willing,Date willingTime,String willingedPerson){ if (willing!=null) { Willinglist willinglist = new Willinglist(); willinglist.setSxbz("N"); willinglist.setWillingname(willing); willinglist.setTjsj(new Date()); willinglist.setWilingt(willingTime); willinglist.setWillingp(willingedPerson); willinglistDao.insert(willinglist); return 1; }else { return 0; } } public int getWilling(String willingPerson,String getter){ List<Willinglist> willinglists=willinglistDao.selectByWillingPerson(willingPerson); for (int i = 0; i <willinglists.size() ; i++) { Waitingtodo waitingtodo=new Waitingtodo(); waitingtodo.setDbry(getter); waitingtodo.setDbsx(willinglists.get(i).getWillingname()); waitingtodo.setSxbz(willinglists.get(i).getSxbz()); waitingtodo.setWillingd(willinglists.get(i).getWillingd()); waitingtodo.setWillingt(willinglists.get(i).getWilingt()); waitingtodoDao.insert(waitingtodo); } return 1; } }下一章开始将开发Controller层以及讲解基本Resultful API知识,注:如果使用接口方式,可以实现类中添加标签@Service(value=“实例名”)的方式添加实例,并且在调用类中使用@Resource(name=“实例名”)加载实例到你的变量当中