MyBatis-Plus通過version機(jī)制實(shí)現(xiàn)樂觀鎖的思路
MyBatis-Plus是通過version機(jī)制實(shí)現(xiàn)樂觀鎖的。
大致思路:
- 取出記錄,攜帶記錄的當(dāng)前
version; - 更新記錄的時候,比較記錄當(dāng)前的
version是否有改變; - 如果
version未改變,則更新記錄,并更新version,一般值+1; - 如果
version改變了,則不更新記錄。
version機(jī)制的核心思想就是,假設(shè)發(fā)生并發(fā)沖突的幾率很低,只有當(dāng)更新數(shù)據(jù)的時候采取檢查是否有沖突,而判斷是否有沖突的依據(jù)就是version的值是否被改變了。
配置
MyBatis-Plus中配置樂觀鎖分兩步:
- 實(shí)例化
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不能復(fù)用!!!
配置如下:
首先,實(shí)例化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默認(rèn)值賦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默認(rèn)賦值為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機(jī)制實(shí)現(xiàn)樂觀鎖的思路的文章就介紹到這了,更多相關(guān)MyBatis Plus實(shí)現(xiàn)樂觀鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaWeb入門:ServletContext詳解和應(yīng)用
這篇文章主要介紹了Java ServletContext對象用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-07-07
SpringBoot在項(xiàng)目停止(服務(wù)停止/關(guān)閉退出)之后執(zhí)行的方法
這篇文章主要給大家介紹了SpringBoot在項(xiàng)目停止(服務(wù)停止/關(guān)閉退出)之后執(zhí)行的兩種方法,實(shí)現(xiàn)DisposableBean接口和使用@PreDestroy注解,文中有詳細(xì)的代碼講解,具有一定的參考價值,需要的朋友可以參考下2023-12-12
SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦
這篇文章主要介紹了SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Java8新特性時間日期庫DateTime API及示例詳解
這篇文章主要介紹了Java8新特性時間日期庫DateTime API及示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
java四種引用及在LeakCanery中應(yīng)用詳解
這篇文章主要介紹了java四種引用及在LeakCanery中應(yīng)用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09

