国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

基于hibernate實現(xiàn)的分頁技術(shù)實例分析

2019-11-26 14:28:50
字體:
供稿:網(wǎng)友

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

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

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 = ?");        //設(shè)置參數(shù)        query.setParameter(0, username);        //設(shè)置每頁顯示多少個,設(shè)置多大結(jié)果。        query.setMaxResults(page.getEveryPage());        //設(shè)置起點        query.setFirstResult(page.getBeginIndex());        return query.list();      }});

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

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

Page.java:

package com.fenye;public class Page {  // 1.每頁顯示數(shù)量(everyPage)  private int everyPage;  // 2.總記錄數(shù)(totalCount)  private int totalCount;  // 3.總頁數(shù)(totalPage)  private int totalPage;  // 4.當(dāng)前頁(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;  }  //構(gòu)造函數(shù),默認  public Page(){}  //構(gòu)造方法,對所有屬性進行設(shè)置  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工具類主要是封裝頁面信息,一共多少數(shù)據(jù)啊,一頁顯示多少啊,起點的序號,總頁數(shù),是否有上一頁下一頁,當(dāng)前頁。

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

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);  }  //設(shè)置每頁顯示記錄數(shù)  public static int getEveryPage(int everyPage) {    return everyPage == 0 ? 10 : everyPage;  }  //設(shè)置當(dāng)前頁  public static int getCurrentPage(int currentPage) {    return currentPage == 0 ? 1 : currentPage;  }  //設(shè)置總頁數(shù),需要總記錄數(shù),每頁顯示多少  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;  }  //設(shè)置起始點,需要每頁顯示多少,當(dāng)前頁  public static int getBeginIndex(int everyPage,int currentPage) {    return (currentPage - 1) * everyPage;  }  //設(shè)置是否有上一頁,需要當(dāng)前頁  public static boolean getHasPrePage(int currentPage) {    return currentPage == 1 ? false : true;  }  //設(shè)置是否有下一個,需要總頁數(shù)和當(dāng)前頁  public static boolean getHasNextPage(int totalPage, int currentPage) {    return currentPage == totalPage || totalPage == 0 ? false : true;  }}

創(chuàng)建Page只需要3個參數(shù),每頁顯示多少數(shù)據(jù),當(dāng)前頁,總共多少數(shù)據(jù),其他的4個參數(shù)都可以通過這三個計算出來

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

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

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武定县| 荥经县| 邳州市| 浏阳市| 凤庆县| 惠来县| 当阳市| 舒兰市| 德昌县| 黔西县| 济源市| 定边县| 固安县| 永善县| 温泉县| 武宣县| 固始县| 莱西市| 漳平市| 柳河县| 乌拉特后旗| 长兴县| 清涧县| 应用必备| 成都市| 政和县| 遂宁市| 安康市| 巫溪县| 依兰县| 宁德市| 邯郸县| 固阳县| 林甸县| 镇安县| 宁国市| 新龙县| 澳门| 乌鲁木齐县| 当雄县| 中山市|