springboot中添加事務注解


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);
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM