在沒有修改之前,是用的
userService.updateById(user);
這個方法訪問的話會出現一些問題。修改的時候,傳入很多值,修改失敗的情況。
看mybatis-plus官方文檔,修改的話可以用 update方法,然后用條件構造器指定一些匹配方式,然后傳入一個實體類,實體類里面有什么內容就修改什么內容。
條件構造器
修改了相關代碼:
用戶修改密碼:
controller 層:
/**
* 修改
*/
@LoginRequired
@RequestMapping("/update")
public R update(@RequestBody UserEntity user) {
if (user == null) {
return R.error("參數不能為空").put("code", LexueConstant.REGISTER_PARAM_EMPTY);
}
//1-學生,2-教師,3-管理員
if (user.getUserType() != null && user.getUserType() != 1 && user.getUserType() != 2 && user.getUserType() != 3) {
return R.error("用戶類型指定錯誤,1-學生,2-教師,3-管理員").put("code", LexueConstant.REGISTER_USER_TYPE_ERROR);
}
if (user.getUsername() != null && !StringJudge.checkUsername(user.getUsername())) {
return R.error("賬號需6-12位的字母或數字").put("code", LexueConstant.REGISTER_USERNAME_ERROR);
}
if (user.getEmail() != null && !StringJudge.checkEmail(user.getEmail())) {
return R.error("請輸入正確的郵箱").put("code", LexueConstant.REGISTER_EMAIL_ERROR);
}
//注意,這里不能修改密碼,因為是單向加密的形式,所以這里只能把密碼設置成初始狀態:123456
if (user.getPassword() != null && user.getPassword().equals("123456")) {
user.setPassword(bCryptPasswordEncoder.encode("123456"));
} else if (user.getPassword()!=null&&!user.getPassword().equals(userService.getById(user.getId()).getPassword())) {
return R.error("只能設置密碼為“123456”,請在修改密碼的位置修改密碼").put("code", LexueConstant.NOT_UPDATE_PASSWORD);
}
userService.updateUser(user);
// userService.updateById(user);
return R.ok();
}
service層
@Override
public R updateUser(UserEntity userEntity) {
if(userEntity==null){
return R.error("參數不能為空").put("code", LexueConstant.NOT_NULL);
}
if(userEntity.getId()==null||userEntity.getId()<=0){
return R.error("用戶id不能為空").put("code", LexueConstant.NOT_NULL);
}
UpdateWrapper<UserEntity> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id",userEntity.getId());
Integer rows = userDao.update(userEntity, updateWrapper);
return R.ok().put("code", LexueConstant.SUCCESS).put("影響的行數", rows+"行");
}
idea重新打包,發布到雲服務器上重新運行:
postman修改測試:
后台頁面刷新后: