springboot整合pagehelper第一步:在pom.xml中添加依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
然后在代码中直接使用就可以了,如图
PageHelper.startPage(page,rows);
List<User> list =userMapper.search(jsonObject);
PageInfo<User> pageInfo = newPageInfo<User>(list);
return pageInfo;
PageHelper.startPage(需要显示的第几个页面,每个页面显示的数量);
下一行紧跟查询语句,不可以写其他的,否则没有效果。
在jsp页面上进行如下处理:
<div class="row">
<div class="col-md-12">
<table class="table table-hover" target="leftFrame">
<tr>
<th>id</th>
<th>fid</th>
<th>文件原始名字</th>
<th>最后修改时间</th>
<th>文件大小</th>
<th>操作</th>
</tr>
<c:forEach items="${pageInfo.list}" var="file">
<tr>
<th>${file.id}</th>
<th>${file.fid}</th>
<th>${file.originalname}</th>
<th><fmt:formatDate value='${file.lastmodifiedtime}' pattern='yyyy-MM-dd HH:mm:ss'/></th>
<th>${file.fileSize}<c:if test="${file.fileSize!=null}"><span>KB</span></c:if></th>
<th>
<button class="btn btn-primary">
<a href="${pageContext.request.contextPath}/file/downLoad?fid=${file.fid}&fileName=${file.originalname}">下载</a>
</button>
<button class="btn btn-danger">
<a href="${pageContext.request.contextPath}/file/deleteFile?fid=${file.fid}">删除</a>
</button>
</th>
</tr>
</c:forEach>
</table>
</div>
</div>
<!--显示分页信息-->
<div class="row">
<!--文字信息-->
<div class="col-md-6">
当前第 ${pageInfo.pageNum} 页.总共 ${pageInfo.pages} 页.一共 ${pageInfo.total} 条记录
</div>
<!--点击分页-->
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/user/managerUser?index=1">首页</a></li>
<!--上一页-->
<li>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="${pageContext.request.contextPath}/user/managerUser?index=${pageInfo.pageNum-1}" aria-label="Previous">
<span aria-hidden="true">上一页</span>
</a>
</c:if>
</li>
<!--循环遍历连续显示的页面,若是当前页就高亮显示,并且没有链接-->
<c:forEach items="${pageInfo.navigatepageNums}" var="page_num">
<c:if test="${page_num == pageInfo.pageNum}">
<li class="active"><a href="#">${page_num}</a></li>
</c:if>
<c:if test="${page_num != pageInfo.pageNum}">
<li><a href="${pageContext.request.contextPath}/user/managerUser?index=${page_num}">${page_num}</a></li>
</c:if>
</c:forEach>
<!--下一页-->
<li>
<c:if test="${pageInfo.hasNextPage}">
<a href="${pageContext.request.contextPath}/user/managerUser?index=${pageInfo.pageNum+1}"
aria-label="Next">
<span aria-hidden="true">下一页</span>
</a>
</c:if>
</li>
<li><a href="${pageContext.request.contextPath}/user/managerUser?index=${pageInfo.pages}">尾页</a></li>
</ul>
</nav>
</div>
</div>
显示效果如下图
但是要注意在进行了搜索后,你要是选择下一页或者上一页,尾页等的操作时总是会查询出全部的数据,而不是在你搜索的结果内的下一页上一页首页尾页,这个时候我们需要在你搜索的结果页面中进行一个修改,如下
首先你要获取的到你要搜索的关键字,
然后是在页面请求的时候查询的时候直接就在你查询的结果中进行页码等的设置,如下图:
<!--点击分页-->
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/file/search?index=1&sousuo=<%=request.getAttribute("key") %>">首页</a></li>
<!--上一页-->
<li>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="${pageContext.request.contextPath}/file/search?index=${pageInfo.pageNum-1} &sousuo=<%=request.getAttribute("key") %>" aria-label="Previous">
<span aria-hidden="true">上一页</span>
</a>
</c:if>
</li>
<!--循环遍历连续显示的页面,若是当前页就高亮显示,并且没有链接-->
<c:forEach items="${pageInfo.navigatepageNums}" var="page_num">
<c:if test="${page_num == pageInfo.pageNum}">
<li class="active"><a href="#">${page_num}</a></li>
</c:if>
<c:if test="${page_num != pageInfo.pageNum}">
<li><a href="${pageContext.request.contextPath}/file/search?index=${page_num}">${page_num}</a></li>
</c:if>
</c:forEach>
<!--下一页-->
<li>
<c:if test="${pageInfo.hasNextPage}">
<a href="${pageContext.request.contextPath}/file/search?index=${pageInfo.pageNum+1}&sousuo=<%=request.getAttribute("key") %>"
aria-label="Next">
<span aria-hidden="true">下一页</span>
</a>
</c:if>
</li>
<li><a href="${pageContext.request.contextPath}/file/search?index=${pageInfo.pages}&sousuo=<%=request.getAttribute("key") %>">尾页</a></li>
</ul>
</nav>
</div>
注意,在进行上下页请求的时候直接加入了关键字的搜索,这个就限制了你的搜索结果是在你的需要的范围之内的上下页。