Spring Boot的事务管理注解@EnableTransactionManagement的使用
Spring Boot使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactional 便可。
第一步:在SpringBoot启动类上加上@EnableTransactionManagement 开启事务的注解
第二步:service包下的实现类中的方法上面加上@Transactional
当这个方法中报错进行回滚
@Override
@Transactional(rollbackFor = Exception.class)
public ResultBean<AppProductAddDTO> deleteProductAndExtension(Long id) {
//id为BusinessAppProduct表的id
//设置BusinessAppProduct的deleteFlag为1
appProductMapper.deleteById(id);
//设置Ext的deleteFlag为1
ext.deleteByBusinessId(id);
return ResultAssembler.getSuccess();
}