【学习记录】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 最后一页
效果图
完
相关文章推荐
- 记录问题:springboot中使用Mybatis-pagehelper分页插件遇到的版本兼容问题
- Mybatis分页插件PageHelper的学习与使用
- mybatis分页插件PageHelper使用采坑记录 初级菜鸟版
- pageHelper分页插件使用以及 jsp 中数据处理(记录)
- Mybatis分页插件PageHelper正确的使用方法(网上有2篇不够科学的文章)
- pageHelper分页插件的使用
- 【MyBatis】MyBatis分页插件PageHelper的使用
- Spring + Mybatis 使用 PageHelper 插件分页
- MyBatis分页插件PageHelper的使用
- pageHelper分页插件的使用
- Mybatis分页插件PageHelper正确的使用方法(网上有2篇不够科学的文章)
- 淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示
- 分页插件PageHelper使用方法
- MyBatis分页插件PageHelper的使用
- Maven+Spring + Mybatis 使用 PageHelper 插件分页
- 使用mybatis时选择分页插件PageHelper 5.0.0
- 【MyBatis】MyBatis分页插件PageHelper的使用
- mybatis 分页插件pagehelper集成及使用
- MyBatis分页插件PageHelper的使用
- Mybatis分页插件-PageHelper的使用