• 首页

  • 归档

  • 标签

  • 分类

  • 友链
M S B l o g
M S B l o g

ms

获取中...

06
19
java
总结
MyBatis-Plus

MyBatis-Plus分页查询

发表于 2021-06-19 • java 总结 框架 分页查询 • 被 5,936 人看爆

物理分页:相当于执行了limit分页语句,返回部分数据。物理分页只返回部分数据占用内存小,能够获取数据库最新的状态,实施性比较强,一般适用于数据量比较大,数据更新比较频繁的场景。

逻辑分页:一次性把全部的数据取出来,通过程序进行筛选数据。如果数据量大的情况下会消耗大量的内存,由于逻辑分页只需要读取数据库一次,不能获取数据库最新状态,实施性比较差,适用于数据量小,数据稳定的场合。

mybatis实现的分页是逻辑分页或者叫做内存不是物理分页 ,他是把符合条件的数据全部查询出来放到内存中,然后返回你需要的那部分.表中数据不多时,可以使用,速度慢一些;当数据量大时,建议使用物理分页

配置分页

新建MybatisPlusConfig配置类,配置分页插件

//Spring boot方式
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {

    // 旧版
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
    
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
    
}

分页方式

MP的Wrapper提供了两种分页查询的方式

/**
 * 根据 entity 条件,查询全部记录(并翻页)
 *
 * @param page     分页查询条件(可以为 RowBounds.DEFAULT)
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 */
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
/**
 * 根据 Wrapper 条件,查询全部记录(并翻页)
 *
 * @param page     分页查询条件
 * @param queryWrapper 实体对象封装操作类
 */
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

使用

1,使用selectPage(返回值实体类)

    @Test
    public void testPage(){
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name","衣");


        Page<CategoryEntity> page = new Page<>(1,2);

        IPage<CategoryEntity> categoryEntityIPage = categoryDao.selectPage(page, wrapper);

        System.out.println("总页数"+categoryEntityIPage.getPages());
        System.out.println("总记录数"+categoryEntityIPage.getTotal());
        List<CategoryEntity> records = categoryEntityIPage.getRecords();
        records.forEach(System.out::println);
    }

结果为:

总页数22
总记录数43
CategoryEntity(catId=9, name=服饰内衣, parentCid=0, catLevel=1, showStatus=1, sort=0, icon=null, productUnit=null, productCount=0, children=null)
CategoryEntity(catId=78, name=内衣, parentCid=9, catLevel=2, showStatus=1, sort=0, icon=null, productUnit=null, productCount=0, children=null)

2,使用selectMapPage(返回值map)

@Test
    public void testMapPage(){
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name","衣");

        IPage<Map<String, Object>> page = new Page<Map<String, Object>>(1, 2);


        IPage<Map<String,Object>> categoryEntityIPage = categoryDao.selectMapsPage(page,wrapper);


        System.out.println("总页数"+categoryEntityIPage.getPages());
        System.out.println("总记录数"+categoryEntityIPage.getTotal());
        List<Map<String,Object>> records = categoryEntityIPage.getRecords();
        records.forEach(System.out::println);
    }

结果为:

总页数22
总记录数43
{cat_level=1, parent_cid=0, product_count=0, cat_id=9, name=服饰内衣, sort=0, show_status=1}
{cat_level=2, parent_cid=9, product_count=0, cat_id=78, name=内衣, sort=0, show_status=1}

自定义分页

1,在mapper接口中,添加自定义分页接口

IPage<User> selectUserPage(Page<User> page, @Param(Constants.WRAPPER) Wrapper<User> wrapper);

2,在xml文件中书写sql

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisplus.sampleschapter1.dao.UserMapper">

    <select id="selectUserPage" resultType="com.example.mybatisplus.sampleschapter1.entity.User">
        select * from user ${ew.customSqlSegment}
    </select>
    
</mapper>
分享到:
Mybatis-Plus条件构造器condition
MyBatis-plus条件构造器Warpper
  • 文章目录
  • 站点概览
ms

MSms

⚓️HelloWorld⚓️

QQ Email RSS
看爆 Top5
  • MyBatis-Plus分页查询 5,937次看爆
  • @Autowired与@Resource的区别 4,755次看爆
  • feign远程调用及异步调用丢失请求头问题 4,526次看爆
  • spring cloud中OpenFeign整合Sentinel启动报错 4,423次看爆
  • Certbot查看证书过期时间,手动续期以及自动续期 3,302次看爆

Copyright © 2025 ms · 湘ICP备20015239号

Proudly published with Halo · Theme by fyang · 站点地图