使用步骤:
1.在pom中添加spring-boot-starter-jdbc依赖
![96d33b04774776cc847995a787e1cb53.png](https://img-blog.csdnimg.cn/img_convert/96d33b04774776cc847995a787e1cb53.png)
2.在启动类上添加@EnableTransactionManagement
![644ca0b339562b7635ac58a4e888bec8.png](https://img-blog.csdnimg.cn/img_convert/644ca0b339562b7635ac58a4e888bec8.png)
3.在业务层(记住是业务层实现类)类上,或者方法上添加@Transactional(rollbackFor=Exception.class)
i.如果加在类上,这个类下的所有方法都将会被加上事务管理(rollbackFor=Exception.class只要出现异常就会回滚)
ii.如果加在方法上,这个方法会被加上事务管理,异常回滚
![16fb9c6695fdd5c6d6a69b86aad42a99.png](https://img-blog.csdnimg.cn/img_convert/16fb9c6695fdd5c6d6a69b86aad42a99.png)
这样事务就完成了
个人建议:在控制层上的相应方法中 将全部代码放到try-catch中,一旦业务层发生异常事务回滚,就会给用户提供有用的信息,而不是白板页(如下)
![9d5a2edbb7cdd206e636d40a451561ff.png](https://img-blog.csdnimg.cn/img_convert/9d5a2edbb7cdd206e636d40a451561ff.png)