PageInfo的介紹與使用


轉載:https://blog.csdn.net/sinat_42338962/article/details/84314428

https://blog.csdn.net/zhijun0901/article/details/88084357

 

1.PageInfo屬性表

  1 當前頁 
  2 private int pageNum;
  3 每頁的數量  
  4 private int pageSize;  
  5 當前頁的數量  
  6 private int size;  
  7 //由於startRow和endRow不常用,這里說個具體的用法  
  8 //可以在頁面中"顯示startRow到endRow 共size條數據"  
  9 
 10 當前頁面第一個元素在數據庫中的行號  
 11 private int startRow;  
 12 當前頁面最后一個元素在數據庫中的行號  
 13 private int endRow;  
 14 總記錄數  
 15 private long total;  
 16 總頁數  
 17 private int pages;  
 18 結果集  
 19 private List<T> list;  
 20 
 21 第一頁  
 22 private int firstPage;  
 23 前一頁  
 24 private int prePage;  
 25 
 26 是否為第一頁  
 27 private boolean isFirstPage = false;  
 28 是否為最后一頁  
 29 private boolean isLastPage = false;  
 30 是否有前一頁  
 31 private boolean hasPreviousPage = false;  
 32 是否有下一頁  
 33 private boolean hasNextPage = false;  
 34 導航頁碼數  
 35 private int navigatePages;  
 36 所有導航頁號  
 37 private int[] navigatepageNums;  
 38 后台分頁
 39 
 40 服務器端
 41 service
 42 public PageInfo<T>  methodName(int pageNum, int pageSize) {
 43 //1 設置分頁
 44         PageHelper.startPage(pageNum, pageSize);
 45         //2 查詢
 46         List<T> list =TMapper.mapperMethod();
 47         //3 返回
 48         return new PageInfo<>(list);
 49     }
 50 Web
 51 public @ResponseBody DataGridResultInfo methodName (Vovo){
 52         //1 查詢
 53         PageInfo<T> pageInfo = service. methodName (vo.getPage(), vo.getRows());
 54         //2 封裝
 55         return new DataGridBean(pageInfo.getTotal() , pageInfo.getList() );
 56     }
 57 
 58 瀏覽器端
 59 Datagrid
 60 
 61 $(function(){
 62         //繪制datagrid
 63         //1 准備數據
 64         // 1.1 列列表
 65         var columnArr = [[
 66                           {field:'字段名1',title:'標題1',width:80}, 
 67                           {field:'字段名2',title:'標題2',width:80,
 68                               formatter:function(value,rows,index){
 69                                 //filed匹配值(當前的值),當前行,當前行號
 70                                   return value.info;
 71                               }
 72                           }
 73                           ]];
 74         // 1.2 工具條
 75         var toolbarArr = [
 76                             {
 77                                 iconCls: 'icon-add',//按鈕圖標
 78                                 text : '添加用戶',
 79                                 handler: showadduser//方法名
 80                             }
 81                           ];
 82         // 1.3 請求路徑
 83         var url = "……";
 84         
 85         //2 准備參數
 86         var options = {
 87             "columns":columnArr,
 88             "toolbar":toolbarArr,
 89             "striped":true,                //隔行換色
 90             "idField":"id",                //標識字段
 91             "url":url,                    //請求路徑
 92             "pagination":true,
 93             "rownumbers":true,
 94             "pageSize":2,
 95             "pageList":[2,4,6,8]
 96         };
 97         
 98         //3 繪制
 99         $("#id值").datagrid( options );
100         
101     });
102 
103 感覺這個很簡單,沒什么好寫的
104 不用工具
105 <c:forEach items="${pageInfo.list}" var="p">
106       <tr>
107           <td>${p.屬性1}</td>
108 <td>${p.屬性2}</td>
109 <td>${p.屬性…}</td>
110 </tr>
111 </c:forEach>

