当前位置:首页 > 编程语言 > 正文

springboot中添加事务注解

11-18 编程语言

标签:map   list   main   auto   integer   nbsp   static   public   bsp   

1.首先在service层中的方法前添加@Transactional

@Service
public class UserService {
        @Autowired
        private UserMapper userMapper;

        public List<User> queryAll(){
            System.out.println("热部署");
            return userMapper.findAll();
        }
    @Transactional
    public void deleteById(Integer id) {
           userMapper.deleteById(id);
         //  int c=10/0;
    }
}

2.在springboot的启动中添加注解@EnableTransactionManagement

@SpringBootApplication
@EnableTransactionManagement //开启事务管理注解模式 最新的版本可以省略
@MapperScan("com.xz.springboot.mapper") //扫描该包下所有的接口并为该接口生成实现类
public class Springboot01Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot01Application.class, args);
    }

}

 

springboot中添加事务注解

标签:map   list   main   auto   integer   nbsp   static   public   bsp   

温馨提示: 本文由杰米博客推荐,转载请保留链接: https://www.jmwww.net/file/biancheng/12033.html