SpringBoot分页插件的实现过程

实现分页插件的具体步骤:

1.导入jar包

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


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

2.(两种)在application.yml(propreties)文件加上
第一种:yml

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

第二种:propreties

pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql

3.在controller控制层编写代码

  @GetMapping("/getAllPerson")
    public String getAllPerson(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
                               String singleName){
                               
        PageHelper.startPage(pageNum,2); //设置当前页码,每页的行数

        List<Tabulation> list = aaService.getAllPerson(singleName); //查询的集合
        PageInfo<Tabulation> pageInfo = new PageInfo<Tabulation>(list); //把查询的集合放入pageInfo返回给页面
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",singleName);
        return "index";
    }

4.编写页面

 <p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>
    <a th:href="@{/getAllPerson}">首页</a>
    <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
    <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
    <a th:href="@{/getAllPerson(pageNum=${pageInfo.pages})}">尾页</a>

5.运行
在这里插入图片描述
在这里插入图片描述

总结

使用分页插件无需创建page类,也不用在查询语句中写limit

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值