私の万能daoはソースコードを設計して、みんなによけいに指導してもらいます!


まず、まずクラス図を見てみましょう.
http://lym6520.iteye.com/admin/blogs/421504 )
Query層を補充してhqlまたはsqlを保存し、以下のようにします.
我的万能dao 设计源码,请大家多多指教!_第1张图片
IBaseDaoを見てみましょうJAvaファイル:

package lym.dao.base.hibernate;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import lym.dao.obj.AbstractHandleObj;

import org.hibernate.type.Type;

/**
 *   Dao  ,       
 * @author    
 * @version v1.1 2009/07/17
 *
 */
public interface IBaseDao {

	/**
	 *          ,                ,
	 *        , top 0   
	 */
	public void setMaxResults(int top);
	
	/**
	 *   AbstractHandleObj  
	 * @param hobj   AbstractHandleObj      
	 * @return Serializable   
	 */
	public Serializable save(AbstractHandleObj hobj);
	
	/**
	 *   AbstractHandleObj  
	 * @param hobj   AbstractHandleObj        
	 */
	public void update(AbstractHandleObj hobj);
	
	/**
	 *      AbstractHandleObj  
	 * @param hobj   AbstractHandleObj      
	 */
	public void saveOrUpdate(AbstractHandleObj hobj);
	
	/**
	 *          Collection<AbstractHandleObj>
	 *       (      )      ,     
	 * @param hobjList   AbstractHandleObj        
	 */
	public void saveOrUpdateAll(Collection<AbstractHandleObj> hobjList);
	
	/**
	 *   AbstractHandleObj  
	 * @param hobj   AbstractHandleObj      
	 */
	public void delete(AbstractHandleObj hobj);
	
	/**
	 *   Collection<AbstractHandleObj>    
	 * @param hobjList   AbstractHandleObj        
	 */
	public void deleteAll(Collection<AbstractHandleObj> hobjList);
	
	/**
	 *          AbstractHandleObj   
	 * @param entityName   AbstractHandleObj        ( :    )
	 * @param id     
	 * @return AbstractHandleObj
	 */
	public AbstractHandleObj findByID(String entityName, String id);
	
	/**
	 *   hql    AbstractHandleObj,  List<AbstractHandleObj>  
	 * @param hql HQL    
	 * @return List<AbstractHandleObj>
	 */
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuery(String hql);
	
	/**
	 *   hql    AbstractHandleObj,  List<AbstractHandleObj>  
	 * @param hql HQL    
	 * @param obj     ,     ?
	 * @return List<AbstractHandleObj>
	 */
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuery(String hql, Object obj);
	
	/**
	 *   hql    AbstractHandleObj,  List<AbstractHandleObj>  
	 * @param hql HQL    
	 * @param objs       ,        ?
	 * @return List<AbstractHandleObj>
	 */
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuery(String hql, Object[] objs);
	
	/**
	 *   hql       ,  List<Object[]>  
	 * @param hql HQL    
	 * @return List<Object[]>
	 */
	public List<Object[]> findColsByHqlQuery(String hql);
	
	/**
	 *   hql       ,  List<Object[]>  
	 * @param hql HQL    
	 * @param obj     ,     ?
	 * @return List<Object[]>
	 */
	public List<Object[]> findColsByHqlQuery(String hql, Object obj);
	
	/**
	 *   hql       ,  List<Object[]>  
	 * @param hql HQL    
	 * @param objs       ,        ?
	 * @return List<Object[]>
	 */
	public List<Object[]> findColsByHqlQuery(String hql, Object[] objs) ;
	
	/**
	 *   hql       ,  List<Object>  
	 * @param hql HQL    
	 * @return List<Object>
	 */
	public List<Object> findColByHqlQuery(String hql) ;
	
	/**
	 *   hql       ,  List<Object>  
	 * @param hql HQL    
	 * @param obj     ,     ?
	 * @return List<Object>
	 */
	public List<Object> findColByHqlQuery(String hql, Object obj);
	
