// 事务回滚demo @RequestMapping(value = "/testStudents",method = RequestMethod.POST) @Transactional public String gktllkiy(Student student) { // 保存 try { //int result = userService.addStudent(student); student.setName("士大夫11"); int result1 = userService.addStudent(student);//新增数据 Student t2 = null; System.out.println(t2.c()); student.setName("错误的事务"); int result2 = userService.addStudent(student); } catch (Exception e) { e.printStackTrace(); TransactionAspectSupport.currentTransactionStatus().setRollbackonly();//就是这一句了,加上之后,如果抛了空指针异常, //前面新是会回滚的 } return null; }
@Data public class Student { private Long id; private String name; private Long age; private Long conut; public String c(){ return "123"; } }
运行图:
最后事务回滚
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)