springboot学习总结六(Spring boot下事务配置管理)

SpringBoot事务支持:

1.在入口类中使用@EnableTransactionManagement开启事务支持;

springboot学习总结六(Spring boot下事务配置管理)

2.在访问数据库的Service方法上添加注解@Transactional即可

StudentService.java

springboot学习总结六(Spring boot下事务配置管理)

StudentServiceImpl.java

springboot学习总结六(Spring boot下事务配置管理)

MyBaitsController.java

springboot学习总结六(Spring boot下事务配置管理)

执行结果分析:

除数不能为零,会抛出运行时异常,上一步的结果就会回滚

springboot学习总结六(Spring boot下事务配置管理)

数据库查询数据结果未改变:

springboot学习总结六(Spring boot下事务配置管理)

把@Transactional注解去掉,数据库数据结果进行更新:

springboot学习总结六(Spring boot下事务配置管理)