四、修改密码
需要用户提交旧密码和新密码,再根据当前用户信息进行修改
1 修改密码-持久层
1.规划需要执行的sql语句
根据uid修改用户数据,修改者和修改时间也要更改
update t_user set password = #{password},modified_user = #{modifiedUser},modified_time = #{modifiedTime} where uid = #{uid}
根据uid查询用户信息,检测是否被删除原始密码是否正确
select * from t_user where uid = #{uid}
2.设计接口和抽象方法
UserMapper接口
/**
* 根据uid修改用户数据
* @param uid
* @param password 新密码
* @param modifiedUser
* @param modifiedTime
* @return 受影响的行数,便于判断是否更新成功
*/
Integer updatePasswordByUid(Integer uid, String password, String modifiedUser, Date modifiedTime);
/**
* 根据uid查询用户信息
* @param uid
* @return 用户存在返回用户信息,否则返回null
*/
User finUserByUid(Integer uid);
UserMapper.xml
<update id="updatePasswordByUid">
update t_user set
password = #{
password},
modified_user = #{
modifiedUser},
modified_time = #{
modifiedTime}
where uid = #{
uid}
</update>
<select id="finUserByUid" resultMap="UserEntityMap">
select * from t_user where uid = #{
uid}
</select>
-
测试
@Test public void updatePassword(){ Integer i = userMapper.updatePasswordByUid(6,"123","zhou",new Date()); System.out.println(i); } @Test public void finUid(){ System.out.println(userMapper.finUserByUid(06)); }
2 修改密码-业务层
-
规划肯能出现的异常:用户密码错误、用户被删除、更新密码时异常(updatePasswordException)
-
设计接口和抽象方法
void changePassword(Integer uid,String username,String oldPassword,String newPassword);
@Override public void changePassword(Integer uid, String username, String oldPassword, String newPassword) { User user = userMapper.finUserByUid(uid); //判断用户是否存在或被删除 if (user == null || user.getIsDelete() == 1){ throw new UsernameNotFoundException("用户数据不存在"); } String oldMd5Password = getMD5Password(oldPassword, user.getSalt()); //判断老密码是否正确 if (!oldMd5Password.equals(user.getPassword())){ throw new PasswordNotMatchException("密码错误"); } String newMd5Password = getMD5Password(newPassword, user.getSalt()); //更改新密码