PageHelper分頁外掛使用
阿新 • • 發佈:2018-12-21
mybatis的分頁外掛jar包:
配置方法:
在mybatis配置檔案中加下面程式碼
1 <plugin interceptor="com.github.pagehelper.PageInterceptor"> 2 <!-- 設定資料庫型別 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫--> 3 <property name="helperDialect" value="mysql"/> 4 <property name="View CodeoffsetAsPageNum" 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>
使用示例:
後臺控制器
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
前臺接收
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