Spring|Spring Boot 学习之路六,数据库三 ,事务
事务管理:比如两条信息同时插入数据库,如果一条不成功,那么另一条也不插入。这样就需要引入了事务管理,需要在方法上加 @Transactional事务注解
文章图片
结构.png 在上一节基础上添加ManInfoService,内容如下
package com.alun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by Administrator on 2017/5/29.
*/
@Service
public class ManInfoService {
@Autowired
ManInfoRepository manInfoRepository;
@Transactional
public void InsertTwo() {ManInfo manInfo = new ManInfo();
manInfo.setNickname("天下第一帅");
manInfo.setAge(100);
manInfoRepository.save(manInfo);
ManInfo manInfo2 = new ManInfo();
manInfo2.setNickname("天下第二帅");
manInfo2.setAge(99);
manInfoRepository.save(manInfo2);
}}
【Spring|Spring Boot 学习之路六,数据库三 ,事务】在ManInfoController中使用
@Autowired
ManInfoService manInfoService;
@PostMapping(value = "https://www.it610.com/article/insertTwo")
void insertTwo(){manInfoService.InsertTwo();
}
推荐阅读
- 由浅入深理解AOP
- Activiti(一)SpringBoot2集成Activiti6
- 继续努力,自主学习家庭Day135(20181015)
- python学习之|python学习之 实现QQ自动发送消息
- 一起来学习C语言的字符串转换函数
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 定制一套英文学习方案
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- 《深度倾听》第5天──「RIA学习力」便签输出第16期