1.User.java
package com.zhongruan.bean;
public class User {
public User(){
}
public User(int id, String username,String password){
this.id = id;
this.username = username;
this.password = password;
}
public User(String username,String password){
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.PageInfo.java
package com.zhongruan.bean;
import java.util.List;
public class PageInfo<T> {
private List<T> list;
private int totalPage;
private int size;
private int totalCount;
private int currentPage;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@Override
public String toString() {
return "PageInfo{" +
"list=" + list +
", totalPage=" + totalPage +
", size=" + size +
", totalCount=" + totalCount +
", currentPage=" + currentPage +
'}';
}
}
3.Role.java
package com.zhongruan.bean;
public class Role {
private int id;
private String rolename;
private String roledesc;
public Role() {
}
public Role(int id, String rolename, String roledesc) {
this.id = id;
this.rolename = rolename;
this.roledesc = roledesc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public String getRoledesc() {
return roledesc;
}
public void setRoledesc(String roledesc) {
this.roledesc = roledesc;
}
@Override
public String toString() {
return "Role{" +
"id=" + id +
", rolename='" + rolename + '\'' +
", roledesc='" + roledesc + '\'' +
'}';
}
}
4.UserController.java
package com.zhongruan.controller;
import com.zhongruan.bean.PageInfo;
import com.zhongruan.bean.Role;
import com.zhongruan.bean.UserRole;
import com.zhongruan.bean.User;
import com.zhongruan.service.IRoleService;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@Autowired
private IRoleService roleService;
@RequestMapping("/login.do")
public ModelAndView login(User user, HttpSession session){
int id=userService.login(user.getUsername(),user.getPassword());
ModelAndView modelAndView = new ModelAndView();
if(id!=-1){
List<Integer> roleIds = roleService.findRoleId(id);
session.setAttribute("roleIds",roleIds);
session.setAttribute("user",user);
modelAndView.setViewName("main");
}else {
modelAndView.setViewName("../failer");
}
return modelAndView;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage,String username,HttpSession session,@RequestParam(defaultValue = "0") int type){
if(type==1){
session.setAttribute("searchname",username);
}else{
username=(String) session.getAttribute("searchname");
}
PageInfo<User> pageInfo=userService.findAll(currentPage,username);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("pageInfo",pageInfo);
modelAndView.setViewName("user-list");
return modelAndView;
}
@RequestMapping("/deleteById.do")
public String delete(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/add.do")
public String add(User user){
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id){
User user=userService.selectUserById(id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("user-update");
modelAndView.addObject("user",user);
return modelAndView;
}
@RequestMapping("/update.do")
public String update(User user){
userService.update(user);
return "redirect:findAll.do";
}
@RequestMapping("deleteAll.do")
public String deleteAll(String userList){
String[] strs=userList.split(",");
List<Integer> ids=new ArrayList<>();
for(String s:strs){
ids.add(Integer.parseInt(s));
}
userService.deleteAll(ids);
return "redirect:findAll.do";
}
@RequestMapping("toAddRole.do")
public ModelAndView toAddRole(int id){
List<Role> roleList=roleService.findRoleByUserId(id);
ModelAndView mv=new ModelAndView();
mv.addObject("roles",roleList);
mv.setViewName("user-role-add");
return mv;
}
}
5.UserDao.java
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserDao {
int getTotalCount(@Param("username") String username);
User findUserByUserName(String username);
List<User> findAll(@Param("start") int start, @Param("username") String username);
void deleteById(int id);
void add(User user);
User selectById(int id);
void update(User user);
void deleteAll(@Param("ids") List<Integer> ids);
}
6.RoleDao.java
package com.zhongruan.dao;
import com.zhongruan.bean.Role;
import java.util.List;
public interface RoleDao {
List<Integer> findRoleIdByUserId(int userId);
List<Role> findRoleByUserId(int id);
}
7.LigionFliter.java
package com.zhongruan.filter;
import com.zhongruan.bean.User;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
String uri=request.getRequestURI();
if(user==null && uri.indexOf("login.do")==-1){
response.sendRedirect(request.getContextPath()+"../login.jsp");
}else{
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
8.UserService.java
package com.zhongruan.service.Impl;
import com.zhongruan.bean.PageInfo;
import com.zhongruan.dao.UserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.zhongruan.bean.User;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserDao userDao;
@Override
public int login(String username, String password) {
User user = (User) userDao.findUserByUserName(username);
if(user!=null&&user.getPassword().equals(password)){
return user.getId();
}else{
return -1;
}
}
@Override
public PageInfo<User> findAll(int currentPage,String username) {
PageInfo<User> pageInfo=new PageInfo<>();
pageInfo.setSize(5);
pageInfo.setCurrentPage(currentPage);
int tc=userDao.getTotalCount(username);
pageInfo.setTotalCount(tc);
int tp=(int)Math.ceil(tc/5.0);
pageInfo.setTotalPage(tp);
if(currentPage<1){
pageInfo.setCurrentPage(1);
}else if(currentPage>tp){
pageInfo.setCurrentPage(tp);
}else{
pageInfo.setCurrentPage(currentPage);
}
int start=(pageInfo.getCurrentPage()-1)*5;
List<User> userList = userDao.findAll(start,username);
pageInfo.setList(userList);
return pageInfo;
}
// @Override
// public List<User> findAll() {
// return userDao.findAll();
//
//
// }
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void toUpdate(User user) {
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
@Override
public void deleteAll(List<Integer> ids) {
userDao.deleteAll(ids);
}
}
9.RoleService.java
package com.zhongruan.service.Impl;
import com.zhongruan.bean.Role;
import com.zhongruan.bean.UserRole;
import com.zhongruan.dao.RoleDao;
import com.zhongruan.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoleServicleImpl implements IRoleService {
@Autowired
private RoleDao roleDao;
@Override
public List<Integer> findRoleId(int userId) {
return roleDao.findRoleIdByUserId(userId);
}
@Override
public List<Role> findRoleByUserId(int id) {
return roleDao.findRoleByUserId(id);
}
}
10.IUserService.java
package com.zhongruan.service;
import com.zhongruan.bean.PageInfo;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserService {
int login(String username, String password);
PageInfo<User> findAll(int currentPage, String username);
void add(User user);
void deleteById(int id);
void toUpdate(User user);
User selectUserById(int id);
void update(User user);
void deleteAll(List<Integer> ids);
}
11.IRoleService.java
package com.zhongruan.service;
import com.zhongruan.bean.Role;
import com.zhongruan.bean.UserRole;
import java.util.List;
public interface IRoleService {
List<Integer> findRoleId(int userId);
List<Role> findRoleByUserId(int id);
}
12.UserMapping.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.dao.UserDao" >
<select id="findUserByUserName" parameterType="String" resultType="com.zhongruan.bean.User">
select * from tb_user where username=#{username}
</select>
<select id="findAll" resultType="com.zhongruan.bean.User">
select * from tb_user
<if test="username!=null and username!=''">
WHERE username LIKE concat("%",#{username},"%")
</if>
limit #{start},5
</select>
<delete id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
<insert id="add" parameterType="user">
insert into tb_user(username,password) value (#{username},#{password})
</insert>
<select id="selectById" parameterType="int" resultType="user">
select * from tb_user where id=#{id}
</select>
<update id="update" parameterType="user">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
<select id="getTotalCount" resultType="int">
select count(*) from tb_user
<if test="username!=null and username!=''">
WHERE username LIKE concat("%",#{username},"%")
</if>
</select>
<delete id="deleteAll" parameterType="list">
delete from tb_user where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
</mapper>
13.RoleMapping.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.dao.RoleDao" >
<select id="findRoleIdByUserId" parameterType="int" resultType="int">
select roleId from user_role where userId=#{userId}
</select>
<select id="findRoleByUserId" parameterType="int" resultType="role">
SELECT * FROM tb_role WHERE id NOT IN (SELECT roleId FROM user_role WHERE userId=#{id})
</select>
</mapper>