MybatisPlus IPage分页查询工具类

#MybatisPlus IPage 分页查询

项目组的common中 要求用 MybatisPlus 做分页查询 ,由于MybatisPlus 查询出来的都是entity的集合,前端要求统一返回Ipage (Result<IPage<***>>) ,历史查询用的是Pagehelper分页 修改太多,所以创建了该工具类,便于改造旧方法,以及新方法的快速对接分页查询。

工具类

java 复制代码
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjqd.bgsc.common.model.param.CommonQueryParam;

import java.util.ArrayList;
import java.util.List;

/**
 * MyBatis Plus 通用分页工具类(返回IPage<R>)
 * 优化点:页码超出总页数时,自动查询最后一页
 */
public class MyBatisPageUtils {
    /**
     * 通用分页查询方法(适配IPage<Entity>入参、List<Vo>返回,优化页码越界问题)
     * @param param 查询参数(含分页参数)
     * @param queryFunc 业务查询逻辑(入参IPage<E>,返回List<R>)
     * @param <P> 查询参数类型(继承CommonQueryParam)
     * @param <E> 分页对象的实体类型(如ReportWorkEntity)
     * @param <R> 返回列表的VO类型(如ReportWorkVo)
     * @return 分页结果(IPage<R>,兼容不分页/页码越界场景)
     */
    public static <P extends CommonQueryParam, E, R> IPage<R> pageMybatisQuery(
            P param,
            QueryFunction<P, E, R> queryFunc
    ) {
        // 1. 获取原始分页参数
        int current = param.getCurrent();
        int size = param.getSize();

        // 场景1:current和size都为0(都没传),取消分页查全部
        if (current <= 0 && size <= 0) {
            List<R> allRecords = queryFunc.apply(param, null);
            IPage<R> allPage = new Page<>(0, 0);
            allPage.setRecords(allRecords);
            allPage.setTotal(allRecords.size());
            return allPage;
        }

        // 2. 处理分页参数默认值
        int finalCurrent = current <= 0 ? 1 : current; // 传size没传current,默认第1页
        int finalSize = size <= 0 ? 10 : size;        // 传current没传size,默认每页10条

        // 3. 先查询总条数(仅查count,不查数据,避免无效数据查询)
        IPage<E> countPage = new Page<>(1, 0); // size=0时,MyBatis Plus仅查总条数
        queryFunc.apply(param, countPage);    // 执行查询,countPage的total会被填充
        long total = countPage.getTotal();

        // 4. 处理总条数为0的情况(直接返回空分页)
        if (total <= 0) {
            IPage<R> emptyPage = new Page<>(finalCurrent, finalSize);
            emptyPage.setTotal(0);
            emptyPage.setRecords(new ArrayList<>());
            return emptyPage;
        }

        // 5. 计算总页数,修正越界页码(核心优化)
        long totalPage = (total + finalSize - 1) / finalSize; // 向上取整计算总页数
        if (finalCurrent > totalPage) {
            finalCurrent = (int) totalPage; // 页码超出,自动改为最后一页
        }

        // 6. 构建修正后的Entity分页对象,查询数据
        IPage<E> entityPage = new Page<>(finalCurrent, finalSize);
        List<R> voList = queryFunc.apply(param, entityPage);

        // 7. 封装为VO类型的分页对象返回
        IPage<R> resultPage = new Page<>();
        resultPage.setCurrent(entityPage.getCurrent());
        resultPage.setSize(entityPage.getSize());
        resultPage.setTotal(entityPage.getTotal());
        resultPage.setRecords(voList);

        return resultPage;
    }

    /**
     * 分页查询函数式接口(兼容Entity/Vo分离场景)
     * @param <P> 查询参数类型(继承CommonQueryParam)
     * @param <E> 分页对象的实体类型(如ReportWorkEntity)
     * @param <R> 返回列表的VO类型(如ReportWorkVo)
     */
    @FunctionalInterface
    public interface QueryFunction<P extends CommonQueryParam, E, R> {
        /**
         * 执行业务查询逻辑
         * @param param 查询参数
         * @param page 分页对象(泛型为Entity,null表示不分页;size=0时仅查总条数)
         * @return 查询结果列表(泛型为VO)
         */
        List<R> apply(P param, IPage<E> page);
    }
}

controller 调用

java 复制代码
 @PostMapping(value = "/list")
    public Result<IPage<RVo>> selectList(@RequestBody QueryParam params) {


        IPage<RVo> objectIPage = MyBatisPageUtils.<QueryParam, REntity, RVo>pageMybatisQuery(
                params, (param, page) -> Service.selectList(param, page)
        );
        return success(objectIPage);
    }

参数说明:

MyBatisPageUtils.<QueryParam, REntity, RVo>pageMybatisQuery

QueryParam:是工具类中通用的查询参数 继承CommonQueryParam 主要包含 分页参数(current,size)。

REntity :是MybatisPlus 查询的Entity

RVo:香前端返回的 集合的对象

查询方法 只需要查询出 list entity 转为 list<vo> 返回即可,不用考虑分页参数是否传递

service 方法实现

java 复制代码
 public List<RVo> selectList(QueryParam commonQueryParam, IPage<REntity> page) {

        QueryWrapper<REntity> queryWrapper = new QueryWrapper<>();
        
        List<REntity> list = this.list(page, queryWrapper);
        List<RVo> voList = new ArrayList<>();
        for (REntity rEntity : list) {
            RVo o = new RVo();
            BeanUtil.copyProperties(rEntity, o);
          
            
            voList.add(o);
        }
        return voList;
    }
相关推荐
Chasmれ几秒前
Spring Boot 1.x(基于Spring 4)中使用Java 8实现Token
java·spring boot·spring
froginwe111 分钟前
Python 条件语句
开发语言
汤姆yu2 分钟前
2026基于springboot的在线招聘系统
java·spring boot·后端
七夜zippoe4 分钟前
Python统计分析实战:从描述统计到假设检验的完整指南
开发语言·python·统计分析·置信区间·概率分布
2601_9491465311 分钟前
Python语音通知API示例代码汇总:基于Requests库的语音接口调用实战
开发语言·python
3GPP仿真实验室17 分钟前
【Matlab源码】6G候选波形:OFDM-IM 索引调制仿真平台
开发语言·matlab
计算机学姐27 分钟前
基于SpringBoot的校园社团管理系统
java·vue.js·spring boot·后端·spring·信息可视化·推荐算法
Coder_Boy_33 分钟前
基于SpringAI的在线考试系统-企业级教育考试系统核心架构(完善版)
开发语言·人工智能·spring boot·python·架构·领域驱动
java1234_小锋35 分钟前
Java高频面试题:SpringBoot如何自定义Starter?
java·spring boot·面试
落霞的思绪35 分钟前
Spring AI Alibaba 集成 Redis 向量数据库实现 RAG 与记忆功能
java·spring·rag·springai