PageHelper是一款mabatis分页插件,但是归根结底的原理还是基于limit关键字进行分页的。
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>4.1.4version>
dependency>
<dependency>
<groupId>com.github.jsqlparsergroupId>
<artifactId>jsqlparserartifactId>
<version>0.9.5version>
dependency>
1.如果是使用了spring整合mybatis的方式,只需要找到对应的spring 配置文件,在下面添加如下的pageHelper插件配置即可
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
value>
property>
bean>
array>
property>
bean>
<div class="panel-body">
<div class="row">
<div class="col-md-6" style="line-height: 30px;">
当前第<span class="pageStyle">${pageInfo.pageNum}span>页
共<span class="pageStyle">${pageInfo.pages}span>页
总计<span class="pageStyle">${pageInfo.total}span>条
div>
<div class="col-md-6">
<nav aria-label="Page navigation" class="pull-right">
<ul class="pagination pagination-sm" style="margin: 0px;">
<li>
<a href="${pageContext.request.contextPath}/employee/toList?pageNo=1">首页a>
li>
<c:if test="${pageInfo.pageNum==1}">
<li class="disabled">
<a href="#" aria-label="Previous" class="prePage">
<span aria-hidden="true">上一页span>
a>
li>
c:if>
<c:if test="${pageInfo.pageNum!=1}">
<li>
<a href="#" aria-label="Previous" class="prePage">
<span aria-hidden="true">上一页span>
a>
li>
c:if>
<c:forEach items="${pageInfo.navigatepageNums}" step="1" var="itemPage">
<c:if test="${pageInfo.pageNum == itemPage}">
<li class="active">
<a
href="${pageContext.request.contextPath}/employee/toList?pageNo=${itemPage}">${itemPage}a>
li>
c:if>
<c:if test="${pageInfo.pageNum != itemPage}">
<li>
<a
href="${pageContext.request.contextPath}/employee/toList?pageNo=${itemPage}">${itemPage}a>
li>
c:if>
c:forEach>
<c:if test="${pageInfo.pageNum == pageInfo.pages}">
<li class="disabled" class="nextPage">
<a href="#" aria-label="Next">
<span aria-hidden="true">下一页span>
a>
li>
c:if>
<c:if test="${pageInfo.pageNum != pageInfo.pages}">
<li>
<a href="#" aria-label="Next" class="nextPage">
<span aria-hidden="true">下一页span>
a>
li>
c:if>
<li><a
href="${pageContext.request.contextPath}/employee/toList?pageNo=${pageInfo.pages}">尾页a>
li>
ul>
nav>
div>
div>
div>
<%--分页script--%>
<!-- 业务处理 -->
<script type="text/javascript">
//处理上下页
$(function () {
//参数定义 curPage(当前页) totalPages(总页数)
var curPage = ${ pageInfo.pageNum };
var totalPages = ${ pageInfo.total };
//判断是否还存在上一页,是就请求后台
$(".prePage").click(function () {
if (curPage > 1) {
var pageNo = curPage - 1;
$(this).attr("href", "${pageContext.request.contextPath}/employee/toList?pageNo=" + pageNo);
}
});
//判断是否还存在下一页,是就请求后台
$(".nextPage").click(function () {
if (curPage < totalPages) {
var pageNo = curPage + 1;
$(this).attr("href", "${pageContext.request.contextPath}/employee/toList?pageNo=" + pageNo);
}
});
})
</script>
<%--分页script--%>
@RequestMapping(value = "/toList", method = RequestMethod.GET)
public String userList(Model model, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) {
//第一次请求没有页数,所以这里先指定默认第一次请求参数为1
//startPage为pageHelper默认方法,只需要传入两个参数,一个当前页,一个每页显示条数
PageHelper.startPage(pageNo, 10);
//getEmployeeList()为自己定义的查询全部数据
List<Employee> employees = employeeService.getEmployeeList(); //查询全部数据
//将查询到的所有数据放入到实现分页PageInfo中实现分页效果
PageInfo<Employee> pageInfo = new PageInfo<>(employees, 10);
model.addAttribute("pageInfo", pageInfo);
return "employee_info/info";
}