1 @Transactional 2 @ApiOperation(value = "添加一个用户信息") 3 @RequestMapping(value = "/create", method = RequestMethod.POST) 4 public RESTful create() { 5 return userService.insert(null, null); 6 }
1 public RESTful insert(User user, Role role) { 2 try { 3 Date date = new Date(); 4 5 user = new User(); 6 user.setName("tq2"); 7 user.setAge(26); 8 user.setCreate_at(date); 9 10 userMapper.insertUser(user); 11 12 role = new Role(); 13 //role.setName("role1"); 14 role.setCreate_at(date); 15 16 roleMapper.insert(role); 17 18 return new RESTful(CodeEnum.成功.getCode(), CodeEnum.成功.getMsg()); 19 } catch (Exception ex) { 20 return new RESTful(CodeEnum.系统异常.getCode(), CodeEnum.系统异常.getMsg()); 21 } 22 }
1、role表的 name 字段不可空,但并不执行回滚
参考:https://www.cnblogs.com/leohe/p/6673096.html?utm_source=itdadao&utm_medium=referral
实现方式一
1
2 @ApiOperation(value = "添加一个用户信息") 3 @RequestMapping(value = "/create", method = RequestMethod.POST) 4 public RESTful create() { 5 try { 6 return userService.insert(null, null); 7 } catch (Exception ex) { 8 return new RESTful(CodeEnum.系统异常.getCode(), CodeEnum.系统异常.getMsg()); 9 } 10 }
1 @Transactional 2 public RESTful insert(User user, Role role) { 3 try { 4 Date date = new Date(); 5 6 user = new User(); 7 user.setName("tq2"); 8 user.setAge(26); 9 user.setCreate_at(date); 10 11 userMapper.insertUser(user); 12 13 role = new Role(); 14 //role.setName("role1"); 15 role.setCreate_at(date); 16 17 roleMapper.insert(role); 18 19 return new RESTful(CodeEnum.成功.getCode(), CodeEnum.成功.getMsg()); 20 } catch (Exception ex) { 21 throw new RuntimeException(); 22 } 23 }
实现方式二
1
2 @ApiOperation(value = "添加一个用户信息") 3 @RequestMapping(value = "/create", method = RequestMethod.POST) 4 public RESTful create() { 5 return userService.insert(null, null); 6 }
1 @Transactional 2 public RESTful insert(User user, Role role) { 3 try { 4 Date date = new Date(); 5 6 user = new User(); 7 user.setName("tq2"); 8 user.setAge(26); 9 user.setCreate_at(date); 10 11 userMapper.insertUser(user); 12 13 role = new Role(); 14 //role.setName("role1"); 15 role.setCreate_at(date); 16 17 roleMapper.insert(role); 18 19 return new RESTful(CodeEnum.成功.getCode(), CodeEnum.成功.getMsg()); 20 } catch (Exception ex) { 21 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 22 return new RESTful(CodeEnum.系统异常.getCode(), CodeEnum.系统异常.getMsg()); 23 } 24 }