SpringBoot使用PageHelper分页查询
前言
PageHelper分页插件,最方便使用的分页插件。分页插件支持任何复杂的单表、多表分页
官方文档:https://pagehelper.github.io/
添加依赖
<dependency>
<!--分页插件 pagehelper -->
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
配置文件
aplication.yml添加配置:
#分页pageHelper
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
·helper-dialect:
配置使用哪种数据库语言,不配置的话pageHelper也会自动检测
·reasonable:
配置分页参数合理化功能,默认是false。 #启用合理化时,如果pageNum<1会查询第一页,如果pageNum>总页数会查询最后一页; #禁用合理化时,如果pageNum<1或pageNum>总页数会返回空数据。
·params:
为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值; 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。
·support-methods-arguments:
支持通过Mapper接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。
简单使用
举个例子,订单列表数据展示
实体类–enetity
package com.han.pojo;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
*@author han
*@version 1.0
@since 2022-12-22 21:59
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Action {
private Integer actionId;
private String orderSn;
private Integer actionUser;
private Integer orderStatus;
private Integer payStatus;
private Integer shippingStatus;
private String actionNote;
private String actionTime;
private String statusDesc;
}
ActionMapper mapper层
/**
* 查询
* @return
*/
List<Action> getAll(String orderSn);
//mapper.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.han.mapper.ActionMapper">
<select id="getAll" resultType="Action" parameterType="com.han.pojo.Action">
select * from action
<if test="orderSn!= null and orderSn!='' and orderSn.length!=0">
where order_sn like concat('%',#{orderSn},'%')
</if>
</select>
业务逻辑层 ActionService层
/**
* 查询
* @return
*/
PageInfo<Action> AllPages(String orderSn,Integer pageSize,Integer pageIndex);
ActionServiceImpl层
@Override
public PageInfo<Action> AllPages(String orderSn,Integer pageSize,Integer pageIndex) {
//调用分页插件的工具类,计算总页数
PageHelper.startPage(pageIndex,pageSize);
//获取所有数据
List<Action> page=npm.getAll();
//获取所有的数据直接给pageInfo
PageInfo pageInfo=new PageInfo(page);
return pageInfo;
}
ActionContoller
PageNum当前页
PageSize每页条数
orderSn订单编号
@RequestMapping("get")
public String get(Model model,String orderSn,
@RequestParam(value ="PageNum",defaultValue = "1") Integer PageNum,
@RequestParam(value ="PageSize",defaultValue = "2",required = false) Integer PageSize){
PageHelper.startPage(PageNum,PageSize);
List<Action> list = service.AllPages(orderSn);
PageInfo<Action> pageInfo = new PageInfo<>(list);
if (orderSn!=null){
model.addAttribute("orderSn",orderSn);
}
model.addAttribute("pageInfo",pageInfo);
System.out.println(orderSn);
return "index";
}
list.html
实现页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div align="center">
<form method="post" th:action="${get}">
订单编号 <input type="text" name="orderSn"> <input type="submit" value="查询">
</form>
</div>
<table border="1" align="center">
<tr>
<th>操作id</th>
<th>订单编号</th>
<th>操作人</th>
<th>订单状态</th>
<th>支付状态</th>
<th>配送状态</th>
<th>操作记录</th>
<th>操作时间</th>
<th>操作描述</th>
</tr>
<tr th:each="List:${pageInfo.list}">
<td th:text="${List.actionId}"></td>
<td th:text="${List.orderSn}"></td>
<td th:text="${List.actionUser}"></td>
<td th:text="${List.orderStatus}"></td>
<td th:text="${List.payStatus}"></td>
<td th:text="${List.shippingStatus}"></td>
<td th:text="${List.actionNote}"></td>
<td th:text="${List.actionTime}"></td>
<td th:text="${List.statusDesc}"></td>
</tr>
<div th:if="${pageInfo.getPages>0}" align="center">
<!--当前页-->
<span th:text="'当前页:'+${pageInfo.getPageNum}"></span>
<!-- 首页作为第一页不可点击-->
<a th:href="@{get(PageNum=1)}+'&orderSn='+${orderSn}">首页</a>
<span th:if="${pageInfo.PageNum==1}">
<a onclick="return false">上一页</a>
</span>
<span th:unless="${pageInfo.PageNum==1}">
<a th:href="@{get(PageNum=${pageInfo.PageNum-1})}+'&orderSn='+${orderSn}">上一页</a>
</span>
<span th:each="i:${#numbers.sequence(1,pageInfo.getPages)}">
<a th:href="@{get(PageNum=${i})}" th:text="${i}">1</a>
</span>
<span th:if="${pageInfo.getPages==pageInfo.getPageNum}">
<a onclick="return false">下一页</a>
</span>
<span th:unless="${pageInfo.getPages==pageInfo.getPageNum}">
<a th:href="@{get(PageNum=${pageInfo.PageNum+1})}+'&orderSn='+${orderSn}">下一页</a>
</span>
<a th:href="@{get(PageNum=${pageInfo.getPages})}+'&orderSn='+${orderSn}">末页</a>
<span th:text="'总条数'+${pageInfo.getTotal}"></span>
</div>
</table>
</body>
</html>