Java可以通过Request来获取公共请求参数,如pageNo,pageSize这种查询参数,这样可以统一在参数中获取PageHelper所需要的参数来完成分页操作
依赖

    <!-- pagehelper 分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
    </dependency>

代码

package com.modules.api.utils;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一的page分页utils
 */
public class PageUtils {



    /**
     * 当前记录起始索引
     */
    private static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    private static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    private static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    private static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    private static final String REASONABLE = "reasonable";
    private static final String trueString = "true";

    /** 空字符串 */
    private static final String NULLSTR = "";

    /** 下划线 */
    private static final char SEPARATOR = '_';


    /**
     * 设置请求分页数据
     */
    public static void startPage()
    {
        PageHelper.startPage(toInt(getParameter(PAGE_NUM),1), //起始页
                             toInt(getParameter(PAGE_SIZE),10),//每页大小
                toUnderScoreCase(getParameter(ORDER_BY_COLUMN))+" "+getParameter(IS_ASC)) //排序字段加排序规则
                .setReasonable(trueString.equals(getParameter(REASONABLE))); //默认为true
    }
    

    /**
     * 驼峰转下划线命名
     */
    public static String toUnderScoreCase(String str)
    {
        if (str == null)
        {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase = true;
        // 当前字符是否大写
        boolean curreCharIsUpperCase = true;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            if (i > 0)
            {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            }
            else
            {
                preCharIsUpperCase = false;
            }

            curreCharIsUpperCase = Character.isUpperCase(c);

            if (i < (str.length() - 1))
            {
                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
            }

            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase)
            {
                sb.append(SEPARATOR);
            }
            else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase)
            {
                sb.append(SEPARATOR);
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    /**
     * 转换为int<br>
     * 如果给定的值为空,或者转换失败,返回默认值<br>
     * 转换失败不会报错
     *
     * @param value 被转换的值
     * @param defaultValue 转换错误时的默认值
     * @return 结果
     */
    private static Integer toInt(Object value, Integer defaultValue)
    {
        if (value == null)
        {
            return defaultValue;
        }
        if (value instanceof Integer)
        {
            return (Integer) value;
        }
        if (value instanceof Number)
        {
            return ((Number) value).intValue();
        }
        final String valueStr = value.toString();
        if (StringUtils.isEmpty(valueStr))
        {
            return defaultValue;
        }
        try
        {
            return Integer.parseInt(valueStr.trim());
        }
        catch (Exception e)
        {
            return defaultValue;
        }
    }



    /**
     * 获取String参数
     */
    private static String getParameter(String name)
    {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        return request.getParameter(name);
    }
}

4 在需要分页的地方调用 PageUtils.startPage(); 即可

         PageUtils.startPage();
        List<xxxx> list = xxxx.list();
        PageInfo<xxxx> pageInfo = new PageInfo<>(list);

url:

/list?pageNum=1&pageSize=10?isAsc=desc?orderByColumn=orderId?reasonable=true

不语
4 声望1 粉丝