首页 > 其他分享 >Mybatis-Plus 插件——乐观锁

Mybatis-Plus 插件——乐观锁

时间:2023-05-13 17:11:38浏览次数:28  
标签:插件 priceWang priceLi Product 商品价格 productWang Plus productMapper Mybatis

模拟问题场景

场景:

1.商品原先价格100
2.boss通知小李将商品价格加50
3.boss觉得加到150,价格太高,通知小王降价30
4.小李和小王同时查看商品价格为100,小李将价格改为150,小王将价格改为70
5.最终结果商品价格为70,而boss实际想设定的值是120

代码模拟问题:

 @Test
    public void testOptimisticLock() {
        //小李查询商品价格
        Product productLi = productMapper.selectById(1);
        Integer priceLi = productLi.getPrice();
        System.out.println("小李查询商品价格:" + priceLi);

        //小王查询商品价格
        Product productWang = productMapper.selectById(1);
        Integer priceWang = productWang.getPrice();
        System.out.println("小王查询商品价格:" + priceWang);

        //小李涨价50
        priceLi = priceLi + 50;
        productLi.setPrice(priceLi);
        productMapper.updateById(productLi);

        //小王降价30
        priceWang = priceWang - 30;
        productWang.setPrice(priceWang);
        productMapper.updateById(productWang);

        //最终商品价格
        Product product = productMapper.selectById(1);
        System.out.println("最终商品价格:" + product);

    }

结果:

最终商品价格:Product(id=1, name=外星人笔记本, price=70, version=0)

乐观锁解决问题

使用乐观锁中解决上述问题。

乐观锁实现方式:

  • 取出记录时,获取当前 version
  • 更新时,带上这个 version
  • 执行更新时, set version = newVersion where version = oldVersion
  • 如果 version 不对,就更新失败

Mybatis-Plus 在使用乐观锁插件时,执行更新操作时会判断当前版本号是否和数据库一致,一致就执行更新操作,不一致的话可以重试(再查询一下数据,然后执行更新操作)

Mybatis-Plus 使用乐观锁:

  1. 配置类添加乐观锁插件
  2. 实体类版本号字段添加@Version注解

配置类添加乐观锁插件

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    return interceptor;
}

实体类版本号字段添加@Version

@Data
public class Product {
    private Long id;
    private String name;
    private  Integer price;
    @Version
    private Integer version;
}

测试

 /**
     * 乐观锁解决问题
     */
    @Test
    public void testOptimisticLockPlus() {
        //小李查询商品价格
        Product productLi = productMapper.selectById(1);
        Integer priceLi = productLi.getPrice();
        System.out.println("小李查询商品价格:" + priceLi);

        //小王查询商品价格
        Product productWang = productMapper.selectById(1);
        Integer priceWang = productWang.getPrice();
        System.out.println("小王查询商品价格:" + priceWang);

        //小李涨价50
        priceLi = priceLi + 50;
        productLi.setPrice(priceLi);
        productMapper.updateById(productLi);

        //小王降价30
        priceWang = priceWang - 30;
        productWang.setPrice(priceWang);
        int i = productMapper.updateById(productWang);
        if(i == 0) {
            //重试
            Product productWa = productMapper.selectById(1);
            productWa.setPrice(productWa.getPrice() - 30);
            productMapper.updateById(productWang);
        }

        //最终商品价格
        Product product = productMapper.selectById(1);
        System.out.println("最终商品价格:" + product);
    }

结果:

最终商品价格:Product(id=1, name=外星人笔记本, price=70, version=2)

标签:插件,priceWang,priceLi,Product,商品价格,productWang,Plus,productMapper,Mybatis
From: https://www.cnblogs.com/1963942081zzx/p/17397718.html

相关文章

  • MyBatis-Plus 插件——分页插件
    添加配置类@ConfigurationpublicclassMybatisPlusConfig{@BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){MybatisPlusInterceptorinterceptor=newMybatisPlusInterceptor();//添加分页插件interceptor.addInnerIn......
  • MyBatis Plus 解决大数据量查询慢问题
    常规查询流式查询游标查询大数据量操作的场景大致如下:数据迁移数据导出批量处理数据在实际工作中当指定查询数据过大时,我们一般使用分页查询的方式一页一页的将数据放到内存处理。但有些情况不需要分页的方式查询数据或分很大一页查询数据时,如果一下子将数据全部加载出......
  • wordpress插件:用Hide Page And Post Title插件隐藏页面标题(wordpress 6.2)
    一,安装插件:安装完成后点击启用按钮启用后如图:二,隐藏页面标题效果:说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest     对应的源码可以访问这里获取: https://github.com/liuhongdi/     或: https://g......
  • Element-plus中使用pagination显示sizes为中文
    需求描述使用分页组件中添加sizes信息,也就是一个下拉框,用户可以选择当前页的数据量<el-paginationbackgroundv-model:current-page="currentPage"v-model:page-size="pageSize":total="total":page-sizes="[5,10,15,20,25]"@current-change="......
  • SpringBoot整合Mybatis
    SpringBoot整合MyBatisSpringBoot整合MyBatisSpringBoot是一个快速开发应用程序的框架,而MyBatis是一个提供ORM支持的优秀框架。在本文中,我们将学习如何将SpringBoot与MyBatis整合,以便我们能够更加轻松地开发Web应用程序。步骤创建新的SpringBoot项目。在pom.xml文件中添加My......
  • 小白学前端--001 VSCode安装+插件+字号调节
    工于利其事必先利其器,学前端工具选择很重要。一、VSCode是不错的选择。安装步骤不再赘述,一路下一步即可。vscode折叠左侧窗口,ctrl+B二、插架选择1、OpeninBrowser  (在代码中右键选择,在默认浏览器查看代码执行效果)2、Chinese(Simplified)(简体中文)......
  • PostgreSQL插件(1): pg_timeout及pg_timetable 及 若干FAQ(1)
           PostgreSQL插件(1):pg_timeout及pg_timetable及若干FAQ(1)PostgreSQL2023-05-1209:20发表于河北编者荐语:报考PG数据库专家上盘古云课堂以下文章来源于数据库杂记,作者SeanHe1、前言这次将简单介绍PG的两个插件,以及若干常见问题汇集,作......
  • IDEA安装离线插件
    一、下载离线插件1.下载地址IDEA官方插件下载地址:https://plugins.jetbrains.com/2.下载离线插件输入插件名称,进行搜索。选择需要下载的插件。点击"Versions",根据IDEA的版本下载对应的版本插件,否则会安装失败。点击"Download",即可进行下载。二、安装离线插件选择下载......
  • 介绍两款WordPress文章转移插件
    1.InlinePosts 如果想让你的子页面像首页一样出现文章列表 Wordpressletsyoucreatepagesthatcontainstaticcontent.TheTalksandAboutpagesonthisblogaretwoexamples.Unfortunately,untilnowyoucouldnotincludepostsinsideofthesestaticpages.......
  • Gdiplus::Graphics::DrawString绘制文字
     Gdiplus::FontFamilyfontfm(_T("微软雅黑"));Gdiplus::Fontfont(&fontfm,24,Gdiplus::FontStyleBold,Gdiplus::UnitPixel);/*参数1:FontFamilyfamily字体,如我们常见的“宋体”、“仿宋”、“微软雅黑”、“Arial”等参数2:floatemSize......