修改密码开发目录
需要用户提交原始密码和新密码,再根据当前登录的用户进行信息的修改操作
1.修改密码-持久层
1.1规划需要执行的SQL语句
1.2设计接口和抽象方法
//修改密码
Integer updatePassword(
Integer uid,
String password,
String modifiedUser,
Date modifiedTime
);
User findByUid(Integer uid);
1.3编写映射
<update id="updatePassword">
update t_user set password=#{password} ,modified_user=#{modifiedUser} ,modified_time=#{modifiedTime}
where uid=#{uid}
</update>
<!--User findByUid(Integer uid);-->
<select id="findByUid" resultMap="UserEntityMap" >
select * from t_user where uid=#{uid}
</select>
1.4单元测试
@Test
public void updatePasswordTest(){
Integer integer = userMapper.updatePassword(1, "321", "ljl", new Date());
System.out.println(integer);
}
/*User findByUid(Integer uid);*/
@Test
public void findByUidTest(){
User byUid = userMapper.findByUid(1);
System.out.println(byUid);
}
2.修改密码-业务层
2.1规划异常
用户的原密码错误,抛PasswordNotMatchException异常(前面已创建)
检测到is_delete字段为1和uid找不到都是抛出用户没有找到的异常,UsernameNotFoundException(前面已创建)
update在更新的时候,有可能产生未知的异常,抛UpdateException异常
package com.sdjzu.store.service.ex;
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);
}
}
2.2设计接口
void changePassword(
Integer uid,
String username,
String oldPassword,
String newPassword
);
2.3实现接口
@Override
public void changePassword(Integer uid, String username, String oldPassword, String newPassword) {
User byUid = userMapper.findByUid(uid);
if(byUid==null){
throw new UsernameNotFoundException("用户不存在");
}
User byUsername = userMapper.findByUsername(username);
if (oldPassword.equals(byUsername.getPassword())==false){
throw new PasswordNotMatchException("与旧密码不匹配");
}
Integer integer = userMapper.updatePassword(uid, newPassword, username, new Date());
if(integer!=1){
throw new UpdateException("更新数据产生未知的异常");
}
}
2.4单元测试
public void changePassword(){
iUserService.changePassword(3,"lisi","123","1234");
}
3.修改密码-控制层
3.13.1处理异常
UsernameNotFoundException异常和PasswordNotMatchException异常在前面的章节中已经处理过,现在只需要把UpdateException异常配置到统一的异常处理方法中
}else if(e instanceof UpdateException){
objectJsonResult.setState(5003);
objectJsonResult.setMessage("修改密码异常");
}
3.2设计请求
/users/change_password
post
String oldPassword,String newPassword,HttpSession session(uid和username可以通过session获取到,在处理方法的内部获取就可以了)//如果参数名用的是非pojo类型,就需要和表单中的name属性值保持一致
JsonResult
3.3处理请求
@RequestMapping("updatePassword")
public JsonResult<Void> updatePassword(String oldPassword,String newPassword,HttpSession session){
String username = getUsernameFromSession(session);
Integer uid = getUidFromSession(session);
userService.updatePassword(uid,username,oldPassword,newPassword);
return new JsonResult<Void>(ok);
}
测试:http://localhost:8080/store/users/updatePassword?oldPassword=123&newPassword=1
4.修改密码-前端页面
在password.html中添加ajax请求的处理
<script>
$("#btn-change-password").click(function () {
$.ajax({
url: "/store/users/updatePassword",
type: "POST",
data: $("#form-change-password").serialize() ,
dataType: "JSON",
success: function (json) {
if(json.state==200){
alert("修改密码成功")
}else{
alert("修改失败")
}
},
error: function (xhr) {
alert("未知错误"+xhr.message)
}
});
});
</script>