SSM案例-企业权限系统(13)- 查询所有用户
程序员文章站
2022-04-08 13:01:57
...
1 查询用户
1.1 页面
1.2 DAO
public interface IUserDao {
@Select("select * from users where username=#{username}")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "email", column = "email"),
@Result(property = "password", column = "password"),
@Result(property = "phoneNum", column = "phoneNum"),
@Result(property = "status", column = "status"),
@Result(property = "roles", column = "id", javaType = java.util.List.class,
many = @Many(select = "com.tzb.dao.IRoleDao.findRoleByUserId"))
})
public UserInfo findByUserName(String username) throws Exception;
@Select("select * from users")
List<UserInfo> findAll() throws Exception;
}
1.3 Service
- 接口
package com.tzb.service;
import com.tzb.domain.UserInfo;
import org.springframework.security.core.userdetails.UserDetailsService;
import java.util.List;
public interface IUserService extends UserDetailsService {
List<UserInfo> findAll() throws Exception;
}
- 实现类
@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserInfo userInfo = null;
try {
userInfo = userDao.findByUserName(username);
} catch (Exception e) {
e.printStackTrace();
}
// 自己的用户对象封装成 UserDetails的实现类User
// User user = new User(userInfo.getUsername(), "{noop}" + userInfo.getPassword(),
// getAuthority(userInfo.getRoles()));
User user = new User(userInfo.getUsername(), "{noop}" + userInfo.getPassword(),
userInfo.getStatus() == 0 ? false : true,
true,
true,
true,
getAuthority(userInfo.getRoles()));
return user;
}
// 集合中装入的是角色的描述
private List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
List<SimpleGrantedAuthority> list = new ArrayList<>();
for (Role role : roles) {
list.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));
}
return list;
}
@Override
public List<UserInfo> findAll() throws Exception {
return userDao.findAll();
}
}
1.4 Controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll.do")
public ModelAndView findAll() throws Exception {
ModelAndView mv = new ModelAndView();
List<UserInfo> userList = userService.findAll();
mv.addObject("userList",userList);
mv.setViewName("user-list");
return mv;
}
}
2 添加用户
2.1 页面
2.2 spring security 配置
2.3 DAO
public interface IUserDao {
@Select("select * from users where username=#{username}")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "email", column = "email"),
@Result(property = "password", column = "password"),
@Result(property = "phoneNum", column = "phoneNum"),
@Result(property = "status", column = "status"),
@Result(property = "roles", column = "id", javaType = java.util.List.class,
many = @Many(select = "com.tzb.dao.IRoleDao.findRoleByUserId"))
})
public UserInfo findByUserName(String username) throws Exception;
@Select("select * from users")
List<UserInfo> findAll() throws Exception;
@Insert("insert into users(email,username,password,phoneNum,status) values(#{email}," +
"#{username},#{password},#{phoneNum},#{status})")
void save(UserInfo userInfo) throws Exception;
}
2.4 Service
- 接口
public interface IUserService extends UserDetailsService {
List<UserInfo> findAll() throws Exception;
void save(UserInfo userInfo)throws Exception;
}
- 实现类
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
/**
* 添加用户
* @return
* @throws Exception
*/
@RequestMapping("/save.do")
public String save(UserInfo userInfo) throws Exception {
userService.save(userInfo);
return "redirect:findAll.do";
}
/**
* 查询所有用户
* @return
* @throws Exception
*/
@RequestMapping("/findAll.do")
public ModelAndView findAll() throws Exception {
ModelAndView mv = new ModelAndView();
List<UserInfo> userList = userService.findAll();
mv.addObject("userList",userList);
mv.setViewName("user-list");
return mv;
}
}
2.5 Controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
/**
* 添加用户
* @return
* @throws Exception
*/
@RequestMapping("/save.do")
public String save(UserInfo userInfo) throws Exception {
userService.save(userInfo);
return "redirect:findAll.do";
}
/**
* 查询所有用户
* @return
* @throws Exception
*/
@RequestMapping("/findAll.do")
public ModelAndView findAll() throws Exception {
ModelAndView mv = new ModelAndView();
List<UserInfo> userList = userService.findAll();
mv.addObject("userList",userList);
mv.setViewName("user-list");
return mv;
}
}