2.PageInfo類的使用——Java Page分頁顯示

 1 //entity層實體類
 2  
 3 import java.util.List;
 4  
 5 //分頁展示
 6 //相關屬性:當前頁,頁大小(每頁顯示的條數),總頁數,總條數,數據
 7 //select * from t_user limit 3,3
 8 public class Page {
 9     private Integer currentPage;  //當前頁
10     private Integer pageSize;     //頁大小
11     private Integer pageCount;   //頁數量
12     private Integer totalCount;  //總條數
13     private List<?> list;        //數據
14     private String  url;         //參數路徑
15     public Integer getCurrentPage() {
16         return currentPage;
17     }
18     public void setCurrentPage(Integer currentPage) {
19         this.currentPage = currentPage;
20     }
21     public Integer getPageSize() {
22         return pageSize;
23     }
24     public void setPageSize(Integer pageSize) {
25         this.pageSize = pageSize;
26     }
27     public Integer getPageCount() {
28         return pageCount;
29     }
30     public void setPageCount(Integer pageCount) {
31         this.pageCount = pageCount;
32     }
33     public Integer getTotalCount() {
34         return totalCount;
35     }
36     public void setTotalCount(Integer totalCount) {
37         this.totalCount = totalCount;
38     }
39     public List<?> getList() {
40         return list;
41     }
42     public void setList(List<?> list) {
43         this.list = list;
44     }
45     public String getUrl() {
46         return url;
47     }
48     public void setUrl(String url) {
49         this.url = url;
50     }
51     
52     @Override
53     public String toString() {
54         return "Page [currentPage=" + currentPage + ", pageSize=" + pageSize + ", pageCount=" + pageCount
55                 + ", totalCount=" + totalCount + ", list=" + list + ", url=" + url + "]";
56     }
57     
58     public Page(Integer currentPage, Integer pageSize, Integer pageCount, Integer totalCount, List<?> list,
59             String url) {
60         this.currentPage = currentPage;
61         this.pageSize = pageSize;
62         this.pageCount = pageCount;
63         this.totalCount = totalCount;
64         this.list = list;
65         this.url = url;
66     }
67     
68     public Page() {
69     }
70     
71     public Page(Integer currentPage, Integer pageSize) {
72         this.currentPage = currentPage;
73         this.pageSize = pageSize;
74     }
75 }
1 // servlet層 (分頁相關代碼)
2 //-----直接做分頁展示-----
3 String current = request.getParameter("currentPage"); // 獲取前端傳入的當前頁
4 Page page = goodsInfoService.getPage(current); // 在業務層給page對象賦值
5 request.setAttribute("page", page);

 

 1 //service層相關代碼
 2 public Page getPage(String current) {
 3     Integer currentPage = 1; // 默認為第一頁
 4     Integer pageSize    = 5; // 每頁顯示5條記錄
 5     if(current != null){
 6         currentPage = Integer.parseInt(current);
 7     }
 8     Page page = new Page(currentPage, pageSize);  //當前頁和頁大小的賦值
 9     
10     Integer totalCount = goodsInfoDao.getTotalCount();
11     page.setTotalCount(totalCount);  //設置總條數
12     
13    // 頁數量==總條數/頁大小--如果整除就是該值,否則+1
14     Integer pageCount = totalCount/pageSize;
15     pageCount=totalCount%pageSize==0?pageCount:pageCount+1;
16     page.setPageCount(pageCount);   //設置頁數量
17     
18     Integer startIndex = (currentPage-1)*pageSize; // 起始下標為(當前頁-1)*頁大小
19     List<GoodsInfo> list = goodsInfoDao.getGoodsInfoListPage(startIndex, pageSize);
20     page.setList(list);   //設置數據
21     
22     page.setUrl("GoodsInfoServlet?action=goodsInfoList");  //設置url
23     
24     return page;
25 }

 

