国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
應(yīng)用Hibernate3的DetachedCriteria實(shí)現(xiàn)分頁查詢
Hibernate3提供了DetachedCriteria,使得我們可以在Web層構(gòu)造detachedCriteria,然后調(diào)用業(yè)務(wù)層Bean,進(jìn)行動(dòng)態(tài)條件查詢,根據(jù)這一功能,我設(shè)計(jì)了通用的抽象Bean基類和分頁類支持,代碼來自于Quake Wang的javaeye-core包的相應(yīng)類,然后又做了很多修改。

分頁支持類:

Java代碼
 
  1. package com.iteye.common.util;   
  2.   
  3. import java.util.List;   
  4.   
  5. public class PaginationSupport {   
  6.   
  7.     public final static int PAGESIZE = 30;   
  8.   
  9.     private int pageSize = PAGESIZE;   
  10.   
  11.     private List items;   
  12.   
  13.     private int totalCount;   
  14.   
  15.     private int[] indexes = new int[0];   
  16.   
  17.     private int startIndex = 0;   
  18.   
  19.     public PaginationSupport(List items, int totalCount) {   
  20.         setPageSize(PAGESIZE);   
  21.                 setTotalCount(totalCount);   
  22.         setItems(items);           
  23.         setStartIndex(0);   
  24.     }   
  25.   
  26.     public PaginationSupport(List items, int totalCount, int startIndex) {   
  27.                 setPageSize(PAGESIZE);   
  28.         setTotalCount(totalCount);   
  29.         setItems(items);           
  30.         setStartIndex(startIndex);   
  31.     }   
  32.   
  33.     public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {   
  34.                 setPageSize(pageSize);   
  35.         setTotalCount(totalCount);   
  36.         setItems(items);   
  37.         setStartIndex(startIndex);   
  38.     }   
  39.   
  40.     public List getItems() {   
  41.         return items;   
  42.     }   
  43.   
  44.     public void setItems(List items) {   
  45.         this.items = items;   
  46.     }   
  47.   
  48.     public int getPageSize() {   
  49.         return pageSize;   
  50.     }   
  51.   
  52.     public void setPageSize(int pageSize) {   
  53.         this.pageSize = pageSize;   
  54.     }   
  55.   
  56.     public int getTotalCount() {   
  57.         return totalCount;   
  58.     }   
  59.   
  60.     public void setTotalCount(int totalCount) {   
  61.         if (totalCount > 0) {   
  62.             this.totalCount = totalCount;   
  63.             int count = totalCount / pageSize;   
  64.             if (totalCount % pageSize > 0)   
  65.                 count++;   
  66.             indexes = new int[count];   
  67.             for (int i = 0; i < count; i++) {   
  68.                 indexes[i] = pageSize * i;   
  69.             }   
  70.         } else {   
  71.             this.totalCount = 0;   
  72.         }   
  73.     }   
  74.   
  75.     public int[] getIndexes() {   
  76.         return indexes;   
  77.     }   
  78.   
  79.     public void setIndexes(int[] indexes) {   
  80.         this.indexes = indexes;   
  81.     }   
  82.   
  83.     public int getStartIndex() {   
  84.         return startIndex;   
  85.     }   
  86.   
  87.     public void setStartIndex(int startIndex) {   
  88.         if (totalCount <= 0)   
  89.             this.startIndex = 0;   
  90.         else if (startIndex >= totalCount)   
  91.             this.startIndex = indexes[indexes.length - 1];   
  92.         else if (startIndex < 0)   
  93.             this.startIndex = 0;   
  94.         else {   
  95.             this.startIndex = indexes[startIndex / pageSize];   
  96.         }   
  97.     }   
  98.   
  99.     public int getNextIndex() {   
  100.         int nextIndex = getStartIndex() + pageSize;   
  101.         if (nextIndex >= totalCount)   
  102.             return getStartIndex();   
  103.         else  
  104.             return nextIndex;   
  105.     }   
  106.   
  107.     public int getPreviousIndex() {   
  108.         int previousIndex = getStartIndex() - pageSize;   
  109.         if (previousIndex < 0)   
  110.             return 0;   
  111.         else  
  112.             return previousIndex;   
  113.     }   
  114.   
  115. }  


