当前位置:网站首页>Chapter 219 New SSM Integration - Getting Through Mysql Database Control Transactions (including interview questions)
Chapter 219 New SSM Integration - Getting Through Mysql Database Control Transactions (including interview questions)
2022-08-07 15:33:00 【LiuJia111222333】
第1集 Spring 常⻅的事务管理-⾯test site
简介:讲解Spring常⻅的事务管理
- 事务:多个操作,要么同时成功,or after failure⼀rollback
- 具备ACID四种特性
- Atomic(原⼦性)Consistency(⼀致性)Isolation(隔离性)Durability(持久性)
- You know that often⻅的Spring事务管理⽅式吗
- 编程式事务管理:
* 代码中调⽤beginTransaction()、commit()、rollback()related to business management⽅法,通过TransactionTempalte⼿Dynamic management(⽤的少)
声明式事务管理:
* 通过AOP实现,可配置⽂件⽅formula or annotation⽅management control of transactions(⽤的多)
- Do you know the essence of declarative transaction management?:
第2集 Spring事务的传播属性和隔离级别
- 事物传播⾏为介绍:
- 如果在开始当前事务之前,⼀business up and down⽂已经存在,At this time if⼲选项可以指定⼀transactional⽅法的执⾏⾏为
- @Transactional(propagation=Propagation.REQUIRED) 如果有事务, 那么加⼊事务, 没有的话新建⼀个(默认情况下)@Transactional(propagation=Propagation.NOT_SUPPORTED) not for this⽅law to open business@Transactional(propagation=Propagation.REQUIRES_NEW) 不管是否存在事务,都创建⼀个新的事务,原来的挂起,new executive⾏完毕,继续执⾏⽼的事务@Transactional(propagation=Propagation.MANDATORY) 必须在⼀execution in an existing transaction⾏,否则抛出异常@Transactional(propagation=Propagation.NEVER) 必须在⼀execution in a transaction that does not exist⾏,否则抛出异常(与Propagation.MANDATORY相反)@Transactional(propagation=Propagation.SUPPORTS) 如果其他bean调⽤这个⽅法,在其他bean中声明事务,那就⽤事务.如果其他bean没有声明事务,那就不⽤事务.@Transactional(propagation=Propagation.NESTED) 如果当前存在事务,则创建⼀个 Transactions run as nested transactions of the current transaction⾏; 如果当前没有事务,则该取值等价于Propagation.REQUIRED.
- 事务隔离级别: means if⼲The degree of isolation between concurrent transactions
- @Transactional(isolation = Isolation.READ_UNCOMMITTED) 读取未提交数据(会出现脏读, 不可重复读) basically do not use⽤
- @Transactional(isolation = Isolation.READ_COMMITTED) 读取已提交数据(会出现不可重复读和幻读)@Transactional(isolation = Isolation.REPEATABLE_READ) 可重复读(会出现幻读)@Transactional(isolation = Isolation.SERIALIZABLE) 串⾏化
MYSQL: 默认为REPEATABLE_READ级别
第3集 新版SpringBoot-Spring-MybatsiTransaction Control Explained
简介:新版SSM @Transactional Transaction Control Explained
快速创建SpringBoot+Spring+Mybatsi项⽬
https://start.spring.io/
#配置数据源 spring.datasource.driver-class-name =com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis? useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root #使⽤阿⾥巴巴druid数据源,默认使⽤⾃带的 #spring.datasource.type =com.alibaba.druid.pool.DruidDataSource #开启控制台打印sql mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
Maven插件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入JDBC-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!--导入mybatis依赖包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
</dependencies>控制层:
@RestController
@RequestMapping("api/v1/pri/videoOrder")
public class VideoOrderController {
@Autowired
private VideoOrderService videoOrderService;
//下单操作
@RequestMapping("save")
public JsonData Save(){
VideoOrder videoOrder = new VideoOrder();
videoOrder.setUserId(6);
videoOrder.setVideoId(6);
videoOrder.setVideoTitle("Mavericks looking for work");
int save = videoOrderService.save(videoOrder);
JsonData jsonData= save == 1 ? JsonData.JsonDataBuild("插入成功") : JsonData.JsonDataError("",-100,"插入失败");
return jsonData;
}
}服务层:Insert transaction in business
public interface VideoOrderService {
int save(VideoOrder videoOrder);
}@Service
@Transactional(propagation= Propagation.REQUIRED) //开启事务
public class VideoOrderServiceImpl implements VideoOrderService {
@Autowired
private VideoOrderMapper videoOrderMapper;
@Override
public int save(VideoOrder videoOrder) {
// int i = 1/ 0;
return videoOrderMapper.save(videoOrder);
}
}
Mapper:
@Repository
public interface VideoOrderMapper {
/**
* 关联查询
*/
List<VideoOrder> findVideoOrderList();
//下单操作
@Insert("insert into video_order(video_id,user_id,video_title) values(#{VideoId},#{userId},#{videoTitle})")
int save(VideoOrder videoOrder);
}启动类:
@SpringBootApplication
@MapperScan("com.example.ssmdemo.Mapper")
@EnableTransactionManagement //开启事务注解
public class SsmdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SsmdemoApplication.class, args);
}
}运行结果:

第4集 新版SpringBoot-Spring-MybatsiCourse summary and follow-up content preview
Comprehensive item⽬Practical planning
边栏推荐
- C语言文件读写操作/标准IO
- 03 【常用类型(下)】
- 我住得比较远,有好的开户途径么?手机开户股票开户安全吗?
- 【PTA】L2-033 简单计算器 (25 分)
- 【Verilog】组合逻辑电路 -- 程序设计及应用
- pip list导入与导出
- leetcode:636. 函数的独占时间【栈模拟】
- RTT学习笔记12-串口外设和串口驱动框架
- Programming Experts in C Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.8 Software is more difficult than hardware
- (imdb数据集)电影评论分类实战:二分类问题
猜你喜欢
随机推荐
LeetCode hot topic HOT 100 (4. Find the median of two positive-order arrays)
leetcode: 636. Exclusive time of function [stack simulation]
冒泡排序的理解
Open3D ICP精配准(点到面)
LeetCode 热题 HOT 100(1.两数之和)
How a Phase Locked Loop Works
win10 uwp 在 Grid 接收键盘消息
5步详解如何运用设计思维
服务器管理面板aaPanel使用中的一些问题汇总
06 【Generic】
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
win10 uwp 判断文件存在
RTT学习笔记9-IO设备模型
JWT的创建
RTT学习笔记12-串口外设和串口驱动框架
Threads of control and synchronization
labelme安装
打印从1到最大的n位数------2022/08/05
Lianshengde W801 series 2-WIFI one-key distribution network, information preservation
【PTA】L2-033 Simple Calculator (25 points)








