hibernate를 바탕으로 이루어진 페이지 기술 실례 분석

본고는 hibernate를 바탕으로 이루어진 페이지 나누기 기술을 실례로 서술하였다.다음과 같이 여러분에게 참고할 수 있도록 공유합니다.
먼저 hibernate를 바탕으로 페이지를 나누는 원리를 설명합니다. 만약에 데이터베이스에서 100개의 데이터를 꺼낸다면 우리는 페이지당 10개를 보여야 합니다. 만약에 30부터 시작한다면 시작 위치와 최대 반환 결과를 설정하면 됩니다.
먼저 코드: 들어오는 매개 변수는 페이지 종류가 있고 뒤에 소개가 있습니다.

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.java:

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;
  }
}

페이지 도구 클래스는 주로 페이지 정보를 봉인하는 것입니다. 모두 얼마나 많은 데이터가 있습니까? 한 페이지에 얼마나 표시되는지, 시작점의 번호, 총 페이지 수, 이전 페이지의 다음 페이지, 현재 페이지가 있는지 여부입니다.
페이지를 조작하는 도구 클래스가 필요합니다. 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);
  }
  // 
  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;
  }
}

페이지를 만드는 데는 3개의 매개 변수만 필요합니다. 페이지마다 얼마나 많은 데이터를 표시하는지, 현재 페이지, 총 얼마나 많은 데이터를 표시하는지, 기타 4개의 매개 변수는 이 세 가지를 통해 계산할 수 있습니다.
그래서 뒤에 페이지를 만들려면 이 도구를 사용하면 됩니다.createPage(3개 매개 변수), 1Page로 돌아갑니다.
되돌아오는 페이지는 바로 앞의 매개 변수의 페이지입니다. 즉 표시할 페이지입니다.
이렇게 하면 페이지를 나누는 기능을 완성한 셈이다.
본고에서 기술한 바와 같이 Hibernate 프레임워크를 바탕으로 하는 자바 프로그램 설계에 도움이 되기를 바랍니다.

좋은 웹페이지 즐겨찾기