这是事务失效方法部分代码:@Service
@Slf4j
public class UserServiceImpl
extends ServiceImpl<UserMapper, SysUser>
implements UserService {
@Autowired
private UserMapper userMapper;
// TODO: 2019-12-09 事务不起作用
@Transactional()
@Override
public int addUser() {
SysUser user = new SysUser();
user.setUsername("pengda1");
user.setPassword("12344");
userMapper.insert(user);
int a = 1/0;
return 0;
}
}这是调用test@SpringBootTest
class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void trantest(){
userService.addUser();
}
}这是启动类:@SpringBootApplication
@EnableWebMvc
@EnableTransactionManagement
@MapperScan(value = "com.example.intelligentsubstationmangement.mapper")
public class IntelligentSubstationMangementApplication {
public static void main(String[] args) {
SpringApplication.run(IntelligentSubstationMangementApplication.class, args);
}
}这还不是最坑的,最坑的是,我的另外一个测试方法又是回 滚的。@Service
@Slf4j
public class TranscationalServiceImpl extends ServiceImpl<UserMapper, SysUser> implements TranscationalService {
@Autowired
private UserMapper userMapper;
// 事务使用成功,可能是mybatis-plus的通用service会影响事务
@Transactional
@Override
public int addUser() {
log.info("【事务开始】");
SysUser user = new SysUser();
user.setUsername("pengda1");
user.setPassword("12344");
userMapper.insert(user);
int a = 1/0;
return 0;
}
}我曾怀疑可能mybatia-plus 的通用service出现问题,但是后来又重新写了个TranscationalServiceImpl后,就发现是没有问题。但是,现在就不知到问题是出现在哪里了,明明两个方法都是一样的啊。简直迷的很。
添加回答
举报
0/150
提交
取消