MyBatisPlus:分页插件|乐观锁插件

1:分页插件

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

@Configuration
//扫描mapper接口
@MapperScan("com.lk.mybatisplus.mapper")
public class MyBatisPlusConfig {

    //mybatisplus自带分页插件,只需要配置
    @Bean
    public MybatisPlusInterceptor  mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
        //添加分页拦截器将数据库类型写入
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }

}

分页查询: Page<实体类> page = new Page<>(当前页,每页大小);
page对象和wrapper对象一样作为参数传入即可。

    //wrapper对象
    QueryWrapper<User> queryWrapper=new QueryWrapper<User>();
    
    //查询name字段
    queryWrapper.select("name");
    
    //page对象(current,size)
    //current当前页码  size每页显示数据
    Page<User> page=new Page<>(2,3);
   
    //调用查询且分页的方法
    userMapper.selectPage( page,queryWrapper);

生成的SQL:SELECT name FROM t_users WHERE is_deleted=0 LIMIT ?,?

查询完成后page对象携带的数据:

    List<User> records = page.getRecords();//获取当前页数据
  
    long current = page.getCurrent();//获取当前页码是第几页
    
    long size = page.getSize();//获取页码size
    
    long total = page.getTotal();//获取符合条件总记录数
    
    boolean b = page.hasPrevious();//是否有上一页
   
    boolean b1 = page.hasNext();//是否有下一页

2:自定义分页功能

接口中分页方法:

/**
 *
 * @param page   MyBatisPlus提供的分页对象,必须位于第一个参数位置
 * @param age    查询条件字段
 * @return
 */
//自定义分页查询
Page<User> selectPageVo(@Param("page") Page<User> page,@Param("age") Integer age);

Mapper的xml映射:

<!--    Page<User> selectPageVo(@Param("page") Page<User> page,@Param("age") Integer age);-->
    <select id="selectPageVo" resultType="user">
        select name from t_users where age>#{age}
    </select>

测试:

    Page<User> page=new Page<>(2,2);
    userMapper.selectPageVo(page, 15);

生成的SQL: select name from t_users where age>? LIMIT ?,?

3:乐观锁插件

1:取出记录时,获取当前version(版本号)

2:更新记录时,将获取的版本号作为更新条件,且如果更新成功版本号+1

步骤1: @Version
实体类添加@Version注解,表示版本字段

@TableField(value = "VERSION")
@Version //标识乐观锁的版本号
private Integer version;

步骤2: 在MyBatisPlus的配置类中添加乐观锁插件

@Configuration
//扫描mapper接口
@MapperScan("com.lk.mybatisplus.mapper")
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor  mybatisPlusInterceptor(){
    
        MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
        
        //添加乐观锁插件
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        
        return mybatisPlusInterceptor;
    }
}

测试:
小李,小王同时对Price(价格进行操作),小李将价格+50,小王将价格-30。

    //小李查询商品价
    //SELECT id,NAME,price,VERSION FROM t_product WHERE id=?
    Product productLi = productMapper.selectById(1);
    //小李查询的商品价格 100
    System.out.println("小李查询商品价格:"+productLi.getPrice());

    //小王查询商品价
    //SELECT id,NAME,price,VERSION FROM t_product WHERE id=?
    Product productW = productMapper.selectById(1);
    //小王查询的商品价格 100
    System.out.println("小王查询商品价格:"+productW.getPrice());

    //小李将价格加50 版本号+1
    productLi.setPrice(productLi.getPrice()+50);
    //UPDATE t_product SET NAME=?, price=?, VERSION=? WHERE id=? AND VERSION=?    
    productMapper.updateById(productLi);

    //小王将价格降30
    productW.setPrice(productW.getPrice()-30);
    //UPDATE t_product SET NAME=?, price=?, VERSION=? WHERE id=? AND VERSION=?
    //此时小李操作后版本号变为1,所以VERSION=0匹配不到数据
    productMapper.updateById(productW);

    //小王再次查询
    //SELECT id,NAME,price,VERSION FROM t_product WHERE id=?
    //查询到价格为150
    Product productWangSecond = productMapper.selectById(1);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值