# 4.MyBatis-Plus-插件

# 1.分页插件

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

配置

@Configuration
public class mpConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}
1
2
3
4
5
6
7
8
9

测试

@Test
public void testPage(){
    // 设置分页参数
    Page<User> page = new Page<>(1, 5);
    userMapper.selectPage(page, null);
    // 获取分页数据
    List<User> list = page.getRecords();
    list.forEach(System.out::println);
    System.out.println("当前页:"+page.getCurrent());
    System.out.println("每页显示的条数:"+page.getSize());
    System.out.println("总记录数:"+page.getTotal());
    System.out.println("总页数:"+page.getPages());
    System.out.println("是否有上一页:"+page.hasPrevious());
    System.out.println("是否有下一页:"+page.hasNext());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 2.自定义分页

接口方法

IPage<User> selectPageVo(@Param("page") IPage<User> page,@Param("age") Integer age);
1

xml

<mapper namespace="com.demo.mp.mapper.UserMapper">
    <select id="selectPageVo" resultType="User">
        select * from t_user where age > #{age}
    </select>
</mapper>
1
2
3
4
5

测试

    @Test
    public void test2(){
        Page<User> page = new Page<>(1,5);
        userMapper.selectPageVo(page,18);
        // 获取分页数据
        List<User> list = page.getRecords();
        list.forEach(System.out::println);
        System.out.println("当前页:"+page.getCurrent());
        System.out.println("每页显示的条数:"+page.getSize());
        System.out.println("总记录数:"+page.getTotal());
        System.out.println("总页数:"+page.getPages());
        System.out.println("是否有上一页:"+page.hasPrevious());
        System.out.println("是否有下一页:"+page.hasNext());
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 3.乐观锁

# 1.场景

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

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

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

# 2.乐观锁与悲观锁

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

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

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

实体

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

mapper

public interface ProductMapper extends BaseMapper<Product> {

}
1
2
3

test

    @Test
    public void testConcurrentUpdate(){
        Product p1 = productMapper.selectById(1L);
        System.out.println("小李取出的价格:" + p1.getPrice());
        Product p2 = productMapper.selectById(1L);
        System.out.println("小王取出的价格:" + p2.getPrice());
        p1.setPrice(p1.getPrice()+50);
        productMapper.updateById(p1);
        System.out.println("小李更新后的价格:" + p1.getPrice());
        p2.setPrice(p2.getPrice()-30);
        productMapper.updateById(p2);
        System.out.println("小王更新后的价格:" + p2.getPrice());
        Product boss = productMapper.selectById(1L);
        System.out.println("老板查询的价格:" + boss.getPrice());
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

li更新后150,wang更新后70,老板查看价格70

# 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

# 5、Mybatis-Plus实现乐观锁

修改实体类

@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

添加乐观锁插件配置

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 添加乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
1
2
3
4
5
6
7
8

将数据改回100,测试可以看到最后结果为150,小王的版本号没有匹配成功,所以未能成功覆盖到小李修改的数据

优化后流程

    @Test
    public void testConcurrentUpdate(){
        Product p1 = productMapper.selectById(1L);
        System.out.println("小李取出的价格:" + p1.getPrice());
        Product p2 = productMapper.selectById(1L);
        System.out.println("小王取出的价格:" + p2.getPrice());
        p1.setPrice(p1.getPrice()+50);
        productMapper.updateById(p1);
        System.out.println("小李更新后的价格:" + p1.getPrice());
        p2.setPrice(p2.getPrice()-30);
        int i = productMapper.updateById(p2);
        if(i==0){
            p2=productMapper.selectById(1L);
            p2.setPrice(p2.getPrice()-30);
            productMapper.updateById(p2);
            System.out.println("小王重试后的价格:" + p2.getPrice());
        }
        Product boss = productMapper.selectById(1L);
        System.out.println("老板查询的价格:" + boss.getPrice());
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

老板成功看到理想价格120

最近更新: 9/19/2026, 1:27:08 PM
编程NOTE   |