mybatis使用Pagehelper分页

1.在pom文件中引入Pagehelper分页插件

 
  1. <!-- 分页插件 -->

  2. <dependency>

  3. <groupId>com.github.pagehelper</groupId>

  4. <artifactId>pagehelper-spring-boot-starter</artifactId>

  5. <version>1.2.5</version>

  6. </dependency>

2..配置分页插件

打开application.properties,添加如下几行配置信息

 
  1. #properties分页插件

  2. pagehelper.helper-dialect=mysql

  3. pagehelper.params=count=countSql

  4. pagehelper.reasonable=true

  5. pagehelper.support-methods-arguments=true

 

 

  1. #.yml分页插件

  2. #pagehelper分页配置 第二种和第三种不需要 重点讲的第一种需要
    pagehelper:
        helperDialect: mysql
        reasonable: true
        supportMethodsArguments: true
        params: count=countSql

至此,pagehelper分页插件整合完毕,下面开始测试

3.测试

测试之前,我们先给上次创建的Person表增加几条数据

然后修改PersonController的代码

 
  1. @Controller

  2. public class PersonController {

  3. @Autowired

  4. private PersonService personService;

  5.  
  6. @GetMapping("/getAllPerson")

  7. public String getAllPerson(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){

  8. PageHelper.startPage(pageNum,5);

  9. List<Person> list = personService.getAllPerson();

  10. PageInfo<Person> pageInfo = new PageInfo<Person>(list);

  11. model.addAttribute("pageInfo",pageInfo);

  12. return "list";

  13. }

  14. }

其中:PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示5条数据。PageInfo用来封装页面信息,返回给前台界面。PageInfo中的一些我们需要用到的参数如下表:

PageInfo.list结果集
PageInfo.pageNum当前页码
PageInfo.pageSize当前页面显示的数据条目
PageInfo.pages总页数
PageInfo.total数据的总条目数
PageInfo.prePage上一页
PageInfo.nextPage下一页
PageInfo.isFirstPage是否为第一页
PageInfo.isLastPage是否为最后一页
PageInfo.hasPreviousPage是否有上一页
PageHelper.hasNextPage是否有下一页

下面处理返回到list界面的数据信息,依然使用thymeleaf模版,代码如下

 
  1. <!DOCTYPE html>

  2. <html xmlns:th="http://www.thymeleaf.org">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>Title</title>

  6. </head>

  7. <body>

  8. <div align="center">

  9. <table border="1">

  10. <tr>

  11. <th>id</th>

  12. <th>name</th>

  13. <th>sex</th>

  14. <th>age</th>

  15. </tr>

  16. <tr th:each="person:${pageInfo.list}">

  17. <td th:text="${person.id}"></td>

  18. <td th:text="${person.name}"></td>

  19. <td th:text="${person.sex}"></td>

  20. <td th:text="${person.age}"></td>

  21. </tr>

  22. </table>

  23. <p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>

  24. <a th:href="@{/getAllPerson}">首页</a>

  25. <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>

  26. <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>

  27. <a th:href="@{/getAllPerson(pageNum=${pageInfo.pages})}">尾页</a>

  28. </div>

  29. </body>

  30. </html>

打开浏览器查看效果

4.扩展

在springboot的项目中,如果我们修改了前台界面,会发现刷新界面后没有效果,因此需要重启项目,这样很麻烦,简单两步教你搞定它:

(1)在配置文件中给thymeleaf添加如下配置:

spring.thymeleaf.cache=false

(2)在Intellij Idea按Ctrl+Shift+F9

之后在浏览器中刷新界面,即可显示出对页面的更改信息。

5.使用jpa分页如另外一篇博客

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

only-qi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值