环境springboot2.7,mysql5.7
demo1
@Component
@Order(6)
@Slf4j
public class TestRunner implements ApplicationRunner {
@Autowired
TestService testService;
@Override
public void run(ApplicationArguments args) throws Exception {
testService.insertAndUpdate();
}
}
@Service("testService")
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
@Override
public boolean updateByIdOne() {
LambdaUpdateWrapper<Test> lambdaUpdateWrapper = new UpdateWrapper<Test>().lambda();
lambdaUpdateWrapper.eq(Test::getId,1);
lambdaUpdateWrapper.set(Test::getName,"test");
return this.update(lambdaUpdateWrapper);
}
@Transactional(rollbackFor = RuntimeException.class)
@Override
public boolean insertAndUpdate() {
boolean b = this.updateByIdOne();
Test test = new Test();
test.setName("2222");
boolean save = this.save(test);
if(b){
throw new RuntimeException("ts");
}
return save;
}
}
以上代码先跑一遍,看看抛出异常情况,能不能回滚
看库 毫无变化
看主键递增量其实是插入过了,我觉得事务还是生效了
demo2
@Service("testService")
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
@Transactional(rollbackFor = RuntimeException.class)
@Override
public boolean updateByIdOne() {
LambdaUpdateWrapper<Test> lambdaUpdateWrapper = new UpdateWrapper<Test>().lambda();
lambdaUpdateWrapper.eq(Test::getId,1);
lambdaUpdateWrapper.set(Test::getName,"test");
boolean update = this.update(lambdaUpdateWrapper);
if(update){
throw new RuntimeException("updateByIdOne");
}
LambdaUpdateWrapper<Test> lambdaUpdateWrapper2 = new UpdateWrapper<Test>().lambda();
lambdaUpdateWrapper2.eq(Test::getId,2);
lambdaUpdateWrapper2.set(Test::getName,"test");
boolean update1 = this.update(lambdaUpdateWrapper2);
return update;
}
@Transactional(rollbackFor = RuntimeException.class)
@Override
public boolean insertAndUpdate() {
Test test = new Test();
test.setName("2222");
boolean save = this.save(test);
boolean b = this.updateByIdOne();
return save;
}
}
执行结果
子父方法都有事务注解,事务生效
demo3
@Service("testService")
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
@Override
public boolean updateByIdOne() {
LambdaUpdateWrapper<Test> lambdaUpdateWrapper = new UpdateWrapper<Test>().lambda();
lambdaUpdateWrapper.eq(Test::getId,1);
lambdaUpdateWrapper.set(Test::getName,"test");
boolean update = this.update(lambdaUpdateWrapper);
if(update){
throw new RuntimeException("updateByIdOne");
}
return update;
}
@Transactional(rollbackFor = RuntimeException.class)
@Override
public boolean insertAndUpdate() {
Test test = new Test();
test.setName("2222");
boolean save = this.save(test);
boolean b = this.updateByIdOne();
return b;
}
}
insertAndUpdate插入成功后又回滚,update 更新成功也回滚,事务生效
demo4
@Service("testService")
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
@Transactional(rollbackFor = RuntimeException.class)
@Override
public boolean updateByIdOne() {
LambdaUpdateWrapper<Test> lambdaUpdateWrapper = new UpdateWrapper<Test>().lambda();
lambdaUpdateWrapper.eq(Test::getId,1);
lambdaUpdateWrapper.set(Test::getName,"test");
boolean update = this.update(lambdaUpdateWrapper);
if(update){
throw new RuntimeException("updateByIdOne");
}
LambdaUpdateWrapper<Test> lambdaUpdateWrapper2 = new UpdateWrapper<Test>().lambda();
lambdaUpdateWrapper2.eq(Test::getId,2);
lambdaUpdateWrapper2.set(Test::getName,"test");
boolean update1 = this.update(lambdaUpdateWrapper2);
return update;
}
@Override
public boolean insertAndUpdate() {
boolean b = this.updateByIdOne();
return b;
}
}
以上代码一跑,结果就很清楚了。
1、在同类中调用,二个方法都有加上事务注解,生效
2、同类中,子方法有事务注解,父类方法无事务注解,在controller层调用父类方法,子方法事务不生效
3、同类中,子方法无事务注解,父类方法有事务注解,在controller层调用父类方法,之方法事务生效