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 프레임워크를 바탕으로 하는 자바 프로그램 설계에 도움이 되기를 바랍니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Quarkus에서 배치 실행을 위해 프로그래밍 방식으로 Hibernate Commit 제어배치 실행에서 엄청난 양의 데이터를 처리해야 하는 경우 Hibernate가 수행하는 커밋 수를 제어하는 것이 중요합니다. 커밋은 가장 비용이 많이 드는 데이터베이스 작업 중 하나입니다. Quarkus에서는 @Tran...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.