PageHelper分页插件的使用二:
二、使用流程
1.添加依赖包:下载依赖
pagehelper-5.1.10,jsqlparser-3.0;
2.在spring-mvc中添加:
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--pageHelper--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置参数,一行配置一个 --> <value> helperDialect=postgresql reasonable=true supportMethodsArguments=true params=count=countSql autoRuntimeDialect=true </value> </property> </bean> </array> </property> </bean>
4、在service中添加接口:
PageInfo<User> findByPage(Integer pageNum,Integer pageSize);//分页
5、ServiceImpl中实现接口service:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <h1>sdda;sjlfj;aso</h1> <table border="1" cellpadding="0"> <tr> <th>编号</th> <th>编号1</th> <th>编号2</th> <th>编号3</th> <th>编号4</th> </tr> <c:forEach items="${pageInfo.list}" var="user"> <tr> <td>${user.getUserId() }</td> <td>${user.getUserId() }</td> <td>${user.getUserId() }</td> <td>${user.getUserId() }</td> </tr> </c:forEach> </table> <span>第${pageInfo.pageNum}页/共${pageInfo.pages }页</span> <a href="users?pageNum=0">首页</a> <c:if test="${pageInfo.isHasPreviousPage()}"> <a href="users?pageNum=${pageInfo.pageNum-1}">上一页</a> </c:if> <c:forEach begin="1" end="${pageInfo.pages}" var="pageNum" varStatus="1"> <a href="users?pageNum=${pageNum}">${pageNum}</a> </c:forEach> <c:if test="${pageInfo.isHasNextPage()}"> <a href="users?pageNum=${pageInfo.pageNum+1}">下一页</a> </c:if> <a href="users?pageNum=${pageInfo.pages}">未页</a> </body> </html>
结果:
编号 | 编号1 | 编号2 | 编号3 | 编号4 |
---|---|---|---|---|
1 | 1 | 1 | 1 | |
12 | 12 | 12 | 12 | |
123456 | 123456 | 123456 | 123456 |
上篇: