运行环境:
Intellij idea2018.2+maven3.5.4+tomact8.5.4
PageHelper官方文档
https://github.com/pagehelper/Mybatis-PageHelper
一、在pom.xml中添加pagehelper分页插件
<!-- pageHelper 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.6</version>
</dependency>
二、Spring-mybatis中 配置拦截器插件
<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描model包 使用别名 -->
<property name="typeAliasesPackage" value="com.ssm.model"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 在 Spring 配置文件中配置拦截器插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
helperDialect=mysql -->
<value>
helperDialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
说明:helperDialect=mysql
helperDialect:分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 你可以配置helperDialect属性来指定分页插件使用哪种方言。配置时,可以使用缩写值:mysql
reasonable=true
reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
三、UserController.java中的主要代码
//通过@RequestParam注解,可以获取前台Post的值:pageNo
@RequestMapping(value="/list",method= RequestMethod.GET)
public String pageList(ModelMap map, @RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
Integer pageSize=5;//每页显示记录数
//引入分页查询,使用PageHelper分页功能,在查询之前传入当前页码、每页记录数
PageHelper.startPage(pageNo, pageSize);
List<User> userList = userService.findAll();//获取所有用户信息
//使用PageInfo包装查询结果,只需要将pageInfo交给页面就可以
PageInfo<User> pageInfo=new PageInfo<User>(userList,pageSize);
map.addAttribute("pageInfo", pageInfo);
return "list";
}
说明:
defaultValue=“1”,required=true,value="pageNo
1、 以上的value值是对传入的参数有所指定,如果传入的参数不是value值,会报错。
2、request=false(ture)表示前端的参数是否一定要传入。
需要注意的是value=“pageNo”,它是Int型的,如果不传入,会报错,于是设置一个默认值default= 1,因为如果不传入值就会为空值null,int为空值是不可以的。
四、service类、dao类中代码就是查询查询全部用户信息,代码和之前相同
五、jsp页面使用bootstarp获取值,然后实现分页
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<html>
<head>
<meta charset="utf-8">
<title>Bootstrap 实例 - 默认的分页</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<!--通过bootstrap的栅格系统布局-->
<div class="container">
<!--标题-->
<div class="row">
<div class="col-md-12">
<h1>用户管理</h1>
</div>
</div>
<!--按钮-->
<div class="row">
<div class="col-md-4 col-md-offset-8">
<a class="btn btn-primary" href="/user/toAddUser">新增</a>
</div>
</div>
<!--显示表格数据-->
<div class="row">
<div class="col-md-12">
<table class="table table-hover table-striped">
<tr>
<th>#</th>
<th>name</th>
<th>sex</th>
<th>phone</th>
<th>password</th>
<th>操作</th>
</tr>
<c:forEach items="${pageInfo.list}" var="user">
<tr>
<th>${user.id}</th>
<th>${user.name}</th>
<th>${user.sex}</th>
<th>${user.phone}</th>
<th>${user.password}</th>
<th>
<a type="button" href="${path}/user/toUpdate?id=${user.id}" class="btn btn-info btn-sm">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
修改</a>
<a type="button" href="${path}/user/delUser?id=${user.id}" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
删除</a>
</th>
</tr>
</c:forEach>
</table>
</div>
</div>
<!--显示分页信息-->
<div class="row">
<!--文字信息-->
<%--如果想把显示多少条记录挪到右边去,用div style="float: right--%>
<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/list?pageNo=1">首页</a></li>
<!--上一页-->
<li>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="${pageContext.request.contextPath}/user/list?pageNo=${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/list?pageNo=${page_num}">${page_num}</a>
</li>
</c:if>
</c:forEach>
<!--下一页-->
<li>
<c:if test="${pageInfo.hasNextPage}">
<a href="${pageContext.request.contextPath}/user/list?pageNo=${pageInfo.pageNum+1}"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</c:if>
</li>
<li><a href="${pageContext.request.contextPath}/user/list?pageNo=${pageInfo.pages}">尾页</a></li>
</ul>
</nav>
</div>
</div>
</div>
</body>
</html>
六、分页结果如下