您的位置:首页 > 其它

【学习记录】PageHelper分页插件的使用

2020-06-28 05:08 991 查看

框架使用的SSM

spring版本:5.2.5.RELEASE
mybatis版本:3.5.3
模板 Thymeleaf:3.0.11.RELEASE

依赖

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version>
</dependency>

mabatis.xm配置

<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 我这里没有用到自己设置的参数-->
</plugin>
</plugins>

Java示例

@RequestMapping("/api")
//page为传过来的参数,defaultValue 设置其默认值为1
public String getAIPs(@RequestParam(value = "page",defaultValue = "1") Integer page, Model model) {
//写在mapper之前,page是开始页码,6是每页展示的数据条数
PageHelper.startPage(page, 6);
List<API> apis = apiService.queryAPIs();
//这里的3是分页数,前端会用到
PageInfo<API> pageInfo = new PageInfo<>(apis,3);
//pageInfo.getList() 返回的是 List<API>,所以我们在前端使用对象时用pageInfo.list获取,具体看下面HTML
model.addAttribute("pageInfo",pageInfo);
return "API/api";
}

HTML–模板使用的是Thymeleaf

<table class="table table-hover">
<thead>
<tr>
<th>接口</th>
<th>描述</th>
<th>权限</th>
<th>编辑</th>
</tr>
</thead>
<tbody id="api_tbody">
<tr th:id="api_tr_+${api.id}" th:each="api :${pageInfo.list}">
<td th:text="${api.api}" class="text-success"></td>
<td th:text="${api.description}"></td>
<td th:text="${api.roleId}"></td>
<td>
<a class="btn btn-success" th:href="@{/apiUpdate/}+${api.id}">修改</a>
<span class="btn btn-danger api_del" data-toggle="modal" data-target="#myModal" th:id="${api.id}">删除</span>
</td>
</tr>
<div class="bottom mx-auto col-12 text-center">
<ul class="pagination mx-auto col-4">
<li class="page-item">
//首页的页码就是1,所有直接来判断是不是1
<a class="page-link" th:href="@{'/api?page=1'}">首页</a>
</li>
//如果当前页码是1,那么prePage的值就是0,这里判断prePage是不是0,如果是加上class:disabled禁用
<li class="page-item" th:classappend="${pageInfo.prePage == 0}?'disabled':''">
//prePage当前页码的上一页
<a class="page-link" th:href="@{'/api?page='+${pageInfo.prePage}}">上一页</a>
</li>
//遍历分页数就是new PageInfo<>(apis,3);传的3这个值,并判断和当前页码是否相等,若相等加active高亮显示
<li class="page-item" th:classappend="${num == pageInfo.pageNum}?'active':''" th:each="num : ${pageInfo.navigatepageNums}">
<a class="page-link" th:text="${num}" th:href="@{'/api?page='+${num}}"></a>
</li>
//判断是不是查询出来总页数的最后一页,如果是禁用下一页点击
<li class="page-item" th:classappend="${pageInfo.isLastPage == true}?'disabled':''">
<a class="page-link" th:href="@{'/api?page='+${pageInfo.nextPage}}">下一页</a>
</li>
<li class="page-item">
//直接跳转最后一页。注意: 这里要加个1
<a class="page-link" th:href="@{'/api?page='+${pageInfo.lastPage+1}}">末页</a>
</li>
</ul>
</div>
</tbody>
</table>

PageInfo属性

pageNum				当前页
pageSize			每页的数量
size				当前页的数量
orderBy				排序
startRow			当前页面第一个元素在数据库中的行号
endRow				当前页面最后一个元素在数据库中的行号
total				总记录数
pages				总页数
list				结果集
prePage				前一页
nextPage			下一页
isFirstPage			是否为第一页
isLastPage			是否为最后一页
hasPreviousPage		是否有前一页
hasNextPage			是否有下一页
navigatePages		导航页码数
navigatepageNums	所有导航页号
navigateFirstPage	导航第一页
navigateLastPage	导航最后一页
firstPage			第一页
lastPage			最后一页

效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: