物理分页:相当于执行了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>