事务注解@Transactional

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Role insert(Role role) throws Exception {
        userMapper.deleteById(60);
        if(null!=role){
// 抛出Exception,而rollbackFor = RuntimeException.class,不回滚
             throw new Exception();
        }

}

注意:rollbackFor = RuntimeException.class可以不写,@Transactional注解默认运行是异常,回滚

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Role insert(Role role) throws Exception {
        userMapper.deleteById(60);
        if(null!=role){
// 抛出Exception,而rollbackFor = RuntimeException.class,回滚
             throw new Exception();
        }

}

注意:rollbackFor = Exception.class,回滚的对应的异常就更广泛,这种情况下都,回滚

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Role insert(Role role) throws Exception {
        userMapper.deleteById(60);
        if(null!=role){
// 抛出RuntimeException,上面写的rollbackFor = RuntimeException.class没有关系,不写也不要紧,只要是运行异常,回滚
             throw new  RuntimeException();
        }

}

总结:自定义异常,ThrowableProblem,JUnitException,计算异常,空指针异常,99%的异常都是集成RuntimeException

你可能感兴趣的:(java工具包,java代码优化,java,开发语言)