zoukankan      html  css  js  c++  java
  • 在Spring Boot中使用数据库事务

    一:在springboot启动类中添加注释 :@EnableTransactionManagement  

    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableFeignClients
    @EnableTransactionManagement
    public class DeploymentServiceApplication {
    
        public static void main(String[] args){
            SpringApplication.run(DeploymentServiceApplication.class, args);
        }
    
    }

    二:在相应地方加上注解:@Transactional 即可

    @Service
    public class DemoServiceImpl implements DemoService {
        @Autowired
        PersonRepository personRepository;
    
        @Transactional(rollbackFor = {IllegalArgumentException.class})
        @Override
        public Person savePersonWithRollBack(Person person) {
            Person p = personRepository.save(person);
            if (person.getName().equals("sang")) {
                throw new IllegalArgumentException("sang 已存在,数据将回滚");
            }
            return p;
        }
    
        @Transactional(noRollbackFor = {IllegalArgumentException.class})
        @Override
        public Person savePersonWithoutRollBack(Person person) {
            Person p = personRepository.save(person);
            if (person.getName().equals("sang")) {
                throw new IllegalArgumentException("sang已存在,但数据不会回滚");
            }
            return p;
        }
    }
  • 相关阅读:
    QTP err.number
    QTP参数化
    QTP基础
    QTP脚本补录
    QTP添加对象入库
    系统自带计算器自动化
    QTP安装
    App 测试
    本地化和国际化测试
    剑桥雅思写作高分范文ESSAY30
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/7220047.html
Copyright © 2011-2022 走看看