当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

通用DAO

发表于: 2010-04-20   作者:czpae86   来源:转载   浏览:
摘要: package base; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.L

package base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class DAO<T extends Entity>  
{	
	private static final Log log = LogFactory.getLog(DAO.class);
	private Class<T> mytype;
	private HibernateTemplate  hibernateTemplate;
	
	public DAO(Class<T> type,HibernateTemplate temp) {
		mytype=type;
		hibernateTemplate=temp;
	}
	
	public DAO(Class<T> type) {
		mytype=type;
	}
	
    public void setHibernateTemplate(HibernateTemplate temp)
    {    
    	if(hibernateTemplate==null){this.hibernateTemplate = temp;}
    }
    
    public Class<T> getEntityClass()
	{
    	return mytype;
	}
    
    @SuppressWarnings("unchecked")
	public T loadByID(String id)
    {
    	T rt=(T) hibernateTemplate.get(mytype.getName(),id);
    	return rt;
    }
	
    public String save(T o)
    {
    	if(o==null){return "";}
    	if("".equals(o.id)){o.id=null;}    	
    	hibernateTemplate.saveOrUpdate(o);
    	return o.id;
    }
    
    public void save(final List<T> data)
    {
    	if(data==null) return;
    	for(T o : data){save(o);}
    }
    
    public void delete(String id)
    {
    	T obj=loadByID(id);
    	if(obj!=null){hibernateTemplate.delete(obj);}
    }
    
    public void delete(T o)
    {
    	if(o==null){return;}
    	hibernateTemplate.delete(o);
    }
    
    @SuppressWarnings("unchecked")
	private Object queryNum(final String hql, final Map<String, Object> args) {
		return  hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)throws HibernateException {
				if(log.isDebugEnabled()){log.debug("queryNum :" + hql);}			
				Query q = session.createQuery(hql);
				if(args!=null)
				{
					for (String k : args.keySet()){
						q.setParameter(k, args.get(k));
					}
				}
				return q.uniqueResult();//q.executeUpdate()
			}
		});
	}
    
    @SuppressWarnings("unchecked")
	public int getTotalCnt(final String hqlwhere)
    {
    	String hql = "select count(*) from " + mytype.getName() + " where " + hqlwhere;
    	if(log.isDebugEnabled()){log.debug("getTotalCnt :"+hql);}
    	Object rt=  queryNum(hql,new HashMap());	 
		return Integer.parseInt(rt.toString());
    }
    
    public int getTotalCnt(final String hqlwhere, final Map<String, Object> args)
    {
    	String hql = "select count(*) from " + mytype.getName() + " where " + hqlwhere;
    	if(log.isDebugEnabled()){log.debug("getTotalCnt :"+hql);}
    	Object rt=  queryNum(hql,args);	 
    	return Integer.parseInt(rt.toString());
   	}

    @SuppressWarnings("unchecked")
	public List<T> load(final String hqlwhere)
    {
	 	List rt=(List) hibernateTemplate.execute(
			new HibernateCallback() {
				public Object doInHibernate(Session session)throws HibernateException {	    		 	    			
					String hql = "from " + mytype.getName() + " where " + hqlwhere;
					if(log.isDebugEnabled()){log.debug("load :"+hql);}
		  		  	return  session.createQuery(hql).list();
		  		}
		});
	 	return (List<T>)rt;
    }

    @SuppressWarnings("unchecked")
	public List<T> load(final String hqlwhere,final int start,final int size)
    {
    	 List rt=(List) hibernateTemplate.execute(
    		new HibernateCallback() {
    		public Object doInHibernate(Session session)throws HibernateException {
    		 	String hql = "from " + mytype.getName() + " where " + hqlwhere;
    			if(hql.indexOf("order by")<0) hql=hql+" order by lastModifiedDate DESC";
    			if(log.isDebugEnabled()){log.debug("load :"+hql);}
      		    Query q=session.createQuery(hql);
      		    q.setMaxResults(size);
      		    q.setFirstResult(start);
      		    return  q.list();
      		}
   		});
    	return (List<T>)rt;
    }
    
    @SuppressWarnings("unchecked")
	public List<T> load(final String hqlwhere,final Map<String, Object> args,final int start,final int size)
    {
    	 List rt=(List) hibernateTemplate.execute(
    		new HibernateCallback() {
	    		public Object doInHibernate(Session session)throws HibernateException {    	    		 	    			
	    			String hql = "from " + mytype.getName() + " where " + hqlwhere;
	    			if(hql.indexOf("order by")<0) hql=hql+" order by lastModifiedDate DESC";
	    			if(log.isDebugEnabled()){log.debug("load :"+hql);}
	      		    Query q=session.createQuery(hql);
	      		    q.setMaxResults(size);
	      		    q.setFirstResult(start);
	      		  	for (String k : args.keySet()){
	      		  		q.setParameter(k, args.get(k));
	      		  	}
	      		  	return q.list();
	      		}
	   		});
    	 return (List<T>)rt;
    }

    @SuppressWarnings("unchecked")
	public List<T> load(final String hqlwhere, final Map<String, Object> args) {
		 List rt=(List)hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
				String hql = "from " + mytype.getName() + " where " + hqlwhere;
				if(log.isDebugEnabled())	log.debug("load :" + hql);
				Query q = session.createQuery(hql);
				for (String k : args.keySet()){
					q.setParameter(k, args.get(k));
				}
				return q.list();
			}
		});
		return (List<T>)rt;
	}
	   
    @SuppressWarnings("unchecked")
	public Object execute(final String hql, final Map<String, Object> args) {
		return  hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)throws HibernateException {
				if(log.isDebugEnabled()) log.debug("execute :" + hql);				
				Query q = session.createQuery(hql);
				if(args!=null)
				{
					for (String k : args.keySet()){
						q.setParameter(k, args.get(k));
					}
				}
				return q.executeUpdate();
			}
		});
	}
	    
    @SuppressWarnings("unchecked")
	public void delete(List<T> list) {
    	List<String> ids=new ArrayList<String>();    
		for (int i=0 ; i<list.size();i++){
		  T data=list.get(i);
		  if(data!=null&&data.id!=null) ids.add(data.id);
		  //delete(list.get(i));
		}
		int j=0;
		if(ids.size()<1) return;
		String[] tp=new String[ids.size()];	  
		for(String id :ids)
		{
			tp[j++]=new String(id);
		}
		deleteByIds(tp);
	}
    
    @SuppressWarnings("unchecked")
	public void deleteByIds(String[] ids) {		
		  if(ids.length<1) return;
		  StringBuilder sb=new StringBuilder();
		  sb.append("id in (");
		  for(int j=0;j< ids.length;j++)
		  {
			  sb.append("'"+ids[j]+"'");
			  if(j<(ids.length-1))  sb.append(",");
		  }
		  sb.append(")");
		  execute("delete "+mytype.getName()+" where "+sb.toString(),null);
	}
    
    public void deleteByIds(String ids){
    	if(ids==null||ids.equals("")) return;
    	String[] idArr = ids.split(",");
    	StringBuilder sb=new StringBuilder();
		sb.append("id in (");
		for(int j=0;j< idArr.length;j++)
		{
			sb.append("'"+idArr[j]+"'");
		   if(j<(idArr.length-1))  sb.append(",");
		}
		sb.append(")");
		execute("delete "+mytype.getName()+" where "+sb.toString(),null);
    }
	@SuppressWarnings("unchecked")
	public void deleteAll() {
		execute("delete "+mytype.getName(),null);
			
	}
	
	@SuppressWarnings("unchecked")
	public List<T> loadAll(){		
		return hibernateTemplate.loadAll(mytype);
	}
	
	@SuppressWarnings("unchecked")
	public int getTotal(){
		List<T> list = hibernateTemplate.loadAll(mytype);
		return list.size();
	}
}
 

通用DAO

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号