抽象業(yè)務(wù)類
Java代碼
 
  1. /**  
  2.  * Created on 2005-7-12  
  3.  */  
  4. package com.iteye.common.business;   
  5.   
  6. import java.io.Serializable;   
  7. import java.util.List;   
  8.   
  9. import org.hibernate.Criteria;   
  10. import org.hibernate.HibernateException;   
  11. import org.hibernate.Session;   
  12. import org.hibernate.criterion.DetachedCriteria;   
  13. import org.hibernate.criterion.Projections;   
  14. import org.springframework.orm.hibernate3.HibernateCallback;   
  15. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  16.   
  17. import com.iteye.common.util.PaginationSupport;   
  18.   
  19. public abstract class AbstractManager extends HibernateDaoSupport {   
  20.   
  21.     private boolean cacheQueries = false;   
  22.   
  23.     private String queryCacheRegion;   
  24.   
  25.     public void setCacheQueries(boolean cacheQueries) {   
  26.         this.cacheQueries = cacheQueries;   
  27.     }   
  28.   
  29.     public void setQueryCacheRegion(String queryCacheRegion) {   
  30.         this.queryCacheRegion = queryCacheRegion;   
  31.     }   
  32.   
  33.     public void save(final Object entity) {   
  34.         getHibernateTemplate().save(entity);   
  35.     }   
  36.   
  37.     public void persist(final Object entity) {   
  38.         getHibernateTemplate().save(entity);   
  39.     }   
  40.   
  41.     public void update(final Object entity) {   
  42.         getHibernateTemplate().update(entity);   
  43.     }   
  44.   
  45.     public void delete(final Object entity) {   
  46.         getHibernateTemplate().delete(entity);   
  47.     }   
  48.   
  49.     public Object load(final Class entity, final Serializable id) {   
  50.         return getHibernateTemplate().load(entity, id);   
  51.     }   
  52.   
  53.     public Object get(final Class entity, final Serializable id) {   
  54.         return getHibernateTemplate().get(entity, id);   
  55.     }   
  56.   
  57.     public List findAll(final Class entity) {   
  58.         return getHibernateTemplate().find("from " + entity.getName());   
  59.     }   
  60.   
  61.     public List findByNamedQuery(final String namedQuery) {   
  62.         return getHibernateTemplate().findByNamedQuery(namedQuery);   
  63.     }   
  64.   
  65.     public List findByNamedQuery(final String query, final Object parameter) {   
  66.         return getHibernateTemplate().findByNamedQuery(query, parameter);   
  67.     }   
  68.   
  69.     public List findByNamedQuery(final String query, final Object[] parameters) {   
  70.         return getHibernateTemplate().findByNamedQuery(query, parameters);   
  71.     }   
  72.   
  73.     public List find(final String query) {   
  74.         return getHibernateTemplate().find(query);   
  75.     }   
  76.   
  77.     public List find(final String query, final Object parameter) {   
  78.         return getHibernateTemplate().find(query, parameter);   
  79.     }   
  80.   
  81.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria) {   
  82.         return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, 0);   
  83.     }   
  84.   
  85.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int startIndex) {   
  86.         return findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);   
  87.     }   
  88.   
  89.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,   
  90.             final int startIndex) {   
  91.         return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {   
  92.             public Object doInHibernate(Session session) throws HibernateException {   
  93.                 Criteria criteria = detachedCriteria.getExecutableCriteria(session);   
  94.                 int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();   
  95.                 criteria.setProjection(null);   
  96.                 List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();   
  97.                 PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);   
  98.                 return ps;   
  99.             }   
  100.         }, true);   
  101.     }   
  102.   
  103.     public List findAllByCriteria(final DetachedCriteria detachedCriteria) {   
  104.         return (List) getHibernateTemplate().execute(new HibernateCallback() {   
  105.             public Object doInHibernate(Session session) throws HibernateException {   
  106.                 Criteria criteria = detachedCriteria.getExecutableCriteria(session);   
  107.                 return criteria.list();   
  108.             }   
  109.         }, true);   
  110.     }   
  111.   
  112.     public int getCountByCriteria(final DetachedCriteria detachedCriteria) {   
  113.         Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {   
  114.             public Object doInHibernate(Session session) throws HibernateException {   
  115.                 Criteria criteria = detachedCriteria.getExecutableCriteria(session);   
  116.                 return criteria.setProjection(Projections.rowCount()).uniqueResult();   
  117.             }   
  118.         }, true);   
  119.         return count.intValue();   
  120.     }   
  121. }  



用戶在web層構(gòu)造查詢條件detachedCriteria,和可選的startIndex,調(diào)用業(yè)務(wù)bean的相應(yīng)findByCriteria方法,返回一個(gè)PaginationSupport的實(shí)例ps。

ps.getItems()得到已分頁好的結(jié)果集
ps.getIndexes()得到分頁索引的數(shù)組
ps.getTotalCount()得到總結(jié)果數(shù)
ps.getStartIndex()當(dāng)前分頁索引
ps.getNextIndex()下一頁索引
ps.getPreviousIndex()上一頁索引
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
dbutils通用類,dbutils分頁查詢
自己寫的查詢底層方法:查詢
SSM框架——實(shí)現(xiàn)分頁和搜索分頁
java分頁計(jì)算
JAVA MYSQL做分頁
Spring MVC MongoDB 分頁例子下載
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服