Mybatis plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)

导读:本篇文章讲解 Mybatis plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

问题背景

因为mybatis plus非常的流行,虽然平常mybatis generator也够用了,但多会一个,看别人的代码就轻松一点
注意事项:

Mybatis-plus无介绍快使用,CRUD增删改查基本使用附源码(一)

Mybatis-plus无介绍快使用,自定义sql语句CRUD增删改查附源码(二)

Mybatis-plus无介绍快使用,自带封装service层的使用附源码(三)

Mybatis-plus无介绍快使用,注解的使用(四)

Mybatis-plus无介绍快使用,Wrapper条件构造器的使用附源码(五)

Mybatis-plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)

Mybatis-plus无介绍快使用,枚举变量的使用附源码(七)

Mybatis-plus无介绍快使用,多数据源的使用(八)

Mybatis-plus无介绍快使用,MybatisX自动生成代码插件的使用(九)

Mybatis-plus无介绍快使用,可继承通用的基础实体类(十)

项目搭建

1.分页插件,新增配置类

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

  • 添加配置类MyBatisPlusConfig
@Configuration
@MapperScan("com.atguigu.mybatisplus.mapper")
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

或者

    /**
     * 配置分页插件
     * @return page
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //请求页面大于最大页后操作,true回到首页,默认false
        paginationInterceptor.setOverflow(true);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
  • 编写测试方法
@Test
public void testPage(){
    //new Page()中的两个参数分别是当前页码,每页显示数量
    Page<User> page = userMapper.selectPage(new Page<>(1, 2), null);
    List<User> users = page.getRecords();
    users.forEach(System.out::println);
}

2.自定义分页

上面调用的是MyBatis-Plus提供的带有分页的方法,那么我们自己定义的方法如何实现分页呢?

  • UserMapper接口中定义一个方法
/**
  * 根据年龄查询用户列表,分页显示 
  * @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位 
  * @param age 年龄 
  * @return 
  */
Page<User> selectPageVo(@Param("page") Page<User> page,@Param("age") Integer age);
  • UserMapper.xml中编写SQL实现该方法
<select id="selectPageVo" resultType="User">
    select id,username as name,age,email from t_user where age > #{age}
</select>
  • 编写测试方法
@Test
public void testPageVo(){
    Page<User> page = userMapper.selectPageVo(new Page<User>(1,2), 20);
    List<User> users = page.getRecords();
    users.forEach(System.out::println);
}

3.乐观锁

作用:当要更新一条记录的时候,希望这条记录没有被别人更新

乐观锁的实现方式:

  • 取出记录时,获取当前 version

  • 更新时,带上这个 version

  • 执行更新时, set version = newVersion where version = oldVersion

  • 如果 version 不对,就更新失败

3.1 场景


  • 一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太高,可能会影响销量。又通知小王,你把商品价格降低30元。

  • 此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就完全被小王的覆盖了。

  • 现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1万多。

3.2 乐观锁与悲观锁


  • 上面的故事,如果是乐观锁,小王保存价格前,会检查下价格是否被人修改过了。如果被修改过了,则重新取出的被修改后的价格,150元,这样他会将120元存入数据库。

  • 如果是悲观锁,小李取出数据后,小王只能等小李操作完之后,才能对价格进行操作,也会保证最终的价格是120元。

3.3 模拟修改冲突


  • 数据库中增加商品表
CREATE TABLE t_product ( 
    id BIGINT(20) NOT NULL COMMENT '主键ID', 
    NAME VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名称', 
    price INT(11) DEFAULT 0 COMMENT '价格', 
    VERSION INT(11) DEFAULT 0 COMMENT '乐观锁版本号', 
    PRIMARY KEY (id) 
);
  • 添加一条数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
  • 添加一个实体类Product
@Data
public class Product {
    private Long id;
    private String name;
    private Integer price;
    private Integer version;
}
  • 添加一个Mapper接口ProductMapper
public interface ProductMapper extends BaseMapper<Product> {}
  • 测试方法
@Test
public void testProduct01(){
    //1.小李获取商品价格
    Product productLi = productMapper.selectById(1);
    System.out.println("小李获取的商品价格为:" + productLi.getPrice());

    //2.小王获取商品价格
    Product productWang = productMapper.selectById(1);
    System.out.println("小李获取的商品价格为:" + productWang.getPrice());

    //3.小李修改商品价格+50
    productLi.setPrice(productLi.getPrice()+50);
    productMapper.updateById(productLi);

    //4.小王修改商品价格-30
    productWang.setPrice(productWang.getPrice()-30);
    productMapper.updateById(productWang);

    //5.老板查询商品价格
    Product productBoss = productMapper.selectById(1);
    System.out.println("老板获取的商品价格为:" + productBoss.getPrice());
}
  • 执行结果

Mybatis plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)

3.4 乐观锁解决问题


  • 实体类version字段添加注解@Version
@Data
public class Product {
    private Long id;
    private String name;
    private Integer price;
    @Version
    private Integer version;
}
  • 添加乐观锁插件配置
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    //添加分页插件
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    //添加乐观锁插件
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    return interceptor;
}
  • 再次执行测试方法

    小李查询商品信息:

    SELECT id,name,price,version FROM t_product WHERE id=?

    小王查询商品信息:

    SELECT id,name,price,version FROM t_product WHERE id=?

    小李修改商品价格,自动将version+1

    UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?

    Parameters: 外星人笔记本(String), 150(Integer), 1(Integer), 1(Long), 0(Integer)

    小王修改商品价格,此时version已更新,条件不成立,修改失败

    UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?

    Parameters: 外星人笔记本(String), 70(Integer), 1(Integer), 1(Long), 0(Integer)

    最终,小王修改失败,查询价格:150

    SELECT id,name,price,version FROM t_product WHERE id=?

  • 优化执行流程

@Test
public void testProduct01(){
    //1.小李获取商品价格
    Product productLi = productMapper.selectById(1);
    System.out.println("小李获取的商品价格为:" + productLi.getPrice());

    //2.小王获取商品价格
    Product productWang = productMapper.selectById(1);
    System.out.println("小李获取的商品价格为:" + productWang.getPrice());

    //3.小李修改商品价格+50
    productLi.setPrice(productLi.getPrice()+50);
    productMapper.updateById(productLi);

    //4.小王修改商品价格-30
    productWang.setPrice(productWang.getPrice()-30);
    int result = productMapper.updateById(productWang);
    if(result == 0){
        //操作失败,重试
        Product productNew = productMapper.selectById(1);
        productNew.setPrice(productNew.getPrice()-30);
        productMapper.updateById(productNew);
    }

    //5.老板查询商品价格
    Product productBoss = productMapper.selectById(1);
    System.out.println("老板获取的商品价格为:" + productBoss.getPrice());
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q462Df8d-1654434265071)(https://upload-images.jianshu.io/upload_images/24315796-0d428134e0fb1ebf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

4 项目目录
Mybatis plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)

总结

  • 插件需要添加配置类

作为程序员第 154 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
Mybatis plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)Mybatis plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)Mybatis plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)

Lyric: 在远方决斗

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/110721.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!