目录
4:分页插件PageHelper将参数封装到PageInfo中;该对象包含所有的分页所需参数;
这个功能在github上;地址在PageHelper中文文档
1:导包;
<!--mybatis的Pagehelper分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
2:写配置
在mybatis的配置文件中写;写在settings下边
3:测试
@RequestMapping("getEmployeeAll")
public String getEmployeeAll(@RequestParam(value = "pagenum",defaultValue = "1")Integer pagenum, Map map){
//只需要加上这一句就可以使用pagehelper分页插件;第一个参数是当前页数;第二个是每页多少条
//紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。
PageHelper.startPage(pagenum, 10);
List<Employee> employeeAll = employeeServices.getEmployeeAll();
map.put("employeeAll",employeeAll);
return "employeeList";
}
效果:
4:分页插件PageHelper将参数封装到PageInfo中;该对象包含所有的分页所需参数;
controller:
@RequestMapping("getEmployeeAll")
public String getEmployeeAll(@RequestParam(value = "pagenum",defaultValue = "1")Integer pagenum, Map map){
//只需要加上这一句就可以使用pagehelper分页插件;第一个参数是当前页数;第二个是每页多少条
//紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。
PageHelper.startPage(pagenum, 10);
List<Employee> employeeAll = employeeServices.getEmployeeAll();
//将查询结果房子啊PageInfo中;这个pageinfo中包含分页所需的很多参数
//第一个参数是查询结果;第二个参数是连续显示的页码
PageInfo pageInfo = new PageInfo(employeeAll,5);
System.out.println("当前页面:"+pageInfo.getPageNum());
System.out.println("总页码:"+pageInfo.getPages());
System.out.println("总记录数:"+pageInfo.getTotal());
System.out.println("当前页记录数:"+pageInfo.getSize());
System.out.println("当前页的pagesize:"+pageInfo.getPageSize());
System.out.println("前一页:"+pageInfo.getPrePage());
System.out.println("后一页:"+pageInfo.getNextPage());
System.out.println("这一页最后一个id::"+pageInfo.getEndRow());
System.out.println("查询结果:"+pageInfo.getList());
int[] navigatepageNums = pageInfo.getNavigatepageNums();
System.out.println("连续显示的页码:"+navigatepageNums.toString());
map.put("pageInfo",pageInfo);
return "employeeList";
}
跳转展示的页面:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: wkl
Date: 2020/8/27
Time: 下午 4:39
state:
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<th>id</th>
<th>名字</th>
<th>邮箱</th>
</tr>
<c:forEach items="${pageInfo.list}" var="employee">
<tr>
<td>${employee.id}</td>
<td>${employee.name}</td>
<td>${employee.email}</td>
</tr>
</c:forEach>
<tr>
<td colspan="3">
<a href="/getEmployeeAll?pagenum=1">首页</a>
<a href="/getEmployeeAll?pagenum=${pageInfo.prePage}">上一页</a>
<c:forEach items="${pageInfo.navigatepageNums}" var="num">
<c:choose>
<c:when test="${num==pageInfo.pageNum}">【${num}】</a></c:when>
<c:otherwise><a href="/getEmployeeAll?pagenum=${num}">${num}</a></c:otherwise>
</c:choose>
</c:forEach>
<a href="/getEmployeeAll?pagenum=${pageInfo.nextPage}">下一页</a>
<a href="/getEmployeeAll?pagenum=${pageInfo.pages}">末页</a>
</td>
</tr>
</table>
</body>
</html>
dao.xml:
<!--public List<Employee> getEmployeeAll();-->
<select id="getEmployeeAll" resultType="com.wkl.bean.Employee">
select * from t_employee
</select>