package com.bjdream.common; import java.io.Serializable; import java.sql.SQLException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class BaseDao extends HibernateDaoSupport implements IBaseDao { /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#add(java.lang.Object) */ public void add(Object obj) { getHibernateTemplate().save(obj); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#update(java.lang.Object) */ public void update(Object obj) { getHibernateTemplate().update(obj); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#del(java.lang.Object) */ public void del(Object obj) { getHibernateTemplate().delete(obj); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#findAll(java.lang.Class) */ public List findAll(Class c) { return getHibernateTemplate().loadAll(c); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#findById(java.lang.Class, java.io.Serializable) */ public Object findById(Class c, Serializable id) { return getHibernateTemplate().get(c, id); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#loadById(java.lang.Class, java.io.Serializable) */ public Object loadById(Class c, Serializable id) { return getHibernateTemplate().load(c, id); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#delById(java.lang.Class, java.io.Serializable) */ public void delById(Class c, Serializable id) { del(findById(c, id)); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#bulkUpdateOrDel(java.lang.String, java.lang.Object) */ public void bulkUpdateOrDel(String hql, Object... obj) { getHibernateTemplate().bulkUpdate(hql, obj); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#queryPage(java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.Object) */ public List queryPage(final String hql, final Integer page, final Integer size, final Object... obj) { return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery(hql); if (obj != null) { for (int i = 0; i < obj.length; i++) { query.setParameter(i, obj[i]); } } if (page != null && size != null) { query.setFirstResult((page - 1) * size).setMaxResults(size); } return query.list(); } }); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#findByHql(java.lang.String, java.lang.Object) */ public List findByHql(String hql, Object... obj) { return getHibernateTemplate().find(hql, obj); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#findBySql(java.lang.String, java.lang.Object) */ public List findBySql(final String sql, final Object... obj) { return getHibernateTemplate().executeFind(new HibernateCallback() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { SQLQuery squery = session.createSQLQuery(sql); if (obj != null) { for (int i = 0; i < obj.length; i++) { squery.setParameter(i, obj); } } return squery.list(); } }); } /* (non-Javadoc) * @see com.bjdream.common.IBaseDao#findMaxMin(java.lang.String, java.lang.Object) */ public Object findMaxMin(final String hql,final Object... obj) { return getHibernateTemplate().execute(new HibernateCallback() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery(hql); if(obj!=null){ for (int i = 0; i < obj.length; i++) { query.setParameter(i, obj[i]); } } return query.uniqueResult(); } }); } }