SpringBoot 如何增强PageHelper入参的健壮性

PageHelper.startPage(int pageNum, int pageSize, boolean count) 参数为外部输入,故存在异常输入场景。比如 pageNum 和 pageSize 输入的值 负数 或者 0,所以引入PageUtils来对入参进行判断矫正,从而避免引入异常。

第1步:支持配置的方式来修改默认值

yaml 复制代码
page-helper:
  default-page-num: 1
  default-page-size: 10
  max-page-size: 50
  count-total-or-not: true

第2步:引入PageUtils

java 复制代码
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class PageUtils {
    private static int defaultPageNumber; // 默认显示第几页
    private static int defaultPageSize; // 默认每页显示多少条数据
    private static int maxSizePage; // 每页显示条数上限
    private static boolean isQueryTotalCount; // 每次查询DB时,是否进行count查询

    @Value("${page-helper.default-page-num:1}")
    private int pageNumberFromConfig;
    @Value("${page-helper.default-page-size:10}")
    private int pageSizeFromConfig;
    @Value("${page-helper.max-page-size:15}")
    private int maxSizePerPageFromConfig;
    @Value("${page-helper.count-total-or-not:true}")
    private boolean isQueryTotalFromConfig;

    @PostConstruct
    private void init() {
        defaultPageNumber = pageNumberFromConfig;
        defaultPageSize = pageSizeFromConfig;
        maxSizePage = maxSizePerPageFromConfig;
        isQueryTotalCount = isQueryTotalFromConfig;
    }

    public static int getPageNum(Integer pageNum) {
        if (Objects.isNull(pageNum) || pageNum <= 0) {
            return defaultPageNumber;
        } else {
            return pageNum;
        }
    }

    public static int getPageSize(Integer pageSize) {
        if (Objects.isNull(pageSize) || pageSize <= 0) {
            return defaultPageSize;
        } else if (pageSize > 100) {
            return maxSizePage;
        } else {
            return pageSize;
        }
    }

    public static boolean isQueryTotalCount() {
        return isQueryTotalCount;
    }
}

第3步:使用

java 复制代码
public List<Student> listStudents(Integer pageNum, Integer PageSize) {
    PageHelper.startPage(PageUtils.getPageNum(pageNum), PageUtils.getPageSize(PageSize), PageUtils.isQueryTotalCount());
    PageHelper.orderBy("age asc");

    List<Student> students = userMapper.listStudents();

    PageInfo<Student> studentPageInfo = PageInfo.of(students);
    return students;
}

参考

@PostConstruct 的执行时机

相关推荐
北冥有鱼2 分钟前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger38 分钟前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
Sailing1 小时前
🔥🔥sql: waiting for connection 踩坑:不要一味调大 MaxOpenConns
后端·go·gin
一条Go3 小时前
ent + PostgreSQL:部分唯一索引怎么表达「全表最多一个」
后端·github
陈随易4 小时前
MoonBit 2026 深圳线下交流分享
前端·后端·程序员
玖米GEO运营王总监4 小时前
抖音和视频号算法区别:玖米快跑的双端分发信号与内容适配对比
java·前端·人工智能
HarmonSir5 小时前
Octop 架构解析(外番篇):两种多 Agent 协作,Octop 群聊房间 vs dsh 工单授权
后端·架构
tong_li5 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(七):给工具循环上保险
后端·langchain
AI人工智能+电脑小能手5 小时前
集合按大小切片分批,这个Java工具类5个方法含基本类型数组
java·工具类·批量处理·集合分片·分批调用
bai2735 小时前
13 个文件名里 4 个打不开:Node 能建、cmd 和 PowerShell 却读不了
javascript·后端