认识Pagehelper分页插件
官网链接
PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件,如果你也在用 MyBatis,建议尝试该分页插件,这一定是最方便使用的分页插件。分页插件支持任何复杂的单表、多表分页。话不多少,talk is cheap show me the code
导入相关依赖
使用任何插件都是如此,需要导入相关依赖,这里我们导入相关依赖。这是与springboot整合pagehelper
<!-- 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
配置分页插件信息
我们在这里使用yml
#分页插件配置
pagehelper:
helper-dialect: mysql
params: count=countSql
reasonable: true
support-methods-arguments: true
设计相关数据
我这里引入上次的employee表
进入controller层
@RequestMapping("/emp")
public String list(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
PageHelper.startPage(pageNum,4);
List<Employee> employees = employeeService.getAll();
PageInfo<Employee> pageInfo = new PageInfo<>(employees);
model.addAttribute("pageInfo",pageInfo);
return "emp/list";
}
注释:
这里是获取查询员工所有信息
其中:PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示4条数据。PageInfo用来封装页面信息,返回给前台界面。PageInfo中的一些我们需要用到的参数如下表:
将数据展示到页面上
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th>id</th>
<th>employeeName</th>
<th>gender</th>
<th>email</th>
<th>department</th>
<th>birth</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="emp:${pageInfo.list}">
<td th:text="${emp.id}"></td>
<td th:text="${emp.employeeName}"></td>
<td th:text="${emp.gender==0?'女':'男'}"></td>
<td th:text="${emp.email}"></td>
<td th:text="${emp.department.departmentName}"></td>
<td th:text="${#dates.format(emp.birth,'yyyy-MM-dd')}"></td>
<td>
<a class="btn btn-sm btn-primary" th:href="@{'/emp/'+${emp.id}}">编辑</a>
<a class="btn btn-sm btn-danger" th:href="@{'/del/'+${emp.id}}">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>
<a th:href="@{/emp}">首页</a>
<a th:href="@{/emp(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
<a th:href="@{/emp(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
<a th:href="@{/emp(pageNum=${pageInfo.pages})}">尾页</a>
友情提醒:
如果遇到爆红,冷静一下,这并不是你代码的问题哦,并不影响你项目的运行。(强迫症的可以使用如下方法:
首先Alt-Enter
看到弹出菜单了吗?有一项的子菜单DisableInspection选了就好了)至于为啥,小编也不太清楚酱紫,网上说是你的依赖没下好,,,,,
最终效果
打开浏览器看效果:
至此,分页功能实现。
该文章参考出处点击查看