基于SSM框架实现一个用户系统(登录,用户列表,分页,增删改查,用户角色管理功能)
首先搭建一个Maven工程,配置好Tomcat,mybatis等
数据库 tb_role tb_user user_role(这里只给了第一个用户管理员限权,可以对其他用户添加管理员,必须要用第一个用户登录)
bean 这三个就不用多说了,直接按照数据库字段的打。 为了实现分页功能,我们需要引入一个类叫PageInfo,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 + '}' ; } }
dao 我们需要用到两个Dao,一个是用于用户表的增删改查,另外一个是用于角色表的增删改查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.zhongruan.dao; import com.zhongruan.bean.Role;import com.zhongruan.bean.UserRole;import java.util.List;public interface RoleDao { List<Integer> findRoleIdByUserId (int userId) ; List<Role> findRoleByUserId (int id) ; void addRole (UserRole userRole) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.zhongruan.dao; import com.zhongruan.bean.User;import org.apache.ibatis.annotations.Param;import java.util.List;public interface UserDao { 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) ; int getTotalCount (@Param("username" )String username) ; void deleteAll (@Param("ids" ) List<Integer> ids) ; }
Service 同样两个service接口和两个实现类 接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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) ; void add (List<Integer> ids, String userId) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 deleteById (int id) ; void add (User user) ; User selectUserById (int id) ; void update (User user) ; void deleteAll (List<Integer> ids) ; }
实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 RoleService 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); } @Override public void add (List<Integer> ids, String userId) { for (int roleId:ids){ UserRole userRole=new UserRole (); userRole.setUserId (Integer.parseInt (userId)); userRole.setRoleId (roleId); roleDao.addRole (userRole); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 package com.zhongruan.service.impl; import com.zhongruan.bean.PageInfo;import com.zhongruan.bean.User;import com.zhongruan.dao.UserDao;import com.zhongruan.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.util.List;@Service public class UserService implements IUserService { @Autowired private UserDao userDao; @Override public int login (String username, String password) { User user = userDao.findUserByUserName (username); if (user!=null && user.getPassword ().equals (password)){ return user.getId (); } return -1 ; } @Override public PageInfo<User> findAll (int currentPage,String username) { PageInfo<User> pageInfo=new PageInfo<>(); pageInfo.setSize (5 ); 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 void deleteById (int id) { userDao.deleteById (id); } @Override public void add (User user) { userDao.add (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); } }
controller 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 package com.zhongruan.controller;import com.zhongruan.bean.PageInfo;import com.zhongruan.bean.Role;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.bind.annotation.ResponseBody;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,@RequestParam(defaultValue = "0") int type,HttpSession session) { 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.addObject("id" ,id); mv.setViewName("user-role-add" ); return mv; } @RequestMapping("addRole.do") @ResponseBody public String add (String roleList,String userId) { String[] strs = roleList.split("," ); List<Integer> ids=new ArrayList <>(); for (String s:strs){ ids.add(Integer.parseInt(s)); } roleService.add(ids,userId); return "" ; } }
filter(拦截器) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 () { } }
mapper文件 UserMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 <?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="user" > select * from tb_user where username=#{username} </select> <select id="findAll" resultType="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) values (#{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>
RoleMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?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> <insert id="addRole" parameterType="userRole" > insert into user_role (userId,roleId) values (#{userId},#{roleId}) </insert> </mapper>
jsp,完整项目可以从网盘下载: 链接: https://pan.baidu.com/s/1Lse-D_KcDgMFow9nfYZVzA?pwd=zwhe 提取码: zwhe 复制这段内容后打开百度网盘手机App,操作更方便哦
PhD Student @ Hong Kong Polytechnic University