1 //Dao相關分頁代碼
2 public int getTotalCount() { // 獲取總記錄數
3     String sql="select count(1) from t_goods_info";
4     return CommonUtils.getTotalCount(sql);
5 }

 

 1 //jquery 相關分頁代碼
 2 <a href="${page.url}&currentPage=1">首頁</a>
 3     <c:if test="${page.currentPage!=1}">
 4         <a href="${page.url}&currentPage=${page.currentPage-1}">上一頁</a>
 5     </c:if>
 6     <c:if test="${page.currentPage!=page.pageCount}">
 7         <a href="${page.url}&currentPage=${page.currentPage+1}">下一頁</a>
 8     </c:if>
 9     <a href="${page.url}&currentPage=${page.pageCount}">尾頁</a>
10 共${page.currentPage}/${page.pageCount}頁

 

3.MyBatis分頁插件-PageHelper的配置與應用

以下轉載於:https://www.cnblogs.com/li150dan/p/9706585.html

下載地址:https://github.com/pagehelper/Mybatis-PageHelper

Pagehelper 下載地址:

http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/

jsqlparser 下載地址:

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/

使用步驟:

pom.xml 引入依賴:

<!-- pagehelper :分頁插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>
<!-- pagehelper的依賴包:jsqlparser -->
<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>0.9.5</version>
</dependency>

 

1、導入相關包 pagehelper-x.x.x.jar 和 jsqlparser-x.x.x.jar。

2、在MyBatis全局配置文件中配置分頁插件。 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
 
<!-- 注意 <plugins> 在xml文件中的位置,必須要符合 http://mybatis.org/dtd/mybatis-3-config.dtd 中指定的順序:-->
<!-- configuration (properties?, settings?, typeAliases?, typeHandlers?,
    objectFactory?, objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?) -->
<configuration>    
    <!-- 為SQL定義部分的parameterType或resultType屬性指定自定義類型的別名 -->
    <typeAliases>        
        <typeAlias alias="ServiceStation" type="com.mybatis.models.ServiceStation" />
        <typeAlias alias="InspectorInfo" type="com.mybatis.models.InspectorInfo" />
        <typeAlias alias="StationInspector" type="com.mybatis.models.StationInspector" />
    </typeAliases>
    <!-- 配置分頁攔截器 -->
    <plugins>
        <!-- 配置分頁插件  -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
        <!-- com.mybatis.util為PageHelper類所在包名 -->
        <!-- <plugin interceptor="com.mybatis.util.PagePlugin"> -->
            <!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫-->       
            <!-- <property name="dialect" value="SQLite" /> -->
            <!-- <property name="pageSqlId" value=".*Page.*" /> -->
        <!-- </plugin> -->
    </plugins>  
    <!-- 設置數據庫連接參數 -->
    <!-- 與spring 集成之后,這些可以完全刪除,數據庫連接的管理交給 spring 去管理 -->
    <environments default="development">
        <environment id="development">
        <transactionManager type="JDBC" />
            <dataSource type="POOLED"> 
                <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
                <property name="url" value="jdbc:sqlserver://mssql-rw-cyp-coopbusiness.vip.test.suixinhuan.com;DatabaseName=CYP_CoopBusiness" />
                <property name="username" value="Umanager" />
                <property name="password" value="ASD123asd!1" />
           </dataSource>
        </environment>
    </environments>  
    <!-- 加載SQL定義文件 -->
    <!-- 這里交給sqlSessionFactory 的 mapperLocations屬性去得到所有配置信息 -->
    <mappers>          
         <mapper resource="com/mybatis/sql/ServiceStation.xml" />
         <mapper resource="com/mybatis/sql/InspectorInfo.xml" />
         <mapper resource="com/mybatis/sql/StationInspector.xml" />
    </mappers>
</configuration>

3、使用PageHelper提供的方法進行分頁

 1 package com.mybatis;
 2  
 3 import java.util.List;
 4  
 5 import org.apache.ibatis.session.SqlSession;
 6  
 7 import com.github.pagehelper.Page;
 8 import com.github.pagehelper.PageHelper;
 9 import com.github.pagehelper.PageInfo;