	/**
	 *   hql       ,  List<Object>  
	 * @param hql HQL    
	 * @param objs       ,        ?
	 * @return List<Object>
	 */
	public List<Object> findColByHqlQuery(String hql, Object[] objs);
	
	/**
	 *   hql       ,  List<String[]>  ,    
	 * @param hql HQL    
	 * @param start          ( 0  )
	 * @param reCount         (      )
	 * @return List<Object[]>
	 */
	public List<Object[]> findColsByHqlQuerySupportPaging(String hql, int start, int reCount);
	
	/**
	 *   hql       ,  List<String[]>  ,    
	 * @param hql HQL    
	 * @param objs       ,        ?
	 * @param types          ,     objs      
	 * 			 :     Hibernate   ,  String  Hibernate.STRING,boolean  Hibernate.BOOLEAN 
	 * @param start          ( 0  )
	 * @param reCount         (      )
	 * @return List<Object[]>
	 */
	public List<Object[]> findColsByHqlQuerySupportPaging(String hql, Object[] objs,
			Type[] types, int start, int reCount);
	
	/**
	 *   hql    ,     
	 *   :select count(*) from T
	 * @param hql HQL    
	 * @return long
	 */
	public long getRecordCountByHqlQuery(String hql) ;
	
	/**
	 *   hql    ,     
	 *   :select count(*) from T where ...
	 * @param hql HQL    
	 * @param obj     ,     ?
	 * @return long
	 */
	public long getRecordCountByHqlQuery(String hql, Object obj) ;
	
	/**
	 *   hql    ,     
	 *   :select count(*) from T where ...
	 * @param hql
	 * @param objs       ,        ?
	 * @return long
	 */
	public long getRecordCountByHqlQuery(String hql, Object[] objs) ;
	
	/**
	 *     update/  delete  
	 * @param hql
	 * @return int     /    
	 */
	public int executeHqlQuery(String hql) ;
	
	/**
	 *     update/  delete  
	 * @param hql
	 * @param obj     ,     ?
	 * @return int     /    
	 */
	public int executeHqlQuery(String hql, Object obj);
	
	/**
	 *     update/  delete  
	 * @param hql
	 * @param objs       ,        ?
	 * @return int     /    
	 */
	public int executeHqlQuery(String hql, Object[] objs) ;
	
	/**
	 *   hql    AbstractHandleObj,  List<AbstractHandleObj>  ,    
	 * @param hql HQL    
	 * @param start          ( 0  )
	 * @param reCount         (      )
	 * @return  List<AbstractHandleObj>
	 */
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuerySupportPaging(String hql, 
			int start, int reCount); 
	
	/**
	 *   hql    AbstractHandleObj,  List<AbstractHandleObj>  ,    
	 * @param hql HQL    
	 * @param objs       ,        ?
	 * @param types          ,     objs      
	 * 			 :     Hibernate   ,  String  Hibernate.STRING,boolean  Hibernate.BOOLEAN 
	 * @param start          ( 0  )
	 * @param reCount         (      )
	 * @return List<AbstractHandleObj>
	 */
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuerySupportPaging(String hql, Object[] objs,
			Type[] types, int start, int reCount) ;
	
	/**
	 *   SQL  ,  sql       ,  List<String[]>  
	 * @param sql SQL    
	 * @return List<Object[]>
	 */
	public List<Object[]> findColsBySqlQuery(String sql) ;
	
	/**
	 *   SQL  ,  sql      ,  List<Object>  
	 * @param sql SQL    
	 * @return List<Object>
	 */
	public List<Object> findColBySqlQuery(String sql) ;
	
	/**
	 *   SQL  ,  sql  update/delete/    
	 * @param sql SQL  
	 * @return int
	 */
	public int executeSqlQuery(String sql) ;
	
}


実装クラスを見てjava:

package lym.dao.base.hibernate;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import lym.dao.obj.AbstractHandleObj;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;



