分页原理
物理分页
实现原理: SELECT * FROM xxx [WHERE…] LIMIT #{param1}, #{param2}
第一个参数是开始数据的索引位置
第二个参数是要查询多少条数据
- 优点: 不会造成内存溢出
- 缺点: 翻页的速度比较慢
逻辑分页
实现原理: 一次性将所有的数据查询出来放在内存之中,每次需要查询的时候就直接从内存之中去取出相应索引区间的数据
- 优点: 分页的速度比较快
- 缺点: 可能造成内存溢出
1.导入依赖
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2.在mybatis配置文件中配置
<!-- mybatis分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
3.controller
@RequestMapping("/allStudent")
public String list(@RequestParam(required = true,defaultValue = "1") Integer page, Model model){
//page当前页,5-每页的条目数
PageHelper.startPage(page,5);
List<Student> stuList = studentService.queryAllStudent();
PageInfo p = new PageInfo(stuList,5);
//将分页信息传到前端
model.addAttribute("stuList",p);
return "allStudent";
};
4.前端展示
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>学生ID</th>
<th>学生姓名</th>
<th>性别</th>
<th>邮箱</th>
<th>手机号</th>
<th>qq号</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="stu" items="${stuList.list}">
<tr>
<th>${stu.sid}</th>
<th>${stu.sname}</th>
<th>${stu.sex}</th>
<th>${stu.email}</th>
<th>${stu.phone}</th>
<th>${stu.qq}</th>
<th><a href="${pageContext.request.contextPath}/student/toUpdatePage?id=${stu.sid}">更改</a> |
<a href="${pageContext.request.contextPath}/student/deleteStudent?id=${stu.sid}">删除</a>
</th>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<!-- 分页条 -->
<div id="div_pagination_bottom" style="align-content: center">
<nav aria-label="Page navigation">
<ul class="pagination">
<li >
<a <c:if test="${stuList.hasPreviousPage}"> href="${pageContext.request.contextPath}/student/allStudent?page=${stuList.pageNum-1}</c:if> "
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<c:forEach items="${stuList.navigatepageNums }" var="page_Num">
<c:if test="${page_Num == stuList.pageNum }">
<li class="active"><a href="#">${ page_Num}</a></li>
</c:if>
<c:if test="${page_Num != stuList.pageNum }">
<li>
<a href="${pageContext.request.contextPath}/student/allStudent?page=${ page_Num}">${ page_Num}</a>
</li>
</c:if>
</c:forEach>
<li >
<a <c:if test="${stuList.hasNextPage}"> href="${pageContext.request.contextPath}/student/allStudent?page=${stuList.pageNum+1}</c:if>"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
5.pageInfo工具类中的属性
//当前页
private int currentPage;
//总页数
private int pageCount;
//总条数
private int totalRecord;
//页面大小
private int pageSize;
//是否有下一页
private boolean hasNextPage;
//是否有上一页
private boolean hasPrePage;
//当前页码数组
private int[] pageArray;
//当前页数据
private List<T> list;
//下一页
private int nextPage;
//上一页
private int prePage;
//是否是第一页
private boolean isFirstPage;
//是否是最后一页
private boolean isLastPage;
//开始行数
private int startRow;
//结束行数
private int endRow;
//需要显示多少页
private int navigatePages;
public PageInfo() {
this.navigatePages = 5; //默认显示5页
this.hasPrePage = true;
this.hasNextPage = true;
this.isFirstPage = false;
this.isLastPage = true;
}