MyBatis-Plus樂觀鎖插件的用法小結(jié)
什么是樂觀鎖:
就是我們每一次操作數(shù)據(jù)后,我們就會(huì)更改他的版本號(hào),當(dāng)另外的線程若想要對(duì)該數(shù)據(jù)進(jìn)行操作,檢查版本號(hào)是否與自己獲得的版本號(hào)一致,如果不一致,那么我們就會(huì)取消該操作。
簡介
說明
本文介紹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
版本號(hào)的字段上加注解
@Version private Integer version;
說明:
- 支持的數(shù)據(jù)類型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
- 整數(shù)類型下 newVersion = oldVersion + 1
- newVersion 會(huì)回寫到 entity 中
- 僅支持 updateById(id) 與 update(entity, wrapper) 方法
- 在 update(entity, wrapper) 方法下, wrapper 不能復(fù)用!!!
測(cè)試
MP會(huì)把設(shè)置進(jìn)去的版本號(hào)當(dāng)作更新條件,并且版本號(hào)+1更新進(jìn)去。
@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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解java 三種調(diào)用機(jī)制(同步、回調(diào)、異步)
這篇文章主要介紹了java 三種調(diào)用機(jī)制(同步、回調(diào)、異步),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Java中BigInteger類的使用方法詳解(全網(wǎng)最新)
這篇文章主要介紹了Java中BigInteger類的使用方法詳解,常用最全系列,本章作為筆記使用,內(nèi)容比較全面,但常用的只有:構(gòu)造函數(shù),基本運(yùn)算以及compareTo(),intValue(),setBit(),testBit()方法,需要的朋友可以參考下2023-05-05SpringMVC 中配置 Swagger 插件的教程(分享)
下面小編就為大家分享一篇SpringMVC 中配置 Swagger 插件的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12SpringBoot多線程進(jìn)行異步請(qǐng)求的處理方式
這篇文章主要介紹了SpringBoot多線程進(jìn)行異步請(qǐng)求的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜2021-12-12mybatis plus使用redis作為二級(jí)緩存的方法
這篇文章主要介紹了mybatis plus使用redis作為二級(jí)緩存的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09