springboot使用事务
参考:https://www.jianshu.com/p/9b5eb43236cc
在有些service的操作,可能涉及多个dao层面的操作,我们希望这些操作要么同时成功,要么一起失败,这种情况下就需要使用事务
现在在springboot中使用事务非常简单,只需要加上注解@Transactional
@Service public class PersonService { @Resource private PersonMapper personMapper; @Resource private CompanyMapper companyMapper; @Transactional(rollbackFor = {RuntimeException.class, Error.class}) public void saveOne(Person person) { Company company = new Company(); company.setName("tenmao:" + person.getName()); companyMapper.insertOne(company); personMapper.insertOne(person); } }
网络上还说要在@SpringBootApplication
上添加注解@EnableTransactionManagement
,其实已经不需要了,O(∩_∩)O哈哈~