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;
    }
相关推荐
松仔log24 分钟前
JetPack——Paging3+Room
android·java·zoom
biter down5 小时前
14:pytest-order 插件 顺序控制案例
开发语言·python·pytest
郝学胜-神的一滴5 小时前
Qt 高级开发 009: C++ Lambda 表达式
开发语言·c++·qt·软件构建
星栈独行5 小时前
我在 Rust 全栈项目里用 JWT 做无状态认证
开发语言·后端·rust·前端框架·开源·github·web
Lei活在当下6 小时前
先用起来,再理解,关于协程Coroutine应该知道的事
android·java·jvm
石山代码6 小时前
C++ 轻量级日志系统
开发语言·c++
Java爱好狂.6 小时前
Java程序员体系化学习路线(2026最新版)
java·后端·java面试·java架构师·java程序员·java八股文·java学习路线
tongluowan0076 小时前
以ReentrantLock为例解释AQS的工作流程
java·模板方法模式·aqs·reentrantlock
小技与小术6 小时前
玩转Flask
开发语言·python·flask
SilentSamsara7 小时前
Python 性能优化:tracemalloc、profiling 与 C 扩展加速
开发语言·python·青少年编程·性能优化