Springboot 3.x 使用PageHelper实现MyBatis分页查询

发布时间 2023-08-10 20:06:30作者: Hmi1234

开发环境

SpringBoot 3.0.1
Maven 工程
JDK OpenJdk 17.0.6

引入pom依赖


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

注意: Springboot 3.x 版本必须引用1.4.6版本以上,否则无效 GitHub官方描述

设置 application.yml

#MyBatis分页工具
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

代码展示

PageQueryMoDetDTO

/**
 * 分页查询查询德育学分明细DTO
 * @Author Hmi
 * @Date 2023/7/31 8:36
 */
@Data
public class PageQueryMoDetDTO implements Serializable {
    /**
     * 学号
     */
    private String stuid;
    /**
     * 状态值   0-封禁   1-正常
     */
    private Integer status;
    /**
     * 页码
     */
    private int page;
    /**
     * 每页数据量
     */
    private  int pageSize;

    // ... 这里使用了lombok简化了get/set方法
}
MoralDetailsController
 @GetMapping("/pageQueryMoDet")
    @Operation(summary = "分页查询数据详情")
    public Result queryMoDet(PageQueryMoDetDTO pageQueryMoDetDTO) {
        log.info("分页查询数据详情:{}", pageQueryMoDetDTO);
        return moralDetailsServer.pageQueryMoDet(pageQueryMoDetDTO);

    }
MoralDetailsMapper
    List<Moraldetails> pageQueryMoDet(PageQueryMoDetDTO pageQueryMoDetDTO);
MoralDetailsMapper.xml
 <select id="pageQueryMoDet" resultType="Moraldetails">
        select * from moraldetails
        <where>
            stuid = #{stuid}
            <if test="status != null">
                and status = #{status}
            </if>
            order by create_time desc
        </where>
    </select>
MoralDetailsServer
    Result pageQueryMoDet(PageQueryMoDetDTO pageQueryMoDetDTO);
MoralDetailsServerImpl
    public Result pageQueryMoDet(PageQueryMoDetDTO pageQueryMoDetDTO) {
        PageHelper.startPage(pageQueryMoDetDTO.getPage(), pageQueryMoDetDTO.getPageSize());
        List<Moraldetails> list = moralDetailsMapper.pageQueryMoDet(pageQueryMoDetDTO);
        PageInfo<Moraldetails> pageInfo = new PageInfo<Moraldetails>(list);
        return Result.ok(pageInfo);
    }

运行调试

使用ApiFox或其他的Api工具发送请求

服务器输出运行日志

成功获取分页查询数据

{
    "code": 200,
    "success": true,
    "msg": "操作成功",
    "result": {
        "total": 984,
        "list": [
            {
                "id": 1005,
                "stuid": "215534999",
                "name": null,
                "particulars": "ceshiceshi",
                "type": 0,
                "changeValues": 100,
                "way": 3,
                "operator": "215534120",
                "status": 0,
                "createTime": "2023-06-17 19:15:19",
                "updateTime": "2023-06-23 20:27:47",
                "createUser": "",
                "updateUser": ""
            },
            {
                "id": 5,
                "stuid": "215534999",
                "name": null,
                "particulars": "Navicat Cloud could not connect                   ",
                "type": 0,
                "changeValues": 15,
                "way": 1,
                "operator": "215534120",
                "status": 0,
                "createTime": "2023-06-10 17:53:05",
                "updateTime": "2023-06-23 20:37:28",
                "createUser": "",
                "updateUser": ""
            },
            {
                "id": 7,
                "stuid": "215534999",
                "name": null,
                "particulars": "The Main Window consists of several               ",
                "type": 0,
                "changeValues": 7,
                "way": 5,
                "operator": "215534120",
                "status": 0,
                "createTime": "2023-06-10 17:53:05",
                "updateTime": "2023-06-23 20:37:05",
                "createUser": "",
                "updateUser": ""
            },
            {
                "id": 21,
                "stuid": "215534999",
                "name": null,
                "particulars": "It wasn’t raining when Noah built the ark.      ",
                "type": 1,
                "changeValues": 14,
                "way": 4,
                "operator": "215534120",
                "status": 0,
                "createTime": "2023-06-10 17:53:05",
                "updateTime": "2023-06-23 20:27:47",
                "createUser": "",
                "updateUser": ""
            },
            {
                "id": 23,
                "stuid": "215534999",
                "name": null,
                "particulars": "Success consists of going from                    ",
                "type": 1,
                "changeValues": 8,
                "way": 3,
                "operator": "215534120",
                "status": 0,
                "createTime": "2023-06-10 17:53:05",
                "updateTime": "2023-06-23 20:27:47",
                "createUser": "",
                "updateUser": ""
            }
        ],
        "pageNum": 1,
        "pageSize": 5,
        "size": 5,
        "startRow": 1,
        "endRow": 5,
        "pages": 197,
        "prePage": 0,
        "nextPage": 2,
        "isFirstPage": true,
        "isLastPage": false,
        "hasPreviousPage": false,
        "hasNextPage": true,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 8
    },
    "timestamp": 1691668424792
}