密码修改
1、编写接口方法和mybatis的SQL映射文件
Mybatis配置多参数SQL语句
当我们的SQL语句中有多个参数的时候,需要设置每个参数名对应的接口参数,不然会报错: Parameter ‘id’ not found. Available parameters are [argl, argg, paraml, param2)
在接口中使用注解
import org.apache.ibatis.annotations.Param; //... //修改用户密码 int updatePassword(@Param("password") String password, @Param("id") int id);
一定要注意标签的名字!!!修改语句是update 别上来就select
<update id="updatePassword"> update smbms.smbms_user set userPassword = #{password} where id = #{id} </update>
2、UserDao层实现修改方法与数据库交互
在UserDao添加如下方法,这里实现了调用了UserMapper的方法,使得业务代码在这里完成。
注意:mybatis执行增删改这三种语句一定要记得提交事务,否则不生效!!!
//修改当前用户密码 public boolean modifyPassword(int id,String password){ boolean flag = false; SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int res = mapper.updatePassword(password, id); //修改语句必须提交事务 sqlSession.commit(); if (res > 0){ flag = true; } sqlSession.close(); return flag; }
3、编写可复用的Servlet
我们这里要做的修改密码,后期还会有用户的增加删除,显然一个Servlet只实现一个功能太臃肿了。
这里还需要注意,method是我们修改密码是提交的表单form的一个属性,我们获取到method属性后需要先判断是否为空再去判断它的值是修改还是增加或删除。
import com.lyh.dao.UserDao; import com.lyh.pojo.User; import com.lyh.utils.Constant; import com.mysql.cj.util.StringUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class UserServlet extends HttpServlet { //这个Servlet会处用户的增删改 所以需要在这里定义三个方法 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //实现Servlet复用 String method = req.getParameter("method"); if (method!=null && method.equals("savepwd")){//保证method不为空很重要 this.updatePwd(req,resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public void updatePwd(HttpServletRequest req, HttpServletResponse resp){ //获取Session Object user = req.getSession().getAttribute(Constant.USER_SESSION); String newpassword = req.getParameter("newpassword"); boolean flag = false; if (user != null && !StringUtils.isNullOrEmpty(newpassword)){ UserDao dao = new UserDao(); flag = dao.modifyPassword(((User) user).getId(), newpassword); if (flag){ req.setAttribute("message","修改密码成功,请退出重新登录"); //移除session 重新登录 req.getSession().removeAttribute(Constant.USER_SESSION); }else {//修改失败 req.setAttribute("message","密码修改失败"); } }else { req.setAttribute("message","新密码有问题"); } try { req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
配置Servlet映射
<servlet> <servlet-name>UserServlet</servlet-name> <servlet-class>com.lyh.servlet.user.UserServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserServlet</servlet-name> <url-pattern>/jsp/user.do</url-pattern> </servlet-mapping>
添加修改界面
将pwdmodify.jsp复制到webapp/jsp/下
用户管理
1、用户数量统计
1、在接口定义三种统计不同身份用户数量的方法:
//根据用户名或者用户角色查询用户总数 int getUserCount(); //根据姓名模糊查询 int getUserCountByName(String name); //根据身份编号查询用户 1-管理员 2-经理 3-员工 int getUserCountByRole(int role);
2、在UserMapper.xml中实现SQL:
<select id="getUserCount" resultType="int"> select count(1) as count from smbms.smbms_user u,smbms.smbms_role r where u.userRole = r.id </select> <select id="getUserCountByRole" parameterType="int" resultType="int"> select count(1) as count from smbms.smbms_user u,smbms.smbms_role r where u.userRole = r.id and u.userRole = #{role} </select> <select id="getUserCountByName" parameterType="String" resultType="int"> select count(1) as count from smbms.smbms_user u,smbms.smbms_role r where u.userRole = r.id and u.userName like #{name} </select>
3、在UserDao中实现SQL参数传递:
//查询总用户数量 public int getUserCount(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int count = mapper.getUserCount(); sqlSession.close(); return count; } //根据用户身份查询数量 1-管理员 2-经理 3-员工 public int getUserCountByRole(int role){ int count = 0; SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); if (role==0){//如果是0就查询全部 count = mapper.getUserCount(); }else { count = mapper.getUserCountByRole(role); } sqlSession.close(); return count; } public int getUserCountByName(String name){ name = "%"+name+"%"; SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int count = mapper.getUserCountByName(name); sqlSession.close(); return count; }
2、用户列表
1、在接口定义三种统计不同身份用户列表的方法:
//分页展示所有用户列表 List<User> getUserList(@Param("currentPage") int currentPage,@Param("pageSize") int pageSize); //根据姓名模糊查询并返回用户列表 List<User> getUserListByName(@Param("name") String name,@Param("currentPage") int currentPage,@Param("pageSize") int pageSize); //根据身份编号查询用户列表 1-管理员 2-经理 3-员工 List<User> getUserListByRole(@Param("role") int role,@Param("currentPage") int currentPage,@Param("pageSize") int pageSize);
2、在UserMapper.xml中实现SQL:
<select id="getUserList" resultType="User"> select * from smbms_user su join smbms_role sr on su.userRole = sr.id order by su.id DESC limit #{currentPage},#{pageSize} </select> <select id="getUserListByName" resultType="User"> select * from smbms_user su join smbms_role sr on su.userRole = sr.id and su.userName like #{name} order by su.id DESC limit #{currentPage},#{pageSize} </select> <select id="getUserListByRole" resultType="User"> select * from smbms_user su join smbms_role sr on su.userRole = sr.id and su.userRole = #{role} order by su.id DESC limit #{currentPage},#{pageSize} </select>
3、在UserDao中实现SQL参数传递:
public List<User> getUserList(int currentPage,int pageSize){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); currentPage = (currentPage-1)*pageSize; List<User> userList = mapper.getUserList(0, 5); return userList; } public List<User> getUserListByName(String name,int currentPage,int pageSize){ name = "%"+name+"%"; SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserListByName(name, currentPage, pageSize); sqlSession.close(); return userList; } public List<User> getUserListByRole(int role,int currentPage,int pageSize){ List<User> userList = new ArrayList<User>(); SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); if (role==0){//如果是0就查询全部 userList = mapper.getUserList(currentPage, pageSize); }else { userList = mapper.getUserListByRole(role,currentPage,pageSize); } sqlSession.close(); return userList; }