• MyBatis-Plus学习笔记(Spring版)——(六)MyBatis-Plus分页插件与乐观锁


    06、插件

    6.1、分页插件

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

    6.1.1、添加配置

    <bean class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    	<property name="configLocation" value="classpath:mybatis-config.xml">property>
    	<property name="dataSource" ref="dataSource">property>
    	<property name="typeAliasesPackage" value="com.atguigu.mp.pojo">property>
    	<property name="globalConfig" ref="globalConfig">property>
    	
    	<property name="plugins">
    		<array>
    			<ref bean="mybatisPlusInterceptor">ref>
    		array>
    	property>
    bean>
    
    
    <bean id="mybatisPlusInterceptor" class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
    	<property name="interceptors">
    		<list>
    			<ref bean="paginationInnerInterceptor">ref>
    		list>
    	property>
    bean>
    
    
    <bean id="paginationInnerInterceptor" class="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor">
    	
    	<property name="dbType" value="MYSQL">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    6.1.2、测试

    @Test
    public void testPage() {
        Page<User> page = new Page<>(2,3);
        userMapper.selectPage(page,null);
        //当前页
        System.out.println(page.getCurrent());
        //分页数据
        System.out.println(page.getRecords());
        //总页数
        System.out.println(page.getPages());
        //总记录数
        System.out.println(page.getTotal());
        //是否有下一页
        System.out.println(page.hasNext());
        //是否有上一页
        System.out.println(page.hasPrevious());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    6.2、xml自定义分页

    1、UserMapper中定义接口方法

    public interface UserMapper extends BaseMapper<User> {
        /**
         * 根据年龄进行查询用户信息,并分页
         * @param page MyBatis-Plus所提供的的分页对象,必须位于第一个参数的位置
         * @param age
         * @return
         */
        Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、UserMapper.xml中编写SQL

    
    <select id="selectPageVo" resultType="user">
        select uid,user_name,age,email from t_user where age>#{age}
    select>
    
    • 1
    • 2
    • 3
    • 4

    3、测试

    @Test
    public void testPageVo() {
        Page<User> page = new Page<>(1,3);
        userMapper.selectPageVo(page, 18);
        //当前页
        System.out.println(page.getCurrent());
        //分页数据
        System.out.println(page.getRecords());
        //总页数
        System.out.println(page.getPages());
        //总记录数
        System.out.println(page.getTotal());
        //是否有下一页
        System.out.println(page.hasNext());
        //是否有上一页
        System.out.println(page.hasPrevious());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    6.3、乐观锁

    6.3.1、场景

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

    此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据 库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就完全被小王的覆盖了。 现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1万多。

    6.3.2、乐观锁与悲观锁

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

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

    6.3.3、模拟修改冲突

    1、数据库中增加商品表

    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)
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2、添加数据

    INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
    
    • 1

    3、创建实体类

    import lombok.Data;
    
    @Data
    public class Product {
        private Long id;
        private String name;
        private Integer price;
        private Integer version;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4、添加mapper接口

    public interface ProductMapper extends BaseMapper<Product> {
    }
    
    • 1
    • 2

    5、测试

    @Test
    public void testProduct01() {
        //小李查询商品价格
        Product productLi = productMapper.selectById(1);
        System.out.println("小李查询的商品价格:" + productLi.getPrice());
        //小王查询商品价格
        Product productWang = productMapper.selectById(1);
        System.out.println("小王查询的商品价格:" + productWang.getPrice());
    
        //小李将商品价格+50
        productLi.setPrice(productLi.getPrice() + 50);
        productMapper.updateById(productLi);
        //小王将商品价格-30
        productWang.setPrice(productWang.getPrice()-30);
        productMapper.updateById(productWang);
    
        //老板查询商品价格
        Product productBoss = productMapper.selectById(1);
        System.out.println("老板查询的商品价格:" + productBoss.getPrice());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    结果:

    小李查询的商品价格:100

    小王查询的商品价格:100

    老板查询的商品价格:70

    6.3.4、乐观锁实现流程

    数据库中添加version字段

    取出记录时,获取当前version

    SELECT id,`name`,price,`version` FROM product WHERE id=1
    
    • 1

    更新时,version + 1,如果where语句中的version版本不对,则更新失败

    UPDATE product SET price=price+50, `version`=`version` + 1 WHERE id=1 AND `version`=1
    
    • 1

    6.3.5、MyBatis-Plus实现乐观锁

    1、修改实体类 --> 添加@version

    import lombok.Data;
    
    @Data
    public class Product {
        private Long id;
        private String name;
        private Integer price;
        @Version//标识乐观锁的版本号字段
        private Integer version;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、添加乐观锁插件配置

    
    <bean id="mybatisPlusInterceptor" class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
    	<property name="interceptors">
    		<list>
    			<ref bean="paginationInnerInterceptor">ref>
    			<ref bean="optimisticLockerInnerInterceptor">ref>
    		list>
    	property>
    bean>
    
    <bean id="optimisticLockerInnerInterceptor" class="com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor">bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3、测试修改冲突

    小李查询商品信息:

    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=?

    4、优化流程

    @Test
    public void testProduct01() {
        //小李查询商品价格
        Product productLi = productMapper.selectById(1);
        System.out.println("小李查询的商品价格:" + productLi.getPrice());
        //小王查询商品价格
        Product productWang = productMapper.selectById(1);
        System.out.println("小王查询的商品价格:" + productWang.getPrice());
    
        //小李将商品价格+50
        productLi.setPrice(productLi.getPrice() + 50);
        productMapper.updateById(productLi);
        //小王将商品价格-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);
        }
    
        //老板查询商品价格
        Product productBoss = productMapper.selectById(1);
        System.out.println("老板查询的商品价格:" + productBoss.getPrice());
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

  • 相关阅读:
    12.HTTPS
    网络安全(黑客)自学
    http协议连接方式 —— 短连接和长连接(请求头Connection属性)
    【云原生】SpringCloud系列之服务治理Eureka
    可视化工具Datart踩(避)坑指南(5)——先清空回收再删除
    RichView RichEdit SRichViewEdit PageSize 页面设置与同步
    Java数据结构算法:算法的空间复杂度分析
    为什么学了PMBOK,考了PMP证书还是管不好项目?
    在k8s中使用secret存储敏感数据与四种用法
    Linux chmod命令使用介绍
  • 原文地址:https://blog.csdn.net/kuaixiao0217/article/details/126003179