JAVA 捕获 MySQLIntegrityConstraintViolationException (唯一索引重复)异常

在 service 层的数据库操作块无法捕捉因唯一索引而引发的数据库更新或插入异常,只能通过service方法抛出 DataAccess 异常,再到 Controller 层捕获

1
2
3
4
5
6
7
8
9
10
11
12
13
14

public Object update(UserEntity user) throws DataAccessException{
...
}

@RequestMapping("/update")
@ResponseBody
public Object update(@RequestBody userEntity){
try{
return userService.update(userEntity);
}catch(DataAccessException e){
return FunCommon.getReturnJson("msg","用户名重复");
}
}

 评论


本站使用 Material X 作为主题 , 总访问量为 次 。
隐藏