MyBatis-Plus通過version機制實現(xiàn)樂觀鎖的思路
MyBatis-Plus
是通過version
機制實現(xiàn)樂觀鎖的。
大致思路:
- 取出記錄,攜帶記錄的當前
version
; - 更新記錄的時候,比較記錄當前的
version
是否有改變; - 如果
version
未改變,則更新記錄,并更新version
,一般值+1
; - 如果
version
改變了,則不更新記錄。
version
機制的核心思想就是,假設(shè)發(fā)生并發(fā)沖突的幾率很低,只有當更新數(shù)據(jù)的時候采取檢查是否有沖突,而判斷是否有沖突的依據(jù)就是version
的值是否被改變了。
配置
MyBatis-Plus
中配置樂觀鎖分兩步:
- 實例化
OptimisticLockerInnerInterceptor
,并添加到MyBatis-Plus
的攔截器鏈中; - 定義
version
字段,并在Entity
中使用@Version
注解注釋version
字段。
說明:
支持的數(shù)據(jù)類型只有:
int
、Integer
、long
、Long
、Date
、Timestamp
、LocalDateTime
;整數(shù)類型下
newVersion = oldVersion + 1
;
newVersion
會回寫到entity
中;僅支持
updateById(id)
與update(entity, wrapper)
方法;在
update(entity, wrapper)
方法下,wrapper
不能復用!!!
配置如下:
首先,實例化OptimisticLockerInnerInterceptor
,并添加到攔截器鏈中:
@Configuration public class MyBatisPlusConfig { /** * 插件配置 * * @return */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 向MyBatis-Plus的過濾器鏈中添加分頁攔截器,需要設(shè)置數(shù)據(jù)庫類型(主要用于分頁方言) interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 添加樂觀鎖攔截器 interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return interceptor; } }
然后,使用@Version
注解:
@Data @TableName("tb_user") public class UserEntity { private Long id; private String name; private Integer age; private String email; @TableField(value = "create_time", fill = FieldFill.INSERT) private Date createTime; @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) private Date updateTime; @TableLogic(value = "0", delval = "-1") @TableField(value = "delete_flag", fill = FieldFill.INSERT) private Integer deleteFlag; @Version @TableField(value = "version", fill = FieldFill.INSERT) private Integer version; }
配置insert
時候,version
默認值賦1
:
/** * 自動填充字段值得配置 */ @Component public class AutoFillFieldValueConfig implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { this.strictInsertFill(metaObject, "createTime", Date.class, new Date()); this.strictInsertFill(metaObject, "updateTime", Date.class, new Date()); this.strictInsertFill(metaObject, "deleteFlag", Integer.class, 0); this.strictInsertFill(metaObject, "version", Integer.class, 1); } @Override public void updateFill(MetaObject metaObject) { this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); } }
測試一下
1.測試新增記錄
首先新增一條數(shù)據(jù):
@Test public void testVersionInsert() { // 插入一個新的用戶 UserEntity newUser = new UserEntity(); newUser.setId(12L); newUser.setName("Kelly"); newUser.setAge(28); newUser.setEmail("Kelly@163.com"); userMapper.insert(newUser); }
控制臺日志:
==> Preparing: INSERT INTO tb_user ( id, name, age, email, create_time, update_time, delete_flag, version ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 12(Long), Kelly(String), 28(Integer), Kelly@163.com(String), 2021-09-25 00:14:23.894(Timestamp), 2021-09-25 00:14:23.896(Timestamp), 0(Integer), 1(Integer)
<== Updates: 1
數(shù)據(jù)庫數(shù)據(jù):
可以看到,新增記錄時,
version
默認賦值為1
。
2.測試更新記錄
下面,來測試一下更新記錄,看看version
的變化。
@Test public void testVersionUpdate() { // 查詢用戶記錄 UserEntity updateUser = userMapper.selectById(12L); // 更新用戶記錄 updateUser.setId(12L); updateUser.setAge(30); userMapper.updateById(updateUser); }
注意:這里有一個坑!
一定要先查詢出這條數(shù)據(jù),再更新,樂觀鎖才會生效!??!
控制臺打印的日志:
==> Preparing: SELECT id,name,age,email,create_time,update_time,delete_flag,version FROM tb_user WHERE id=? AND delete_flag=0
==> Parameters: 12(Long)
<== Columns: id, name, age, email, create_time, update_time, delete_flag, version
<== Row: 12, Kelly, 30, Kelly@163.com, 2021-09-25 00:14:24, 2021-09-25 00:20:24, 0, 1
<== Total: 1......
==> Preparing: UPDATE tb_user SET name=?, age=?, email=?, create_time=?, update_time=?, version=? WHERE id=? AND version=? AND delete_flag=0
==> Parameters: Kelly(String), 30(Integer), Kelly@163.com(String), 2021-09-25 00:14:24.0(Timestamp), 2021-09-25 00:20:24.0(Timestamp), 2(Integer), 12(Long), 1(Integer)
<== Updates: 1
數(shù)據(jù)庫數(shù)據(jù):
可以看到,version
字段由原來的1
,更新為2
。
到此這篇關(guān)于MyBatis-Plus通過version機制實現(xiàn)樂觀鎖的思路的文章就介紹到這了,更多相關(guān)MyBatis Plus實現(xiàn)樂觀鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring框架學習之Spring?@Autowired實現(xiàn)自動裝配的代碼
自動裝配就是說,你不用手動實現(xiàn)bean之間的組合關(guān)系,只要使用了@Autowired注解,程序就會自動的注入這個需要的bean,前提是你的Spring容器有這個bean,這篇文章主要介紹了Spring?@Autowired實現(xiàn)自動裝配,需要的朋友可以參考下2021-12-12Java中用Socket實現(xiàn)HTTP文件上傳實例
本篇文章主要介紹了Java中用Socket實現(xiàn)HTTP文件上傳實例,詳細的介紹了通過讀取Socket的輸入流來實現(xiàn)一個文件上傳的功能,有興趣的同學可以一起了解一下2017-04-04