一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 基于hibernate實現的分頁技術實例分析

基于hibernate實現的分頁技術實例分析

2020-04-12 16:50beyond667 JAVA教程

這篇文章主要介紹了基于hibernate實現的分頁技術,結合實例形式分析了Hibernate分頁技術的原理,實現步驟與相關實現技巧,需要的朋友可以參考下

本文實例講述了基于hibernate實現的分頁技術。分享給大家供大家參考,具體如下:

先說明一下基于hibernate實現分頁的原理,假如從數據庫取出100條數據,我們要讓每頁顯示10條,假如從30開始,只需要設置起始位置和最大的返回結果即可
先上代碼:注意傳進來的參數有 Page這類,后面有介紹

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public List<Article> queryByPage(final String username, final Page page) {
    return this.getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session session)
          throws HibernateException, SQLException {
        Query query = session.createQuery("select art from Article art where art.username = ?");
        //設置參數
        query.setParameter(0, username);
        //設置每頁顯示多少個,設置多大結果。
        query.setMaxResults(page.getEveryPage());
        //設置起點
        query.setFirstResult(page.getBeginIndex());
        return query.list();
      }
});

上面關鍵代碼是 setMaxResults(),和setFirstResult(),即設置最大顯示值和起點

這里我們需要一個Page工具類,用來操作分頁。

Page.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.fenye;
public class Page {
  // 1.每頁顯示數量(everyPage)
  private int everyPage;
  // 2.總記錄數(totalCount)
  private int totalCount;
  // 3.總頁數(totalPage)
  private int totalPage;
  // 4.當前頁(currentPage)
  private int currentPage;
  // 5.起始點(beginIndex)
  private int beginIndex;
  // 6.是否有上一頁(hasPrePage)
  private boolean hasPrePage;
  // 7.是否有下一頁(hasNextPage)
  private boolean hasNextPage;
  public Page(int everyPage, int totalCount, int totalPage, int currentPage,
      int beginIndex, boolean hasPrePage, boolean hasNextPage) {
    this.everyPage = everyPage;
    this.totalCount = totalCount;
    this.totalPage = totalPage;
    this.currentPage = currentPage;
    this.beginIndex = beginIndex;
    this.hasPrePage = hasPrePage;
    this.hasNextPage = hasNextPage;
  }
  //構造函數,默認
  public Page(){}
  //構造方法,對所有屬性進行設置
  public int getEveryPage() {
    return everyPage;
  }
  public void setEveryPage(int everyPage) {
    this.everyPage = everyPage;
  }
  public int getTotalCount() {
    return totalCount;
  }
  public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
  }
  public int getTotalPage() {
    return totalPage;
  }
  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }
  public int getCurrentPage() {
    return currentPage;
  }
  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }
  public int getBeginIndex() {
    return beginIndex;
  }
  public void setBeginIndex(int beginIndex) {
    this.beginIndex = beginIndex;
  }
  public boolean isHasPrePage() {
    return hasPrePage;
  }
  public void setHasPrePage(boolean hasPrePage) {
    this.hasPrePage = hasPrePage;
  }
  public boolean isHasNextPage() {
    return hasNextPage;
  }
  public void setHasNextPage(boolean hasNextPage) {
    this.hasNextPage = hasNextPage;
  }
}

Page工具類主要是封裝頁面信息,一共多少數據啊,一頁顯示多少啊,起點的序號,總頁數,是否有上一頁下一頁,當前頁。

還需要一個操作page的工具類,PageUtil.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.sanqing.fenye;
/*
 * 分頁信息輔助類
 */
public class PageUtil {
  public static Page createPage(int everyPage,int totalCount,int currentPage) {
    everyPage = getEveryPage(everyPage);
    currentPage = getCurrentPage(currentPage);
    int totalPage = getTotalPage(everyPage, totalCount);
    int beginIndex = getBeginIndex(everyPage, currentPage);
    boolean hasPrePage = getHasPrePage(currentPage);
    boolean hasNextPage = getHasNextPage(totalPage, currentPage);
    return new Page(everyPage, totalCount, totalPage, currentPage,
        beginIndex, hasPrePage, hasNextPage);
  }
  public static Page createPage(Page page,int totalCount) {
    int everyPage = getEveryPage(page.getEveryPage());
    int currentPage = getCurrentPage(page.getCurrentPage());
    int totalPage = getTotalPage(everyPage, totalCount);
    int beginIndex = getBeginIndex(everyPage, currentPage);
    boolean hasPrePage = getHasPrePage(currentPage);
    boolean hasNextPage = getHasNextPage(totalPage, currentPage);
    return new Page(everyPage, totalCount, totalPage, currentPage,
        beginIndex, hasPrePage, hasNextPage);
  }
  //設置每頁顯示記錄數
  public static int getEveryPage(int everyPage) {
    return everyPage == 0 ? 10 : everyPage;
  }
  //設置當前頁
  public static int getCurrentPage(int currentPage) {
    return currentPage == 0 ? 1 : currentPage;
  }
  //設置總頁數,需要總記錄數,每頁顯示多少
  public static int getTotalPage(int everyPage,int totalCount) {
    int totalPage = 0;
    if(totalCount % everyPage == 0) {
      totalPage = totalCount / everyPage;
    } else {
      totalPage = totalCount / everyPage + 1;
    }
    return totalPage;
  }
  //設置起始點,需要每頁顯示多少,當前頁
  public static int getBeginIndex(int everyPage,int currentPage) {
    return (currentPage - 1) * everyPage;
  }
  //設置是否有上一頁,需要當前頁
  public static boolean getHasPrePage(int currentPage) {
    return currentPage == 1 ? false : true;
  }
  //設置是否有下一個,需要總頁數和當前頁
  public static boolean getHasNextPage(int totalPage, int currentPage) {
    return currentPage == totalPage || totalPage == 0 ? false : true;
  }
}

創建Page只需要3個參數,每頁顯示多少數據,當前頁,總共多少數據,其他的4個參數都可以通過這三個計算出來

所以后面要創建Page,只需要調用這工具方法PageUtil.createPage(3個參數),就返回一Page.

返回的Page就是前面參數的Page,即要顯示的分頁

這樣就算完成了分頁的功能。

希望本文所述對大家基于Hibernate框架的Java程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜理论电影在线观看亚洲 | 欧美香蕉人人人人人人爱 | 午夜欧美福利视频 | 久久久久久免费高清电影 | 四虎e234hcom| 国产亚洲女在线精品 | 日韩精品免费一区二区三区 | 日本xxxx69hd | 春意影院午夜爽爽爽免费 | 岛国虐乳紧缚媚药调教 | 99撸| 国产人妖xxxxx免费看 | 办公室强行丝袜秘书啪啪 | 青青草原国产一区二区 | 国产思妍小仙女一二区 | 精品久久一 | 日韩精品欧美 | 国产精品日韩在线观看 | 成人免费观看网欧美片 | 日本hd18| 欧美日韩导航 | 日本三级做a全过程在线观看 | fuqer老师 | 男人的天堂久久精品激情a 男人的天堂va | 欧美乱妇高清无乱码视频在线 | 亚洲精品视 | 亚洲v日韩v欧美在线观看 | 99视频一区 | 亚洲精品永久免费 | 太大了轻点阿受不了小说h 四色6677最新永久网站 | 午夜尤物| 天堂色| 精品日韩欧美一区二区三区 | 久久re视频精品538在线 | 国产精品猎奇系列在线观看 | 亚洲午夜精品久久久久 | 天海翼三级 | 青春草在线观看精品免费视频 | 天天狠天天天天透在线 | 奇米影视先锋 | 欧美日韩中文国产一区 |