Example analysis of paging technology based on hibernate implementation

  • 2020-05-07 19:44:35
  • OfStack

This article illustrates the paging technique based on the hibernate implementation. Share with you for your reference, as follows:

First, I will explain the principle of paging based on hibernate. If we take 100 pieces of data from the database, we will display 10 pieces of data per page. If we start from 30, we only need to set the starting position and the maximum return result
Code first: note that the parameters passed in are Page, which is described later


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 = ?");
        // Set the parameters 
        query.setParameter(0, username);
        // Set how many to display per page and how big the results are. 
        query.setMaxResults(page.getEveryPage());
        // Set the starting point 
        query.setFirstResult(page.getBeginIndex());
        return query.list();
      }
});

The key codes above are setMaxResults(), and setFirstResult(), which sets the maximum display value and the starting point

Here we need an Page utility class to manipulate paging.

Page. java:


package com.fenye;
public class Page {
  // 1. Quantity per page (everyPage)
  private int everyPage;
  // 2. The total number of records (totalCount)
  private int totalCount;
  // 3. Total number of pages (totalPage)
  private int totalPage;
  // 4. The current page (currentPage)
  private int currentPage;
  // 5. The starting point (beginIndex)
  private int beginIndex;
  // 6. Whether to have the 1 page (hasPrePage)
  private boolean hasPrePage;
  // 7. Whether there is the 1 page (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;
  }
  // Constructor, default 
  public Page(){}
  // Constructor to set all properties 
  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 tool class is mainly to encapsulate the page information, 1 total number of data ah, how much ah, page 1 display, starting number, total number of pages, whether there is a page on the next page, the current page.

You also need a utility class that operates on page, PageUtil.java


package com.sanqing.fenye;
/*
 *  Paging information helper class 
 */
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);
  }
  // Set the number of records to be displayed per page 
  public static int getEveryPage(int everyPage) {
    return everyPage == 0 ? 10 : everyPage;
  }
  // Set the current page 
  public static int getCurrentPage(int currentPage) {
    return currentPage == 0 ? 1 : currentPage;
  }
  // Set total pages , Total number of records required, how many to display per page 
  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;
  }
  // Set the starting point, how many pages to display per page, and the current page 
  public static int getBeginIndex(int everyPage,int currentPage) {
    return (currentPage - 1) * everyPage;
  }
  // Set whether there is on 1 Page, which requires the current page 
  public static boolean getHasPrePage(int currentPage) {
    return currentPage == 1 ? false : true;
  }
  // Set whether there is a down 1 The total number of pages and the current page are required 
  public static boolean getHasNextPage(int totalPage, int currentPage) {
    return currentPage == totalPage || totalPage == 0 ? false : true;
  }
}

To create Page, you only need three parameters, how much data is displayed per page, the current page, and the total data. The other four parameters can be calculated from these three parameters

So to create Page, just call the tool method PageUtil.createPage (three parameters) and return 1Page.

The Page returned is the Page of the previous argument, which is the page to display

This completes the paging function.

I hope this article is helpful for you to design Java program based on Hibernate framework.


Related articles: