paginationCode(hibenagte蛍匈)
扮寂:2011-01-30 cnblogs 清弼湊峠剴
import org.apache.log4j.Logger;
import java.util.List;
/**
* @author Pietro Polsinelli ppolsinelli@open-lab.com
*/
public interface Page {
boolean isFirstPage();
boolean isLastPage();
boolean hasNextPage();
boolean hasPreviousPage();
int getLastPageNumber();
List getThisPageElements();
Logger getLogger();
int getTotalNumberOfElements();
int getThisPageFirstElementNumber();
int getThisPageLastElementNumber();
int getNextPageNumber();
int getPreviousPageNumber();
int getPageSize();
int getPageNumber();
}
import net.sf.hibernate.*;
import net.sf.hibernate.impl.SQLQueryImpl;
import net.sf.hibernate.persister.EntityPersister;
import org.apache.log4j.Logger;
import org.jblooming.PlatformRuntimeException;
import org.jblooming.persistence.hibernate.HibernateFactory;
import org.jblooming.tracer.Tracer;
import org.jblooming.waf.settings.ApplicationState;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
/**
* @author Pietro Polsinelli ppolsinelli@open-lab.com
*/
public class HibernatePage implements Page {
protected List elements;
protected int pageSize;
protected int pageNumber;
protected int totalElements = 0;
public static Set jdbcClassesSupportingScrollCursors = new HashSet();
private ScrollableResults scrollableResults;
private HibernatePage(int pageNumber, int pageSize) {
this.pageNumber = pageNumber;
this.pageSize = pageSize;
}
public boolean isFirstPage() {
return getPageNumber() == 0;
}
public boolean isLastPage() {
return getPageNumber() >= getLastPageNumber();
}
public boolean hasNextPage() {
return elements.size() > getPageSize();
}
public boolean hasPreviousPage() {
return getPageNumber() > 0;
}
public int getLastPageNumber() {
double totalResults =
new Integer(getTotalNumberOfElements()).doubleValue();
return new Double(Math.floor(totalResults / getPageSize())).intValue();
}
public List getThisPageElements() {
/*
* Since we retrieved one more than the specified pageSize when the
* class was constructed, we now trim it down to the pageSize if a next
* page exists.
*/
return hasNextPage() ? elements.subList(0, getPageSize()) : elements;
}
public Logger getLogger() {
return Tracer.hibernateLogger;
}
public int getTotalNumberOfElements() {
return totalElements;
}
public int getThisPageFirstElementNumber() {
return getPageNumber() * getPageSize() + 1;
}
public int getThisPageLastElementNumber() {
int fullPage = getThisPageFirstElementNumber() + getPageSize() - 1;
return getTotalNumberOfElements() < fullPage ?
getTotalNumberOfElements() :
fullPage;
}
public int getNextPageNumber() {
return getPageNumber() + 1;
}
public int getPreviousPageNumber() {
return getPageNumber(
|