有时候我们在service层的某个方法中,得到的不是我们期望的结果,我们就要手动或者自动触发事务回滚
首先要设置@EnableTransactionManagement(springboot开启事务支持的方式),然后要在方法上设置@Transactional
场景1:
//在if后直接抛一个RuntimeException,让Controller中调用该方法的方法去try/catch,return错误类型等信息
if (pmConfigMapper.countByExample(pmConfigExample) > 0) throw new RuntimeException();
场景2:
//手动回滚异常,在service层中
if (pmConfigMapper.countByExample(pmConfigExample) > 0){
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}

网友评论