Mybatis-Plus樂(lè)觀鎖配置流程
一.樂(lè)觀鎖實(shí)現(xiàn)
1.配置插件
(1).XML方式
<bean class="com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor" id="optimisticLockerInnerInterceptor"/> <bean id="mybatisPlusInterceptor" class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor"> <property name="interceptors"> <list> <ref bean="optimisticLockerInnerInterceptor"/> </list> </property> </bean>
(2).Springboot注解方式
下方為博主使用時(shí)場(chǎng)景 單獨(dú)寫config帶@Bean注解
private MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mpi = new MybatisPlusInterceptor(); //添加樂(lè)觀鎖攔截器 mpi.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return mpi; }
單獨(dú)寫config文件如下:
// Spring Boot 方式 @Configuration @MapperScan("按需修改") public class MybatisPlusConfig { /** * 舊版 */ @Bean public OptimisticLockerInterceptor optimisticLockerInterceptor() { return new OptimisticLockerInterceptor(); } /** * 新版 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return mybatisPlusInterceptor; } }
2.實(shí)體類字段增加注解
@Version private Integer versionNumber;
注意:支持的數(shù)據(jù)類型有
int,Integer,long,Long,Date,Timestamp,LocalDateTime
并且僅支持updateById(id) 與 update(entity, wrapper)
方法在update(entity, wrapper)
方法下, wrapper 不能復(fù)用!!!
二.Parameter ‘MP_OPTLOCK_VERSION_ORIGINAL‘ not found. Available parameters are [param1, et]問(wèn)題解決
本身樂(lè)觀鎖使用比較簡(jiǎn)單 但是遇到了樂(lè)觀鎖插件注入失敗問(wèn)題 網(wǎng)上解決思路如下:
1、先在數(shù)據(jù)庫(kù)表中添加version字段
2、在實(shí)體類中定義version字段,并使用@Version注解標(biāo)記
3、在mybatis-plus攔截器中添加OptimisticLockerInnerInterceptor()樂(lè)觀鎖攔截器
但是并未解決我的問(wèn)題 隨后查看項(xiàng)目 發(fā)現(xiàn)項(xiàng)目中已經(jīng)創(chuàng)建了一個(gè)sqlSessionFactory:
本身mybatisplus在啟動(dòng)項(xiàng)目時(shí)會(huì)自動(dòng)掃描并注入相關(guān)插件 但是此處單獨(dú)配置后 并且添加了@Primary注解 后續(xù)就只會(huì)走此處的配置 所以我們?cè)谕獠刻砑拥奶砑訕?lè)觀鎖插件實(shí)際并未真正添加成功 或者說(shuō)并沒(méi)有走我們添加樂(lè)觀鎖插件的代碼 解決如下:
還有一種思路就是 啟動(dòng)類禁用掉之前配置 那我們單獨(dú)寫的添加樂(lè)觀鎖配置類也可以生效~
以上就是Mybatis-Plus樂(lè)觀鎖配置流程的詳細(xì)內(nèi)容,更多關(guān)于Mybatis-Plus樂(lè)觀鎖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- MyBatis-Plus樂(lè)觀鎖插件的用法小結(jié)
- SpringBoot整合MyBatis-Plus樂(lè)觀鎖不生效的問(wèn)題及解決方法
- Mybatis-Plus進(jìn)階分頁(yè)與樂(lè)觀鎖插件及通用枚舉和多數(shù)據(jù)源詳解
- MyBatis-Plus通過(guò)version機(jī)制實(shí)現(xiàn)樂(lè)觀鎖的思路
- mybatis-plus樂(lè)觀鎖實(shí)現(xiàn)方式詳解
- MP(MyBatis-Plus)實(shí)現(xiàn)樂(lè)觀鎖更新功能的示例代碼
- MyBatis-Plus 樂(lè)觀鎖的具體實(shí)現(xiàn)
相關(guān)文章
SpringBoot集成MongoDB的實(shí)現(xiàn)
本文主要介紹了SpringBoot集成MongoDB的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01SpringBoot項(xiàng)目啟動(dòng)執(zhí)行任務(wù)的多種方法小結(jié)
這篇文章主要介紹了SpringBoot項(xiàng)目啟動(dòng)執(zhí)行任務(wù)的多種方法小結(jié),本文給大家分享的這幾種方法經(jīng)常會(huì)被用到,當(dāng)我們的項(xiàng)目啟動(dòng)后需要調(diào)用對(duì)應(yīng)的方法,用來(lái)項(xiàng)目的初始化等,本文通過(guò)示例代碼講解的非常詳細(xì),需要的朋友參考下吧2023-07-07關(guān)于pom.xml中maven無(wú)法下載springcloud包問(wèn)題
小編遇到這樣一個(gè)問(wèn)題spring-cloud-starter-feign,spring-cloud-starter-eureka 一直無(wú)法下載,maven倉(cāng)庫(kù)中包路徑顯示為unknown,怎么解決呢?下面小編給大家?guī)?lái)了pom.xml中maven無(wú)法下載springcloud包問(wèn)題,需要的朋友可以參考下2022-08-08Java利用位運(yùn)算實(shí)現(xiàn)乘法運(yùn)算詳解
這篇文章主要為大家詳細(xì)介紹了Java如何用位運(yùn)算實(shí)現(xiàn)乘法運(yùn)算,在實(shí)現(xiàn)乘法時(shí)要用位運(yùn)算實(shí)現(xiàn),并且不能出現(xiàn)加減乘除任何符號(hào),感興趣的可以了解一下2023-04-04劍指Offer之Java算法習(xí)題精講數(shù)組與列表的查找及字符串轉(zhuǎn)換
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03SpringBoot中@ComponentScan的使用詳解
這篇文章主要介紹了SpringBoot中@ComponentScan的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11處理Log4j2不能打印行號(hào)的問(wèn)題(AsyncLogger)
這篇文章主要介紹了處理Log4j2不能打印行號(hào)的問(wèn)題(AsyncLogger),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Logger.getLogger()與LogFactory.getLog()的區(qū)別詳解
LogFactory來(lái)自common-logging包。如果用LogFactory.getLog,你可以用任何實(shí)現(xiàn)了通用日志接口的日志記錄器替換log4j,而程序不受影響2013-09-09解決@Scope(“prototype“)不生效的問(wèn)題
這篇文章主要介紹了解決@Scope(“prototype“)不生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06