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