MyBatis-Plus樂觀鎖插件的用法小結(jié)
什么是樂觀鎖:
就是我們每一次操作數(shù)據(jù)后,我們就會更改他的版本號,當(dāng)另外的線程若想要對該數(shù)據(jù)進行操作,檢查版本號是否與自己獲得的版本號一致,如果不一致,那么我們就會取消該操作。
簡介
說明
本文介紹Mybatis-Plus的樂觀鎖插件的用法。
官網(wǎng)網(wǎng)址
配置樂觀鎖插件
@Configuration public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return mybatisPlusInterceptor; } }
Entity
版本號的字段上加注解
@Version private Integer 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ù)用!!!
測試
MP會把設(shè)置進去的版本號當(dāng)作更新條件,并且版本號+1更新進去。
@Test public void update(){ User user = userMapper.getById(1L); user.setEmail("Test1111@email.com"); user.setUpdateTime(LocalDateTime.now()); int update = userMapper.updateById(user); System.out.println(update); }
DEBUG==> Preparing: UPDATE sys_user SET email=?, update_time=?, version=? WHERE id=? AND version=? DEBUG==> Parameters: Test1111@email.com(String), 2019-09-19T16:00:38.149(LocalDateTime), 2(Integer), 1(Long), 1(Integer) DEBUG<== Updates: 1
到此這篇關(guān)于MyBatis-Plus樂觀鎖插件的用法的文章就介紹到這了,更多相關(guān)MyBatis-Plus樂觀鎖插件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解java 三種調(diào)用機制(同步、回調(diào)、異步)
這篇文章主要介紹了java 三種調(diào)用機制(同步、回調(diào)、異步),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Java中BigInteger類的使用方法詳解(全網(wǎng)最新)
這篇文章主要介紹了Java中BigInteger類的使用方法詳解,常用最全系列,本章作為筆記使用,內(nèi)容比較全面,但常用的只有:構(gòu)造函數(shù),基本運算以及compareTo(),intValue(),setBit(),testBit()方法,需要的朋友可以參考下2023-05-05SpringMVC 中配置 Swagger 插件的教程(分享)
下面小編就為大家分享一篇SpringMVC 中配置 Swagger 插件的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12