10 import com.mybatis.dao.InspectorInfoMapper;
11 import com.mybatis.dao.MyBatisUtil;
12 import com.mybatis.dao.ServiceStationMapper;
13 import com.mybatis.dao.StationInspectorMapper;
14 import com.mybatis.models.ServiceStation;
15 import com.mybatis.models.InspectorInfo;
16 import com.mybatis.models.StationInspector;
17 //import com.mybatis.util.Page;
18  
19 /*
20  * 測試類
21  */
22 public class HelloMyBatisProgram {
23      public static void main(String[] args) {
24           SqlSession session = MyBatisUtil.getSession();
25           //由框架生成ServiceStationMapper接口實現對象
26           ServiceStationMapper ssDaoMaper = session.getMapper(ServiceStationMapper.class);
27           InspectorInfoMapper iiDaoMaper = session.getMapper(InspectorInfoMapper.class);
28           StationInspectorMapper siDaoMaper = session.getMapper(StationInspectorMapper.class);
29           //System.out.println(ssDaoMaper.getClass().getName());       
30           System.out.println("===========分頁獲取所有服務站列表============");    
31         
32           //分頁設置放在查詢之前           
33           Page<Object> page = PageHelper.startPage(1, 5, "StationName");
34           List<ServiceStation> listPage = ssDaoMaper.findAllPage("110100");      
35           for(ServiceStation item:listPage) {
36               System.out.println(item.getStationName()+"  "+item.getCityCode()+"  "+item.getCityName());
37           }    
38           System.out.println("當前頁碼:"+page.getPageNum());
39           System.out.println("每頁的記錄數:"+page.getPageSize());
40           System.out.println("總記錄數:"+page.getTotal());
41           System.out.println("總頁碼:"+page.getPages());              
42      }    
43 }

 4、可以使用更強大的PageInfo封裝返回結果

 1 package com.mybatis;
 2  
 3 import java.util.List;
 4  
 5 import org.apache.ibatis.session.SqlSession;
 6  
 7 import com.github.pagehelper.Page;
 8 import com.github.pagehelper.PageHelper;
 9 import com.github.pagehelper.PageInfo;
10 import com.mybatis.dao.InspectorInfoMapper;
11 import com.mybatis.dao.MyBatisUtil;
12 import com.mybatis.dao.ServiceStationMapper;
13 import com.mybatis.dao.StationInspectorMapper;
14 import com.mybatis.models.ServiceStation;
15 import com.mybatis.models.InspectorInfo;
16 import com.mybatis.models.StationInspector;
17 //import com.mybatis.util.Page;
18  
19 /*
20  * 測試類
21  */
22 public class HelloMyBatisProgram {
23      public static void main(String[] args) {
24           SqlSession session = MyBatisUtil.getSession();
25           //由框架生成ServiceStationMapper接口實現對象
26           ServiceStationMapper ssDaoMaper = session.getMapper(ServiceStationMapper.class);
27           InspectorInfoMapper iiDaoMaper = session.getMapper(InspectorInfoMapper.class);
28           StationInspectorMapper siDaoMaper = session.getMapper(StationInspectorMapper.class);
29           //System.out.println(ssDaoMaper.getClass().getName());       
30           System.out.println("===========分頁獲取所有服務站列表============");    
31              
32           //分頁設置放在查詢之前           
33           page = PageHelper.startPage(1, 3, "StationName desc");
34           List<ServiceStation> list = ssDaoMaper.findAll();
35           for(ServiceStation item:list) {
36                System.out.println(item.getStationName()+"  "+item.getCityCode()+"  "+item.getCityName());
37           }         
38           PageInfo<ServiceStation> info = new PageInfo<ServiceStation>(list, 3);                   
39           System.out.println("當前頁碼:"+info.getPageNum());
40           System.out.println("每頁的記錄數:"+info.getPageSize());
41           System.out.println("總記錄數:"+info.getTotal());
42           System.out.println("總頁碼:"+info.getPages());
43           System.out.println("是否第一頁:"+info.isIsFirstPage());
44           System.out.println("連續顯示的頁碼:");
45           int[] nums = info.getNavigatepageNums();
46           for (int i = 0; i < nums.length; i++) {
47                System.out.println(nums[i]);
48           }      
49      }    
50 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM