1、问题描述
springboot项目中配置事务,以前通过标签用过,上个内部项目,因为是在别人代码中使用了,发现好像没起作用(然后因为有框架保证,就没计较了),新启动项目又用到,验证了下,springboot项目下配置两个标签就可以了。
2、解决方案
2.1 说明
(1)其实就两个标签,一个用到启动类中(@EnableTransactionManagement),另一个就是那里需要就配置到哪里(@Transactional);
(2)方案,就随便找了个以前的测试项目,一个方法中包含:保存一条数据到数据库中和一个1除以0代码,然后看下加不加事务标签有什么区别;
2.2 代码说明
(1)controller类
@ResponseBody
@GetMapping("/tstest")
public String tstest() {
this.userService.tstest();
return "SUCCESS";
}
(2)service类
public void tstest() {
//保存
UserEntity userEntity = new UserEntity();
userEntity.setType(0);
userEntity.setOpenid("111