SpringBoot中的修改以及删除操作
1.dao层文件
//修改操作(返回受影响的行数)
int updateUser(User user);
//删除操作(返回受影响的行数)
int deleteUser(Integer id);
2.service层文件
/*修改用户*/
public void updateUser(User user){
AssertUtil.isTrue(StringUtils.isBlank(user.getUserName()),"用户名为空");
AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"密码为空");
//通过用户名查询对象是否存在
User temp=userMapper.queryUserByUserName(user.getUserName());
//如果用户对象存在,但不是当前的对象
AssertUtil.isTrue(null!=temp&&!(user.getId()).equals(temp.getId()),"该用户已存在");
AssertUtil.isTrue(userMapper.updateUser(user)<1,"修改用户失败");
}
/*删除用户操作*/
public void deleteUser(Integer id){
AssertUtil.isTrue(null==id||null==userMapper.queryById(id),"用户不存在!");
AssertUtil.isTrue(userMapper.deleteUser(id)<1,"删除失败");
}
3.controller层
@PostMapping("/user")
public ResultInfo updateUser(User user){
ResultInfo resultInfo=new ResultInfo();
try{
userService.updateUser(user);
}catch(ParamException e){
resultInfo.setCode(e.getCode());
resultInfo.setResult(e.getMsg());
e.printStackTrace();
}catch(Exception e){
resultInfo.setCode(300);
resultInfo.setResult("添加失败");
e.printStackTrace();
}
return resultInfo;
}
@DeleteMapping("/user/{userid}")
public ResultInfo deleteUser(@PathVariable Integer userid){
ResultInfo resultInfo=new ResultInfo();
try{
userService.deleteUser(userid);
}catch(ParamException e){
resultInfo.setCode(e.getCode());
resultInfo.setResult(e.getMsg());
e.printStackTrace();
}catch(Exception e){
resultInfo.setCode(300);
resultInfo.setResult("删除失败");
e.printStackTrace();
}
return resultInfo;
}