密码修改 需要用户提交原始密码和新密码,再根据当前登录用户进行信息的修改操作
持久层 规划需要执行的SQL语句 根据用户uid修改用户的password值
1 update t_user set password= ?,modified_user= ?,modified_time= ? where uid= ?;
根据uid查询用户数据,在修改密码之前,要保证当前用户数据存在,检测当前用户是否被标记删除,检测用户输入的原始密码是否正确
1 select * from t_user where uid= ?;
设计接口和抽象方法 在接口UserMapper
中定义对应的抽象方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Integer updatePasswordByUid (Integer uid, String password, String modifiedUser, Date modifiedDate) ; User findByUid (Integer uid) ;
抽象方法配置到映射文件UserMapper.xml
中 1 2 3 4 5 6 7 8 <select id ="findByUid" resultMap ="UserPojoMap" > select * from t_user where uid=#{uid}; </select > <update id ="updatePasswordByUid" > update t_user set password=#{password},modified_user=#{modifiedUser},modified_time=#{modifiedDate} where uid=#{uid}; </update >
功能单元测试 1 2 3 4 5 6 7 8 9 10 11 @Test public void findByUid () { Integer uid = 1 ; User user = userMapper.findByUid(uid); System.out.println(user); } @Test public void updatePasswordByUid () { Integer row = userMapper.updatePasswordByUid(2 ,"znew" ,"张飞" ,new Date ()); System.out.println(row); }
业务层 规划可能产生的异常 整个过程可能发生的错误
用户输入的原始密码错误;用户找不到或者已被删除(is_delete字段为1)
update执行过程中引发的未知错误
密码不匹配,用户找不到的异常类在登陆功能中以实现过
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class UpdateException extends ServiceException { public UpdateException () { super (); } public UpdateException (String message) { super (message); } public UpdateException (String message, Throwable cause) { super (message, cause); } public UpdateException (Throwable cause) { super (cause); } protected UpdateException (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super (message, cause, enableSuppression, writableStackTrace); } }
设计接口和抽象方法 用户密码修改核心方法
1 2 3 4 5 6 7 8 void alertPassword (Integer uid,String username,String oldPassword,String newPassword) ;
实现方法1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Override public void alertPassword (Integer uid, String username, String oldPassword, String newPassword) { User user = userMapper.findByUid(uid); if (user==null || user.getIsDelete()==1 ){ throw new UsernameNotFoundException ("当前用户不存在" ); } String salt = user.getSalt(); if (!user.getPassword().equals(getMD5password(oldPassword,salt))){ throw new PasswordNotMatchException ("用户密码错误" ); } String password = getMD5password(newPassword,salt); Integer rows = userMapper.updatePasswordByUid(uid, password, username, new Date ()); if (rows==0 ){ throw new UpdateException ("用户信息插入发生未知错误" ); } }
功能单元测试 1 2 3 4 @Test public void alertPassword () { iUserService.alertPassword(1 ,"李白" ,"123456" ,"li123456" ); }
控制层 处理异常 在基类BaseController
中添加新的异常类型
1 2 3 4 else if (e instanceof UpdateException){ result.setState(5003 ); result.setMessage("更新数据时未知异常" ); }
设计请求 1 2 3 4 url: /user/alert_ password method: post request params: String oldPassword,String newPassword response data: new JsonResult<Void>
处理请求 1 2 3 4 5 6 7 8 @RequestMapping("/alert_password") public JsonResult<Void> alertPassword (String oldPassword,String newPassword,HttpSession session) { Integer uid = getUidFromSession(session); String username = getUsernameFromSession(session); userService.alertPassword(uid,username,oldPassword,newPassword); return new JsonResult <>(200 ,"密码修改成功" ); }
前端页面开发 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <script> $("#btn-change-password" ).click (function ( ){ $.ajax ({ url : "/user/alert_password" ,type : "POST" ,data : $("#form-change-password" ).serialize () ,dataType : "JSON" ,success : function (data ){ if (data.state == 200 ){ alert ("密码修改成功" ); }else { alert ("密码修改失败 " +data.message ); } } ,error :function (xmh ){ alert ("密码修改失败" +xmh.status ); } }); }); </script>