这是修改密码接口,但是直接请求的话会找不到empid。解决方法是:使用ThreadLocal拿到当前id
修改密码接口代码如下:
EmployeeController:
···
@PutMapping("/editPassword") public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO) { //如果新旧密码一样,直接抛出异常 if(passwordEditDTO.getOldPassword() .equals(passwordEditDTO.getNewPassword())){ return Result.error("新旧密码相同"); } employeeService.updatePassword(passwordEditDTO); return Result.success(); }
···
EmployeeServiceImpl:
···
@Override public void updatePassword(PasswordEditDTO passwordEditDTO) { //先验证密码是否正确 String password = passwordEditDTO.getOldPassword(); //把旧密码进行md5加密,再进行对比 password = DigestUtils.md5DigestAsHex(password.getBytes()); Employee employee = employeeMapper.getById(BaseContext.getCurrentId()); if(!password.equals(employee.getPassword())){ throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR); }else{ employee.setPassword(DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes())); employeeMapper.updateEmployee(employee); } }
···
EmployeeMapper:
···
@Select("select id ,name, username, password, phone, sex, id_number, status, create_time, update_time, create_user, update_user from employee where id = #{id}") Employee getById(long id);
void updateEmployee(Employee employee);
···
EmployeeMapper.xml:
···
<update id="updateEmployee"> update employee <set> <if test="idNumber != null"> id_number = #{idNumber},</if> <if test="name!= null"> name = #{name},</if> <if test="phone!= null"> phone = #{phone},</if> <if test="sex!= null"> sex = #{sex},</if> <if test="username!= null"> username = #{username},</if> <if test="updateTime!= null"> update_time = #{updateTime},</if> <if test="status != null"> status = #{status},</if> <if test="password != null"> password = #{password},</if> </set> where id = #{id} </update>
···