Spring Boot 事务反对

Spring Boot 应用事务非常简单,底层仍然采纳的是 Spring 自身提供的事务管理

➢ 在入口类中应用注解 @EnableTransactionManagement 开启事务反对

➢ 在拜访数据库的 Service 办法上增加注解 @Transactional 即可

1、案例思路

通过 SpringBoot +MyBatis 实现对数据库学生表的更新操作,在 service 层的办法中构建

异样,查看事务是否失效

项目名称:012-springboot-web-mybatis-transacation

该我的项目是在 011 的根底上增加新增办法,在新增办法中进行案例的演示

2、实现步骤

(9) 在 StudentController 中增加更新学生的办法

(10) 在 StudentService 接口中增加更新学生办法

(11) 在 StudentServiceImpl 接口实现类中对更新学生办法进行实现,并构建一个异样,同时在该办法上加@Transactional 注解

(12) 在 Application类上加@EnableTransactionManagement 开启事务反对

@EnableTransactionManagement 可选,然而业务办法上必须增加@Transactional 事务才失效

(13) 启动 Application,通过浏览器拜访进行测试

通过以上后果,阐明事务起作用了

(14) 正文掉 StudentServiceImpl 上的@Transactional 测试数据库的数据被更新