最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-Plus樂觀鎖插件的用法小結(jié)

 更新時間:2022年08月23日 10:29:58   作者:IT利刃出鞘  
樂觀鎖很樂觀,對任何事情都保持著一個樂觀的態(tài)度,認為別人不會修改數(shù)據(jù),所以不會上鎖,只是在更新數(shù)據(jù)的時候,去判斷這條數(shù)據(jù)有沒有被別人修改過,這篇文章主要介紹了MyBatis-Plus樂觀鎖插件的用法,需要的朋友可以參考下

什么是樂觀鎖:

就是我們每一次操作數(shù)據(jù)后,我們就會更改他的版本號,當另外的線程若想要對該數(shù)據(jù)進行操作,檢查版本號是否與自己獲得的版本號一致,如果不一致,那么我們就會取消該操作。

簡介

說明

本文介紹Mybatis-Plus的樂觀鎖插件的用法。

官網(wǎng)網(wǎng)址

樂觀鎖插件 | MyBatis-Plus

配置樂觀鎖插件

@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 不能復用!!!

測試

MP會把設(shè)置進去的版本號當作更新條件,并且版本號+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)文章

最新評論

云安县| 西乌珠穆沁旗| 武鸣县| 黔江区| 沂南县| 灯塔市| 济宁市| 外汇| 泸溪县| 西安市| 泰和县| 紫金县| 兴仁县| 余庆县| 堆龙德庆县| 穆棱市| 新郑市| 奉贤区| 二连浩特市| 朝阳市| 安平县| 延长县| 永丰县| 衡水市| 东莞市| 崇明县| 高陵县| 嵊州市| 镇巴县| 九台市| 合水县| 师宗县| 万源市| 新民市| 微山县| 晴隆县| 滨州市| 洪雅县| 内丘县| 濮阳市| 凤凰县|