package com.service.impl; import com.dao.RoleDao; import com.dao.UserDao; import com.domain.Role; import com.domain.User; import com.mapper.RoleMapper; import com.mapper.UserMapper; import com.service.UserService; import com.utils.SqlSessionFactoryUtils; import org.apache.ibatis.session.SqlSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Autowired private RoleMapper roleMapper; public void setRoleMapper(RoleMapper roleMapper) { this.roleMapper = roleMapper; } public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public List list() { List userList = userMapper.findAll(); //封装userList中的每一个User的roles数据 for (User user : userList) { //获得user的id Long id = user.getId(); //将id作为参数 查询当前userId对应的Role集合数据 List roles = roleMapper.findRoleByUserId(id); user.setRoles(roles); } return userList; } public void save(User user, Long[] roleIds) { //第一步 向sys_user表中存储数据 Long userId = userMapper.save(user); //第二步 向sys_user_role 关系表中存储多条数据 userMapper.saveUserRoleRel(userId,roleIds); } public void del(Long userId) { //1、删除sys_user_role关系表 userMapper.delUserRoleRel(userId); //2、删除sys_user表 userMapper.del(userId); } public User login(String username, String password) { try { User user = userMapper.findByUsernameAndPassword(username, password); return user; }catch (EmptyResultDataAccessException e){ return null; } } /*private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } private RoleDao roleDao; public void setRoleDao(RoleDao roleDao) { this.roleDao = roleDao; } public List list() { List userList = userDao.findAll(); //封装userList中的每一个User的roles数据 for (User user : userList) { //获得user的id Long id = user.getId(); //将id作为参数 查询当前userId对应的Role集合数据 List roles = roleDao.findRoleByUserId(id); user.setRoles(roles); } return userList; } public void save(User user, Long[] roleIds) { //第一步 向sys_user表中存储数据 Long userId = userDao.save(user); //第二步 向sys_user_role 关系表中存储多条数据 userDao.saveUserRoleRel(userId,roleIds); } public void del(Long userId) { //1、删除sys_user_role关系表 userDao.delUserRoleRel(userId); //2、删除sys_user表 userDao.del(userId); } public User login(String username, String password) { try { User user = userDao.findByUsernameAndPassword(username, password); return user; }catch (EmptyResultDataAccessException e){ return null; } }*/ }