/**
 *   Dao  ,      
 * @author    
 * @version v1.1 2009/07/17
 *
 */
public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao {
	
	
	/**
	 *          ,                ,
	 *        , top 0   
	 */
	public void setMaxResults(int top){	
		if(top > 0){
			getHibernateTemplate().setMaxResults(top);
		}		
	}
	
	public Serializable save(AbstractHandleObj hobj){
		return getHibernateTemplate().save(hobj);		
	}

	public void update(AbstractHandleObj hobj) {
		getHibernateTemplate().update(hobj);		
	
	}
	
	public void saveOrUpdate(AbstractHandleObj hobj) {
		getHibernateTemplate().saveOrUpdate(hobj);
		
	}
		
	public void saveOrUpdateAll(Collection<AbstractHandleObj> hobjList) {
		getHibernateTemplate().saveOrUpdateAll(hobjList);				
	}
	
	public void delete(AbstractHandleObj hobj) {
		getHibernateTemplate().delete(hobj);			
	}
	
	public void deleteAll(Collection<AbstractHandleObj> hobjList) {
		getHibernateTemplate().deleteAll(hobjList);		
	}
	
	public AbstractHandleObj findByID(String entityName, String id)
		{
		return (AbstractHandleObj) getHibernateTemplate().get(entityName, id);			
	}
	
	@SuppressWarnings("unchecked")	
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuery(String hql) 
		{	
		List<AbstractHandleObj> resultList = new ArrayList<AbstractHandleObj>();
		resultList = getHibernateTemplate().find(hql);
		return resultList;
	}

	@SuppressWarnings("unchecked")	
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuery(String hql, Object obj) 
		{		
		List<AbstractHandleObj> resultList = new ArrayList<AbstractHandleObj>();
		resultList = getHibernateTemplate().find(hql, obj);
		return resultList;			
	}

	@SuppressWarnings("unchecked")	
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuery(String hql, Object[] objs) 
		{		
		List<AbstractHandleObj> resultList = new ArrayList<AbstractHandleObj>();
		resultList = getHibernateTemplate().find(hql, objs);
		return resultList;		
	}	
	
	@SuppressWarnings("unchecked")	
	public List<Object[]> findColsByHqlQuery(String hql) 
		{		
		List<Object[]> resultList = getHibernateTemplate().find(hql);
		return resultList;		
	}

	@SuppressWarnings("unchecked")	
	public List<Object[]> findColsByHqlQuery(String hql, Object obj) 
		{		
		List<Object[]> resultList = getHibernateTemplate().find(hql, obj);
		return resultList;
	}

	@SuppressWarnings("unchecked")	
	public List<Object[]> findColsByHqlQuery(String hql, Object[] objs)
		{		
		List<Object[]> resultList = getHibernateTemplate().find(hql, objs);
		return resultList;		
	}
	
	@SuppressWarnings("unchecked")	
	public List<Object> findColByHqlQuery(String hql) 
		{		
		List<Object> resultList = getHibernateTemplate().find(hql);
		return resultList;
	}

	@SuppressWarnings("unchecked")	
	public List<Object> findColByHqlQuery(String hql, Object obj) 
		{		
		List<Object> resultList = getHibernateTemplate().find(hql, obj);
		return resultList;		
	}

	@SuppressWarnings("unchecked")	
	public List<Object> findColByHqlQuery(String hql, Object[] objs) 
		{		
		List<Object> resultList = getHibernateTemplate().find(hql, objs);
		return resultList;		
	}
	
	@SuppressWarnings("unchecked")	
	public List<Object[]> findColsByHqlQuerySupportPaging(final String hql,final int start, 
			final int reCount) {
		List<Object[]> resultList = getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult(start);
				query.setMaxResults(reCount);
				
				return query.list();
			}				
		});
		return resultList;		
	}
	
	@SuppressWarnings("unchecked")	
	public List<Object[]> findColsByHqlQuerySupportPaging(final String hql,
			final Object[] objs, final Type[] types, final int start, final int reCount) 
			{
		List<Object[]> resultList = getHibernateTemplate().executeFind(new HibernateCallback(){
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					/*    */
					query.setParameters(objs, types);
					query.setFirstResult(start);
					query.setMaxResults(reCount);
					
					return query.list();
				}				
			});
			return resultList;		
	}
	
	public long getRecordCountByHqlQuery(String hql) {
		long recordCount = (Long) getHibernateTemplate().find(hql).get(0);
		return recordCount;		
	}
	
	public long getRecordCountByHqlQuery(String hql, Object obj) {
		long recordCount = (Long) getHibernateTemplate().find(hql, obj).get(0);
		return recordCount;		
	}
	
	public long getRecordCountByHqlQuery(String hql, Object[] objs) {
		long recordCount = (Long) getHibernateTemplate().find(hql, objs).get(0);
		return recordCount;		
	}
	
	public int executeHqlQuery(String hql) {
		return getHibernateTemplate().bulkUpdate(hql);			
	}
	
	public int executeHqlQuery(String hql, Object obj) {
		return getHibernateTemplate().bulkUpdate(hql, obj);
	}

	public int executeHqlQuery(String hql, Object[] objs) {
		return getHibernateTemplate().bulkUpdate(hql, objs);			
	}

	@SuppressWarnings("unchecked")
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuerySupportPaging(final String hql,
			final int start, final int reCount) {
		List<AbstractHandleObj> resultList = getHibernateTemplate().executeFind(new HibernateCallback(){
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					query.setFirstResult(start);
					query.setMaxResults(reCount);
					
					return query.list();
				}				
			});
			return resultList;		
	}

	@SuppressWarnings("unchecked")
	public List<AbstractHandleObj> findAbstractHandleObjByHqlQuerySupportPaging(final String hql, final Object[] objs,
			final Type[] types, final int start, final int reCount) {
		List<AbstractHandleObj> resultList = getHibernateTemplate().executeFind(new HibernateCallback(){
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					/*    */
					query.setParameters(objs, types);
					query.setFirstResult(start);
					query.setMaxResults(reCount);
					
					return query.list();
				}				
			});
			return resultList;	
	}

	@SuppressWarnings("unchecked")	
	public List<Object[]> findColsBySqlQuery(final String sql) {		
		List<Object[]> resultList = new ArrayList<Object[]>();			
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		resultList = session.createSQLQuery(sql).list();
		session.close();
		return resultList;		
	}

	@SuppressWarnings("unchecked")	
	public List<Object> findColBySqlQuery(String sql) {		
		List<Object> resultList = new ArrayList<Object>();			
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		resultList = session.createSQLQuery(sql).list();
		session.close();
		return resultList;		
	}
	
	public int executeSqlQuery(final String sql) {		
		int result = 0;
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		result = session.createSQLQuery(sql).executeUpdate();
		session.close();
		return result;		
	}
}


抽象的なものについてお話ししますJAvaの役割は、まずコードを見て、実は抽象的なクラスで、空のクラスです!

package lym.dao.obj;

/**
 *    ,        Dao    ,
 *       (           )
 * @author    
 * @version v1.0 2009/07/05
 *
 */
public abstract class AbstractHandleObj {

}


この抽象クラスを構築する目的は、実際にはエンティティを継承したり、オブジェクトに戻って継承したりすることで、daoを通じてエンティティを操作することができます.これもこのdaoを使ってエンティティを操作する前提条件です.もちろん、受け継がなければあなたも
私たちのプロジェクトはspring+hibernate開発を使用しているので、構成すると:

   <bean id="baseDao" class="lym.dao.base.hibernate.BaseDaoImpl">
   		<!--  sessionFactory-->
 		<property name="sessionFactory" ref="sessionFactory"></property>
   </bean>

現在の欠点はネーミングパラメータのサポートだと思いますので、皆さんで補ってあげましょう.