PageHelper分页插件使用

时间:2022-05-10 22:38:18

mybatis的分页插件jar包:

PageHelper分页插件使用
配置方法:

在mybatis配置文件中加下面代码

PageHelper分页插件使用PageHelper分页插件使用
 1 <plugin interceptor="com.github.pagehelper.PageInterceptor">
 2             <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
 3             <property name="helperDialect" value="mysql"/>
 4             <property name="offsetAsPageNum" value="true"/>
 5             <!--
 6                 该参数默认为false
 7                 设置为true时,使用RowBounds分页会进行count查询
 8               -->
 9             <property name="rowBoundsWithCount" value="true"/>
10             <!-- 
11                 设置为true时,如果pageSize=0或者RowBounds.limit=0就会查询出全部的结果
12                 (相当于没有执行分页查询,只是返回结果仍然是Page类型)
13              -->
14             <property name="pageSizeZero" value="true"/>
15             <!-- 
16                 3.3.0版本可用-分页参数合理化,默认false禁用
17                 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
18                 禁用合理化时,如果pageNum<1或pages会返回空数据
19              -->
20             <property name="reasonable" value="true"/>
21             <!-- 
22                 3.5.0版本可用-为了支持startPage(Object params)方法
23                 增加了一个'params'参数来配置参数映射,用于从Map或ServletRequest中取值
24                 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值
25                  不理解该含义的前提下,不要随便复制该配置
26              -->
27             <property name="params" value="pageNum=start;pageSize=limit;"/>
28             <!-- 支持通过Mapper接口参数来传递分页参数  -->
29             <property name="supportMethodsArguments" value="true"/>
30             <!-- always重视返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
31             <property name="returnPageInfo" value="check"/>
32         </plugin>
33     </plugins>
View Code

 

使用示例:

后台控制器

PageHelper分页插件使用PageHelper分页插件使用
 1 //查看学生信息
 2     @RequestMapping(value="/jiaoliu",method=RequestMethod.GET)
 3     public String list(@RequestParam(required = false,defaultValue = "1",value = "pn")Integer pn,
 4             Map<String,Object> map,Model model,HttpSession session) throws IOException{
 5         PageHelper.startPage(pn,4);
 6          SqlSession sqlSession = GetSqlSession.getSqlSession();
 7          int id=((User) session.getAttribute("USER_SESSION")).getId();
 8          //System.out.println(id);
 9          List<Jiaoliu> ans = sqlSession.selectList("cn.edu.hziee.dao.JiaoliuMapper.selectByteacherid",id);
10          PageInfo pageInfo = new PageInfo<>(ans,5);
11          map.put("pageInfo",pageInfo);
12         return "jiaoliu";
13     }
View Code

前台接收

PageHelper分页插件使用PageHelper分页插件使用
  1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2 <%
  3 String path = request.getContextPath();
  4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5 %>
  6 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
  7 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  8 
  9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 10 <html>
 11   <head>
 12     <base href="<%=basePath%>">
 13     
 14     <title>My JSP 'person_list.jsp' starting page</title>
 15     
 16     <meta http-equiv="pragma" content="no-cache">
 17     <meta http-equiv="cache-control" content="no-cache">
 18     <meta http-equiv="expires" content="0">    
 19     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 20     <meta http-equiv="description" content="This is my page">
 21     <!--
 22     <link rel="stylesheet" type="text/css" href="styles.css">
 23     -->
 24     <link rel="stylesheet" href="<%=path %>/css/bootstrap.css">  
 25     <script src="<%=path %>/jq/jquery-3.3.1.min.js"></script>
 26     <script src="<%=path %>/jq/bootstrap.js"></script>
 27   </head>
 28   
 29   <body>
 30  <!--通过bootstrap的栅格系统布局-->
 31 
 32     <div class="container">
 33      
 34         <!--标题-->
 35         <div class="row">
 36                 <div class="col-md-12">
 37                     <h1>学生联系消息</h1>
 38             </div>
 39      
 40         </div>
 41      
 42         <!--按钮-->
 43        <!--  <div class="row">
 44         <div class="col-md-4 col-md-offset-8">
 45             <button class="btn btn-primary">新增</button>
 46             <button class="btn btn-danger">删除</button>
 47         </div>
 48     </div> -->
 49  
 50     <!--显示表格数据-->
 51     <div class="row">
 52         <div class="col-md-12">
 53             <table class="table table-hover">
 54                 <tr>
 55                     <th>编号</th>
 56                     <th>课程名称</th>
 57                     <th>选课学生</th>
 58                     <th>邮箱</th>
 59                     <th>联系电话</th>
 60                     
 61                 </tr>
 62  
 63  
 64                 <c:forEach items="${pageInfo.list}" var="emp">
 65                     <tr>
 66                     <th>${emp.id}</th>
 67                         <th>${emp.ketiName}</th>
 68                         <th>${emp.studentName}</th>
 69                         <th>${emp.email }</th>
 70                         <th>${emp.phone}</th>
 71                        
 72                        <!--  <th>
 73                             <button class="btn btn-primary">
 74                                 <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
 75                                 编辑
 76                             </button>
 77  
 78                             <button class="btn btn-danger">
 79                                 <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
 80                                 删除
 81                             </button>
 82  
 83                         </th> -->
 84                     </tr>
 85                 </c:forEach>
 86  
 87             </table>
 88         </div>
 89  
 90     </div>
 91  
 92     <!--显示分页信息-->
 93     <div class="row">
 94         <!--文字信息-->
 95         <div class="col-md-6">
 96             当前第 ${pageInfo.pageNum} 页.总共 ${pageInfo.pages} 页.一共 ${pageInfo.total} 条记录
 97         </div>
 98  
 99         <!--点击分页-->
100         <div class="col-md-6">
101             <nav aria-label="Page navigation">
102                 <ul class="pagination">
103                     
104                     <li><a href="${pageContext.request.contextPath}/jiaoliu?pn=1">首页</a></li>
105                     
106                     <!--上一页-->
107                     <li>
108                         <c:if test="${pageInfo.hasPreviousPage}">
109                             <a href="${pageContext.request.contextPath}/jiaoliu?pn=${pageInfo.pageNum-1}" aria-label="Previous">
110                                 <span aria-hidden="true">«</span>
111                             </a>
112                         </c:if>
113                     </li>
114  
115                     <!--循环遍历连续显示的页面,若是当前页就高亮显示,并且没有链接-->
116                     <c:forEach items="${pageInfo.navigatepageNums}" var="page_num">
117                         <c:if test="${page_num == pageInfo.pageNum}">
118                             <li class="active"><a href="${pageContext.request.contextPath}/jiaoliu?pn=${page_num}">${page_num}</a></li>
119                         </c:if>
120                         <c:if test="${page_num != pageInfo.pageNum}">
121                             <li><a href="${pageContext.request.contextPath}/jiaoliu?pn=${page_num}">${page_num}</a></li>
122                         </c:if>
123                     </c:forEach>
124  
125                     <!--下一页-->
126                     <li>
127                         <c:if test="${pageInfo.hasNextPage}">
128                             <a href="${pageContext.request.contextPath}/jiaoliu?pn=${pageInfo.pageNum+1}"
129                                aria-label="Next">
130                                 <span aria-hidden="true">»</span>
131                             </a>
132                         </c:if>
133                     </li>
134                     
135                     <li><a href="${pageContext.request.contextPath}/jiaoliu?pn=${pageInfo.pages}">尾页</a></li>
136                 </ul>
137             </nav>
138         </div>
139  
140     </div>
141  
142  
143 </div>
144 
145   </body>
146 </html>
View Code