大约在一年前,看了 robbin 关于Hibernate中DAO分页方案的实现,于是整理了一下,又做了一些扩展,做了一个BaseDAO,这两天看到DAO中都在讨论DAO怎么设计,贡献出来,大家一块讨论一下了,我自己也正在做优化和改进工作

java 代码
  1. package com.***.core.dao.hibernate.controller;   
  2.   
  3. import java.io.Serializable;   
  4. import java.sql.Connection;   
  5. import java.sql.SQLException;   
  6. import java.util.Collection;   
  7. import java.util.Iterator;   
  8. import java.util.List;   
  9.   
  10. import org.apache.commons.beanutils.PropertyUtils;   
  11. import org.apache.commons.lang.StringUtils;   
  12. import org.hibernate.Criteria;   
  13. import org.hibernate.HibernateException;   
  14. import org.hibernate.LockMode;   
  15. import org.hibernate.Query;   
  16. import org.hibernate.Session;   
  17. import org.hibernate.criterion.CriteriaSpecification;   
  18. import org.hibernate.criterion.DetachedCriteria;   
  19. import org.hibernate.criterion.Projections;   
  20. import org.hibernate.criterion.Restrictions;   
  21. import org.springframework.orm.hibernate3.HibernateCallback;   
  22. import org.springframework.orm.hibernate3.HibernateTemplate;   
  23. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  24. import org.springframework.util.Assert;   
  25.   
  26. import com.***.core.dao.hibernate.iface.IBaseHibernateDao;   
  27. import com.***.core.util.exception.DBException;   
  28. import com.***.core.util.helper.PaginationSupport;   
  29. import com.***.core.util.helper.StringUtil;   
  30.   
  31. public class BaseHibernateDao extends HibernateDaoSupport implements IBaseHibernateDao {   
  32.     /**  
  33.      * ============================================================================  
  34.      * 取得记录集数目  
  35.      * ===========================================================================  
  36.      */  
  37.     public int getCount(String queryName) throws DBException {   
  38.   
  39.         return this.getCount(queryName, (String[]) null, (Object[]) null);   
  40.     }   
  41.   
  42.     public int getCount(String queryName, String param, Object value)   
  43.             throws DBException {   
  44.   
  45.         return this.getCount(queryName, new String[] { param },   
  46.                 new Object[] { value });   
  47.     }   
  48.   
  49.     public int getCount(String queryName, String[] params, Object[] values)   
  50.             throws DBException {   
  51.         int result = 0;   
  52.   
  53.         try {   
  54.        
  55.             result = ((Long) this.getHibernateTemplate()   
  56.                     .findByNamedQueryAndNamedParam(queryName, params, values)   
  57.                     .iterator().next()).intValue();   
  58.             return result;   
  59.         } catch (Exception ex) {   
  60.             return result;   
  61.         }   
  62.   
  63.     }   
  64.   
  65.     public int getCount(final DetachedCriteria detachedCriteria)   
  66.             throws DBException {   
  67.         Integer count = ((Long) getHibernateTemplate().execute(   
  68.                 new HibernateCallback() {   
  69.                     public Object doInHibernate(Session session)   
  70.                             throws HibernateException {   
  71.                         Criteria criteria = detachedCriteria   
  72.                                 .getExecutableCriteria(session);   
  73.                         return criteria.setProjection(Projections.rowCount())   
  74.                                 .uniqueResult();   
  75.                     }   
  76.                 }, true)).intValue();   
  77.         return count.intValue();   
  78.     }   
  79.   
  80.     /**  
  81.      * =========================================================================  
  82.      * 取得一页数据  
  83.      *   
  84.      * =========================================================================  
  85.      */  
  86.   
  87.     // ---------------------------------------------------------------------   
  88.     // 无参数取得一页数据   
  89.     // --------------------------------------------------------------------   
  90.     public PaginationSupport getPage(String queryName) throws DBException {   
  91.         return getPage(queryName, (String[]) null, (Object[]) null,   
  92.                 PaginationSupport.PAGESIZE, 0);   
  93.     }   
  94.   
  95.     public PaginationSupport getPage(String queryName, int startIndex)   
  96.             throws DBException {   
  97.         return getPage(queryName, (String[]) null, (Object[]) null,   
  98.                 PaginationSupport.PAGESIZE, startIndex);   
  99.     }   
  100.   
  101.     public PaginationSupport getPage(String queryName, int pageSize,   
  102.             int startIndex) throws DBException {   
  103.         return getPage(queryName, (String[]) null, (Object[]) null, pageSize,   
  104.                 startIndex);   
  105.     }   
  106.   
  107.        
  108.   
  109.        
  110.        
  111.   
  112.        
  113.        
  114.     // ---------------------------------------------------------------------------   
  115.     // 使用带一个参数的HQL取得一页数据   
  116.     // ---------------------------------------------------------------------------   
  117.     public PaginationSupport getPage(String queryName, String param,   
  118.             Object value) throws DBException {   
  119.         return getPage(queryName, new String[] { param },   
  120.                 new Object[] { value }, PaginationSupport.PAGESIZE, 0);   
  121.     }   
  122.   
  123.   
  124.     public PaginationSupport getPage(String queryName, String param,   
  125.             Object value, int startIndex) throws DBException {   
  126.         return getPage(queryName, new String[] { param },   
  127.                 new Object[] { value }, PaginationSupport.PAGESIZE, startIndex);   
  128.     }   
  129.   
  130.        
  131.     public PaginationSupport getPage(String queryName, String param,   
  132.             Object value, int pageSize, int startIndex) throws DBException {   
  133.   
  134.         return getPage(queryName, new String[] { param },   
  135.                 new Object[] { value }, pageSize, startIndex);   
  136.     }   
  137.   
  138.        
  139.   
  140.     // ------------------------------------------------------------------------------   
  141.     // 使用带有一系列参数的HQL取得一页数据   
  142.     // .------------------------------------------------------------------------------   
  143.     public PaginationSupport getPage(String queryName, String[] params,   
  144.             Object[] values) throws DBException {   
  145.         return getPage(queryName, params, values, PaginationSupport.PAGESIZE, 0);   
  146.     }   
  147.   
  148.        
  149.     public PaginationSupport getPage(String queryName, String[] params,   
  150.             Object[] values, int startIndex) throws DBException {   
  151.         return getPage(queryName, params, values, PaginationSupport.PAGESIZE,   
  152.                 startIndex);   
  153.     }   
  154.   
  155.        
  156.   
  157.     public PaginationSupport getPage(final String queryName,   
  158.             final String[] params, final Object[] values, final int pageSize,   
  159.             final int startIndex) throws DBException {   
  160.         if (params != null && values != null && params.length != values.length) {   
  161.             throw new IllegalArgumentException(   
  162.                     "Length of paramNames array must match length of values array");   
  163.         }   
  164.   
  165.         return (PaginationSupport) getHibernateTemplate().execute(   
  166.                 new HibernateCallback() {   
  167.                     public Object doInHibernate(Session session)   
  168.                             throws HibernateException {   
  169.                         StringUtil stringUtil = new StringUtil();   
  170.   
  171.                         Query queryObject = session.getNamedQuery(queryName);   
  172.                            
  173.                         String hql = stringUtil.createCountHQL(queryObject   
  174.                                 .getQueryString());   
  175.                         Query countQuery = session.createQuery(hql);   
  176.                         if (values != null) {   
  177.                             for (int i = 0; i < values.length; i++) {   
  178.                                 queryObject.setParameter(params[i], values[i]);   
  179.                                 countQuery.setParameter(params[i], values[i]);   
  180.                             }   
  181.                         }   
  182.                         queryObject.setFirstResult(startIndex).setMaxResults(   
  183.                                 pageSize);   
  184.                         int totalCount = ((Long) countQuery.list()   
  185.                                 .iterator().next()).intValue();   
  186.                         List items = queryObject.list();   
  187.                         PaginationSupport ps = new PaginationSupport(items,   
  188.                                 totalCount, pageSize, startIndex);   
  189.                         return ps;   
  190.                     }   
  191.                 });   
  192.     }   
  193.     /**  
  194.     *-----------------------------------------------------------  
  195.     *无参数名HQL操作  
  196.     *-------------------------------------------------------------  
  197.     */  
  198.     public int getCont(String queryName, Object value) throws DBException {   
  199.         // TODO Auto-generated method stub   
  200.       return this.getCount(queryName, new Object[]{value});   
  201.     }   
  202.   
  203.     public int getCount(String queryName, Object[] values) throws DBException {   
  204.         // TODO Auto-generated method stub   
  205.         int result = 0;   
  206.   
  207.         try {   
  208.             result = ((Long) this.getHibernateTemplate().findByNamedQuery(queryName, values)   
  209.                     .iterator().next()).intValue();   
  210.   
  211.         } catch (DBException ex) {   
  212.             ex.printStackTrace();   
  213.         }   
  214.         return result;   
  215.     }   
  216.   
  217.     public PaginationSupport getPage(String queryName, Object value) throws DBException {   
  218.         // TODO Auto-generated method stub   
  219.         return this.getPage(queryName,new Object[] {value},PaginationSupport.PAGESIZE,0);   
  220.     }   
  221.   
  222.     public PaginationSupport getPage(String queryName, Object[] values) throws DBException {   
  223.         // TODO Auto-generated method stub   
  224.         return this.getPage(queryName,values,PaginationSupport.PAGESIZE,0);   
  225.     }   
  226.   
  227.     public PaginationSupport getPage(String queryName, Object value, int startIndex) throws DBException {   
  228.         // TODO Auto-generated method stub   
  229.         return this.getPage(queryName,new Object[]{value},PaginationSupport.PAGESIZE,startIndex);   
  230.     }   
  231.   
  232.     public PaginationSupport getPage(String queryName, Object value, int pageSize, int startIndex) throws DBException {   
  233.         // TODO Auto-generated method stub   
  234.         return this.getPage(queryName,new Object[]{value},pageSize,startIndex);   
  235.     }   
  236.   
  237.     public PaginationSupport getPage(String queryName, Object[] values, int startIndex) throws DBException {   
  238.         // TODO Auto-generated method stub   
  239.         return this.getPage(queryName,values,PaginationSupport.PAGESIZE,startIndex);   
  240.     }   
  241.   
  242.     public PaginationSupport getPage(final String queryName,final  Object[] values,final  int pageSize,final  int startIndex) throws DBException {   
  243.         // TODO Auto-generated method stub   
  244.            
  245.         return (PaginationSupport) getHibernateTemplate().execute(   
  246.                 new HibernateCallback() {   
  247.                     public Object doInHibernate(Session session)   
  248.                             throws HibernateException {   
  249.                         StringUtil stringUtil = new StringUtil();   
  250.                         Query queryObject = session.getNamedQuery(queryName);   
  251.                         String hql = stringUtil.createCountHQL(queryObject   
  252.                                 .getQueryString());   
  253.                         Query countQuery = session.createQuery(hql);   
  254.                         if (values != null) {   
  255.                             for (int i = 0; i < values.length; i++) {   
  256.                                 queryObject.setParameter(i, values[i]);   
  257.                                 countQuery.setParameter(i, values[i]);   
  258.                             }   
  259.                         }   
  260.                         queryObject.setFirstResult(startIndex).setMaxResults(   
  261.                                 pageSize);   
  262.                         int totalCount = ((Long) countQuery.list()   
  263.                                 .iterator().next()).intValue();   
  264.                         List items = queryObject.list();   
  265.                         PaginationSupport ps = new PaginationSupport(items,   
  266.                                 totalCount, pageSize, startIndex);   
  267.                         return ps;   
  268.                     }   
  269.                 });   
  270.     }   
  271.     // -------------------------------------------------------------------------   
  272.     // 使用DetachedCriteria取得一页数据,条件包装在DetachedCriteria中   
  273.     // -----------------------------------------------------------------------   
  274.     public PaginationSupport getPage(DetachedCriteria detachedCriteria)   
  275.             throws DBException {   
  276.         return getPage(detachedCriteria, PaginationSupport.PAGESIZE, 0);   
  277.     }   
  278.   
  279.     public PaginationSupport getPage(DetachedCriteria detachedCriteria,   
  280.             int startIndex) throws DBException {   
  281.         return getPage(detachedCriteria, PaginationSupport.PAGESIZE, startIndex);   
  282.     }   
  283.   
  284.     public PaginationSupport getPage(final DetachedCriteria detachedCriteria,   
  285.             final int pageSize, final int startIndex) throws DBException {   
  286.         return (PaginationSupport) getHibernateTemplate().execute(   
  287.                 new HibernateCallback() {   
  288.                     public Object doInHibernate(Session session)   
  289.                             throws HibernateException {   
  290.                         Criteria criteria = detachedCriteria   
  291.                                 .getExecutableCriteria(session);   
  292.                         int totalCount = ((Long) criteria.setProjection(   
  293.                                 Projections.rowCount()).uniqueResult())   
  294.                                 .intValue();   
  295.                         criteria.setProjection(null);   
  296.                         criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);   
  297.                         List items = criteria.setFirstResult(startIndex)   
  298.                                 .setMaxResults(pageSize).list();   
  299.                         PaginationSupport ps = new PaginationSupport(items,   
  300.                                 totalCount, pageSize, startIndex);   
  301.                         return ps;   
  302.                     }   
  303.                 }, true);   
  304.   
  305.     }   
  306.   
  307.     // -------------------------------------------------------------------------   
  308.     // 取得所有数据。(根据条件)   
  309.     // -------------------------------------------------------------------------   
  310.     public List getAll(final DetachedCriteria detachedCriteria)   
  311.             throws DBException {   
  312.         return (List) getHibernateTemplate().execute(new HibernateCallback() {   
  313.             public Object doInHibernate(Session session)   
  314.                     throws HibernateException {   
  315.                 Criteria criteria = detachedCriteria   
  316.                         .getExecutableCriteria(session);   
  317.                 return criteria.list();   
  318.             }   
  319.         }, true);   
  320.     }   
  321.   
  322.     public List getAll(String queryName) throws DBException {   
  323.         return this.getHibernateTemplate().findByNamedQuery(queryName);   
  324.     }   
  325.   
  326.     public List getAll(String queryName, String param, Object value)   
  327.             throws DBException {   
  328.         return this.getHibernateTemplate().findByNamedQueryAndNamedParam(   
  329.                 queryName, param, value);   
  330.     }   
  331.   
  332.     public List getAll(String queryName, String[] params, Object[] values)   
  333.             throws DBException {   
  334.         return this.getHibernateTemplate().findByNamedQueryAndNamedParam(   
  335.                 queryName, params, values);   
  336.     }   
  337.   
  338.     public List getAll(String queryName, Object value) throws DBException {   
  339.         return this.getAll(queryName, new Object[] { value });   
  340.     }   
  341.   
  342.     public List getAll(String queryName, Object[] values) throws DBException {   
  343.         return this.getHibernateTemplate().findByNamedQuery(queryName, values);   
  344.     }   
  345.   
  346.     /**  
  347.      * ============================================================================  
  348.      * 数据记录操作 包括新增,修改,删除数据  
  349.      * ============================================================================  
  350.      */  
  351.   
  352.   &nbs
评论
xly_971223 2007-04-22   回复
这就完了? 没看到有什么设计啊
而且IBaseHibernateDao 接口是针对hibernate的 ,不具备通用性
rainlife 2007-04-21   回复
把代码格式化一下就更好了,呵呵
发表评论

该博客是同时发布到论坛的,无法评论在论坛已被锁定的帖子

ecsun
搜索本博客
我的相册
959d6764-20ee-3f5d-8e7d-62b829f243a0-thumb
Faceye基础版(开源)新建标签页
共 24 张
最近加入圈子
存档
最新评论