`

Java分页实现

阅读更多
Java代码 复制代码
  1. public interface Paginable {   
  2.   
  3.     public int getTotalCount();   
  4.   
  5.     public int getPageCount();   
  6.   
  7.     public int getPageSize();   
  8.   
  9.     public int getCurrentPage();   
  10.        
  11.     public int getStartIndex();   
  12.   
  13.     public boolean isFirstPage();   
  14.   
  15.     public boolean isLastPage();   
  16.   
  17.     public int getNextPage();   
  18.   
  19.     public int getPrePage();   
  20. }  
public interface Paginable {

	public int getTotalCount();

	public int getPageCount();

	public int getPageSize();

	public int getCurrentPage();
	
	public int getStartIndex();

	public boolean isFirstPage();

	public boolean isLastPage();

	public int getNextPage();

	public int getPrePage();
}



Java代码 复制代码
  1. public class SimplePage implements Paginable {   
  2.   
  3.     public final static int PAGESIZE = 4;   
  4.   
  5.     // Total count of records   
  6.     protected int totalCount;   
  7.        
  8.     // The size of records per page   
  9.     protected int pageSize = PAGESIZE;   
  10.        
  11.     // Current page   
  12.     protected int currentPage;   
  13.        
  14.     // The count of pages   
  15.     private int pageCount;   
  16.   
  17.     public SimplePage() {   
  18.     }   
  19.        
  20.     public SimplePage(int totalCount) {   
  21.         setPageSize(PAGESIZE);   
  22.         setTotalCount(totalCount);   
  23.         setCurrentPage(1);   
  24.     }   
  25.   
  26.     public SimplePage(int currentPage, int totalCount) {   
  27.         setPageSize(PAGESIZE);   
  28.         setTotalCount(totalCount);   
  29.         setCurrentPage(currentPage);   
  30.     }   
  31.   
  32.     public SimplePage(int currentPage, int pageSize, int totalCount) {   
  33.         setPageSize(pageSize);   
  34.         setTotalCount(totalCount);   
  35.         setCurrentPage(currentPage);   
  36.     }   
  37.   
  38.     public boolean isFirstPage() {   
  39.         return currentPage <= 1;   
  40.     }   
  41.   
  42.     public boolean isLastPage() {   
  43.         return currentPage >= pageCount;   
  44.     }   
  45.   
  46.     public int getNextPage() {   
  47.         if (isLastPage()) {   
  48.             return currentPage;   
  49.         } else {   
  50.             return currentPage + 1;   
  51.         }   
  52.     }   
  53.   
  54.     public int getPrePage() {   
  55.         if (isFirstPage()) {   
  56.             return currentPage;   
  57.         } else {   
  58.             return currentPage - 1;   
  59.         }   
  60.     }   
  61.   
  62.     public int getCurrentPage() {   
  63.         return currentPage;   
  64.     }   
  65.        
  66.     public int getStartIndex() {   
  67.         return (currentPage - 1) * pageSize;   
  68.     }   
  69.   
  70.     public void setCurrentPage(int currentPage) {   
  71.         if (totalCount <= 0)   
  72.             this.currentPage = 0;   
  73.         else if (currentPage >= pageCount)   
  74.             this.currentPage = pageCount;   
  75.         else if (currentPage <= 1)   
  76.             this.currentPage = 1;   
  77.         else {   
  78.             this.currentPage = currentPage;   
  79.         }   
  80.     }   
  81.   
  82.     public int getPageSize() {   
  83.         return pageSize;   
  84.     }   
  85.   
  86.     public void setPageSize(int pageSize) {   
  87.         this.pageSize = pageSize;   
  88.     }   
  89.   
  90.     public int getTotalCount() {   
  91.         return totalCount;   
  92.     }   
  93.   
  94.     public void setTotalCount(int totalCount) {   
  95.         if (totalCount > 0) {   
  96.             this.totalCount = totalCount;   
  97.             pageCount = totalCount / pageSize;   
  98.             if (totalCount % pageSize > 0)   
  99.                 pageCount++;   
  100.         } else {   
  101.             this.totalCount = 0;   
  102.         }   
  103.     }   
  104.   
  105.     public int getPageCount() {   
  106.         return pageCount;   
  107.     }   
  108.   
  109.     public void setPageCount(int pageCount) {   
  110.         this.pageCount = pageCount;   
  111.     }   
  112. }  
public class SimplePage implements Paginable {

	public final static int PAGESIZE = 4;

	// Total count of records
	protected int totalCount;
	
	// The size of records per page
	protected int pageSize = PAGESIZE;
	
	// Current page
	protected int currentPage;
	
	// The count of pages
	private int pageCount;

	public SimplePage() {
	}
	
	public SimplePage(int totalCount) {
		setPageSize(PAGESIZE);
		setTotalCount(totalCount);
		setCurrentPage(1);
	}

	public SimplePage(int currentPage, int totalCount) {
		setPageSize(PAGESIZE);
		setTotalCount(totalCount);
		setCurrentPage(currentPage);
	}

	public SimplePage(int currentPage, int pageSize, int totalCount) {
		setPageSize(pageSize);
		setTotalCount(totalCount);
		setCurrentPage(currentPage);
	}

	public boolean isFirstPage() {
		return currentPage <= 1;
	}

	public boolean isLastPage() {
		return currentPage >= pageCount;
	}

	public int getNextPage() {
		if (isLastPage()) {
			return currentPage;
		} else {
			return currentPage + 1;
		}
	}

	public int getPrePage() {
		if (isFirstPage()) {
			return currentPage;
		} else {
			return currentPage - 1;
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}
	
	public int getStartIndex() {
		return (currentPage - 1) * pageSize;
	}

	public void setCurrentPage(int currentPage) {
		if (totalCount <= 0)
			this.currentPage = 0;
		else if (currentPage >= pageCount)
			this.currentPage = pageCount;
		else if (currentPage <= 1)
			this.currentPage = 1;
		else {
			this.currentPage = currentPage;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		if (totalCount > 0) {
			this.totalCount = totalCount;
			pageCount = totalCount / pageSize;
			if (totalCount % pageSize > 0)
				pageCount++;
		} else {
			this.totalCount = 0;
		}
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
}



Java代码 复制代码
  1. import java.util.List;   
  2.   
  3. @SuppressWarnings("serial")   
  4. public class Pagination extends SimplePage implements java.io.Serializable,   
  5.         Paginable {   
  6.   
  7.     public Pagination() {   
  8.     }   
  9.   
  10.     @SuppressWarnings("unchecked")   
  11.     public Pagination(int totalCount, List list) {   
  12.         super(totalCount);   
  13.         this.list = list;   
  14.     }   
  15.   
  16.     @SuppressWarnings("unchecked")   
  17.     public Pagination(int currentPage, int totalCount, List list) {   
  18.         super(currentPage, totalCount);   
  19.         this.list = list;   
  20.     }   
  21.   
  22.     @SuppressWarnings("unchecked")   
  23.     public Pagination(int currentPage, int pageSize, int totalCount, List list) {   
  24.         super(currentPage, pageSize, totalCount);   
  25.         this.list = list;   
  26.     }   
  27.   
  28.     public Pagination(int currentPage, int pageSize, int totalCount) {   
  29.         super(currentPage, pageSize, totalCount);   
  30.     }   
  31.   
  32.     public int getFirstResult() {   
  33.         return (currentPage - 1) * pageSize;   
  34.     }   
  35.   
  36.     @SuppressWarnings("unchecked")   
  37.     private List list;   
  38.   
  39.     @SuppressWarnings("unchecked")   
  40.     public List getList() {   
  41.         return list;   
  42.     }   
  43.   
  44.     @SuppressWarnings("unchecked")   
  45.     public void setList(List list) {   
  46.         this.list = list;   
  47.     }   
  48. }  
import java.util.List;

@SuppressWarnings("serial")
public class Pagination extends SimplePage implements java.io.Serializable,
		Paginable {

	public Pagination() {
	}

	@SuppressWarnings("unchecked")
	public Pagination(int totalCount, List list) {
		super(totalCount);
		this.list = list;
	}

	@SuppressWarnings("unchecked")
	public Pagination(int currentPage, int totalCount, List list) {
		super(currentPage, totalCount);
		this.list = list;
	}

	@SuppressWarnings("unchecked")
	public Pagination(int currentPage, int pageSize, int totalCount, List list) {
		super(currentPage, pageSize, totalCount);
		this.list = list;
	}

	public Pagination(int currentPage, int pageSize, int totalCount) {
		super(currentPage, pageSize, totalCount);
	}

	public int getFirstResult() {
		return (currentPage - 1) * pageSize;
	}

	@SuppressWarnings("unchecked")
	private List list;

	@SuppressWarnings("unchecked")
	public List getList() {
		return list;
	}

	@SuppressWarnings("unchecked")
	public void setList(List list) {
		this.list = list;
	}
}



Java代码 复制代码
  1. public class PageUtil {   
  2.   
  3.     public static String getPageBar1(Paginable page, String url) {   
  4.         String temp = "";   
  5.         if (url.indexOf("?") == -1) {   
  6.             temp = "?";   
  7.         } else {   
  8.             temp = "&";   
  9.         }   
  10.         StringBuffer pageBar = new StringBuffer();   
  11.         if (page.isFirstPage())   
  12.             pageBar.append("First Previous&nbsp;");   
  13.         else {   
  14.             pageBar.append("<a href='").append(url).append(temp).append(   
  15.                     "currentPage=1'>First</a>&nbsp;");   
  16.             pageBar.append("<a href='").append(url).append(temp).append(   
  17.                     "currentPage=").append(page.getPrePage()).append(   
  18.                     "'>Previous</a>&nbsp;");   
  19.         }   
  20.         if (page.isLastPage())   
  21.             pageBar.append("Next Last&nbsp;");   
  22.         else {   
  23.             pageBar.append("<a href='").append(url).append(temp).append(   
  24.                     "currentPage=").append(page.getNextPage()).append(   
  25.                     "'>Next</a>&nbsp;");   
  26.             pageBar.append("<a href='").append(url).append(temp).append(   
  27.                     "currentPage=").append(page.getPageCount()).append(   
  28.                     "'>Last</a>&nbsp;");   
  29.         }   
  30.         pageBar.append("&nbsp;Total Page:").append(page.getPageCount()).append(   
  31.                 "&nbsp;");   
  32.         pageBar.append("&nbsp;Go<select name='page' onChange=\"location='");   
  33.         pageBar.append(url).append(temp).append(   
  34.                 "currentPage='+this.options[this.selectedIndex].value\">");   
  35.         int begin = (page.getCurrentPage() > 10) ? page.getCurrentPage() - 10  
  36.                 : 1;   
  37.         int end = (page.getPageCount() - page.getCurrentPage() > 10) ? page   
  38.                 .getCurrentPage() + 10 : page.getPageCount();   
  39.         for (int i = begin; i <= end; i++) {   
  40.             if (i == page.getCurrentPage())   
  41.                 pageBar.append("<option value='").append(i).append(   
  42.                         "' selected>-").append(i).append("-</option>");   
  43.             else  
  44.                 pageBar.append("<option value='").append(i).append("'>-")   
  45.                         .append(i).append("-</option>");   
  46.         }   
  47.         pageBar.append("</select>");   
  48.         return pageBar.toString();   
  49.     }   
  50.        
  51.     //Implement other page bar you wanted   
  52.     public static String getPageBar2(Paginable page, String url){   
  53.         return "";   
  54.     }   
  55. }  
public class PageUtil {

	public static String getPageBar1(Paginable page, String url) {
		String temp = "";
		if (url.indexOf("?") == -1) {
			temp = "?";
		} else {
			temp = "&";
		}
		StringBuffer pageBar = new StringBuffer();
		if (page.isFirstPage())
			pageBar.append("First Previous&nbsp;");
		else {
			pageBar.append("<a href='").append(url).append(temp).append(
					"currentPage=1'>First</a>&nbsp;");
			pageBar.append("<a href='").append(url).append(temp).append(
					"currentPage=").append(page.getPrePage()).append(
					"'>Previous</a>&nbsp;");
		}
		if (page.isLastPage())
			pageBar.append("Next Last&nbsp;");
		else {
			pageBar.append("<a href='").append(url).append(temp).append(
					"currentPage=").append(page.getNextPage()).append(
					"'>Next</a>&nbsp;");
			pageBar.append("<a href='").append(url).append(temp).append(
					"currentPage=").append(page.getPageCount()).append(
					"'>Last</a>&nbsp;");
		}
		pageBar.append("&nbsp;Total Page:").append(page.getPageCount()).append(
				"&nbsp;");
		pageBar.append("&nbsp;Go<select name='page' onChange=\"location='");
		pageBar.append(url).append(temp).append(
				"currentPage='+this.options[this.selectedIndex].value\">");
		int begin = (page.getCurrentPage() > 10) ? page.getCurrentPage() - 10
				: 1;
		int end = (page.getPageCount() - page.getCurrentPage() > 10) ? page
				.getCurrentPage() + 10 : page.getPageCount();
		for (int i = begin; i <= end; i++) {
			if (i == page.getCurrentPage())
				pageBar.append("<option value='").append(i).append(
						"' selected>-").append(i).append("-</option>");
			else
				pageBar.append("<option value='").append(i).append("'>-")
						.append(i).append("-</option>");
		}
		pageBar.append("</select>");
		return pageBar.toString();
	}
	
	//Implement other page bar you wanted
	public static String getPageBar2(Paginable page, String url){
		return "";
	}
}




Java代码 复制代码
  1. import java.io.Serializable;   
  2. import java.util.List;   
  3. import org.hibernate.Criteria;   
  4. import org.hibernate.HibernateException;   
  5. import org.hibernate.Session;   
  6. import org.hibernate.criterion.DetachedCriteria;   
  7. import org.hibernate.criterion.Projections;   
  8. import org.springframework.orm.hibernate3.HibernateCallback;   
  9. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  10. import page.Pagination;   
  11.   
  12. public abstract class AbstractDaoManager extends HibernateDaoSupport {   
  13.   
  14.     private boolean cacheQueries = false;   
  15.   
  16.     private String queryCacheRegion;   
  17.   
  18.     public void setCacheQueries(boolean cacheQueries) {   
  19.         this.cacheQueries = cacheQueries;   
  20.     }   
  21.   
  22.     public void setQueryCacheRegion(String queryCacheRegion) {   
  23.         this.queryCacheRegion = queryCacheRegion;   
  24.     }   
  25.   
  26.     public void save(final Object entity) {   
  27.         getHibernateTemplate().save(entity);   
  28.     }   
  29.   
  30.     public void persist(final Object entity) {   
  31.         getHibernateTemplate().save(entity);   
  32.     }   
  33.   
  34.     public void update(final Object entity) {   
  35.         getHibernateTemplate().update(entity);   
  36.     }   
  37.   
  38.     public void delete(final Object entity) {   
  39.         getHibernateTemplate().delete(entity);   
  40.     }   
  41.   
  42.     public Object load(final Class entity, final Serializable id) {   
  43.         return getHibernateTemplate().load(entity, id);   
  44.     }   
  45.   
  46.     public Object get(final Class entity, final Serializable id) {   
  47.         return getHibernateTemplate().get(entity, id);   
  48.     }   
  49.   
  50.     public List findAll(final Class entity) {   
  51.         return getHibernateTemplate().find("from " + entity.getName());   
  52.     }   
  53.   
  54.     public List findByNamedQuery(final String namedQuery) {   
  55.         return getHibernateTemplate().findByNamedQuery(namedQuery);   
  56.     }   
  57.   
  58.     public List findByNamedQuery(final String query, final Object parameter) {   
  59.         return getHibernateTemplate().findByNamedQuery(query, parameter);   
  60.     }   
  61.   
  62.     public List findByNamedQuery(final String query, final Object[] parameters) {   
  63.         return getHibernateTemplate().findByNamedQuery(query, parameters);   
  64.     }   
  65.   
  66.     public List find(final String query) {   
  67. 分享到:
    评论

相关推荐

Global site tag (gtag.js) - Google Analytics