SpringBoot使用PageHelper分页查询

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

导入依赖

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
</dependency>

后去做相关配置

pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=true
spring.main.allow-circular-references=true

Action.java

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

/**
     * 查询
     * @return
     */
    List<Action> get(String orderSn);

 ActionService与ActionMapper方法一致

ActionServiceImpl

@Override
    public List<Action> get(String orderSn) {
        return actionMapper.get(orderSn);
    }

ActionMapper.xml

<?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="get" 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>

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.get(orderSn);
        PageInfo<Action> pageInfo = new PageInfo<>(list);
        if (orderSn!=null){
            model.addAttribute("orderSn",orderSn);
        }
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(orderSn);
        return "index";
    }

index.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>
        &nbsp;&nbsp;
        <!--            首页作为第一页不可点击-->
        <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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值