import java.io.Serializable; import java.sql.SQLException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /**数据仿问辅助类 * @author llg 2010-12-01 * @param <T> */ public class DaoHelper<T> extends HibernateDaoSupport { private static final String FORM_SEARCH = "from "; /**新增 * @param obj (实体对象) * @return */ @SuppressWarnings("unchecked") public T save(Object obj){ return (T) super.getHibernateTemplate().save(obj); } /**按主键删除 * @param entityClass 实体类型 * @param primaryID 主键 */ @SuppressWarnings("unchecked") public void del(Class entityClass,Serializable primaryID){ super.getHibernateTemplate().delete(findByID(entityClass, primaryID)) ; } /**修改 * @param obj * @return */ @SuppressWarnings("unchecked") public T update(Object obj){ super.getHibernateTemplate().update(obj); return (T) obj; } /**按主键查询 * @param entityClass 实体类型 * @param primaryID 主键 * @return */ @SuppressWarnings("unchecked") public T findByID(Class entityClass, Serializable primaryID) { return (T) super.getHibernateTemplate().get(entityClass, primaryID); } /**根据HQL查询单个对象 * @param hql * @param p * @return */ public T uniqueResultByHql(String hql , Object...p){ return findByHql(hql,null,null,p).get(0); } /**根据nameQuery查询单个对象 * @param methodName (要调用的方法配置名) * @param p * @return */ public T uniqueResultByNameQuery(String methodName , Object...p){ return findByNameQuery(methodName,null,null,p).get(0); } /** 查所有 * @param entityClass * @return */ @SuppressWarnings("unchecked") public List<T> findAll(Class entityClass) { return super.getHibernateTemplate().find( FORM_SEARCH + entityClass.getSimpleName()); } /**根据HQL动态增删改 * @param hql * @param p */ public void bulkExecuteByHql(String hql , Object...p ){ super.getHibernateTemplate().bulkUpdate(hql, p); } /**根据NameQuery动态增删改 * @param methodName(要调用的方法配置名) * @param p */ @SuppressWarnings("unchecked") public void bulkExecuteByNameQuery(final String methodName,final Object...p){ super.getHibernateTemplate().execute(new HibernateCallback(){ @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.getNamedQuery(methodName); setParameter(query, null, null, p); query.executeUpdate() ; return null; } }); } /**根据HQL条件查询 * @param hql * @param pageSize * @param currentPage * @param p * @return */ @SuppressWarnings("unchecked") public List<T> findByHql(final String hql,final Integer pageSize,final Integer currentPage,final Object... p) { return super.getHibernateTemplate().executeFind(new HibernateCallback<List<T>>(){ @Override public List<T> doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery(hql); setParameter(query, pageSize, currentPage, p); return query.list(); } }); } /**根据NameQuery条件查询 * @param methodName * @param pageSize * @param currentPage * @param p * @return */ @SuppressWarnings("unchecked") public List<T> findByNameQuery(final String methodName,final Integer pageSize,final Integer currentPage,final Object... p){ return super.getHibernateTemplate().executeFind(new HibernateCallback<List<T>>(){ @Override public List<T> doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.getNamedQuery(methodName) ; setParameter(query, pageSize, currentPage, p); return query.list(); } }); } /**封闭Query 参数 * @param query * @param pageSize * @param currentPage * @param p */ @SuppressWarnings("unused") private static void setParameter(Query query, Integer pageSize, Integer currentPage, Object... p) { if (pageSize != null && currentPage != null) query.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize); if (p != null) for (int i = 0; i < p.length; i++) { query.setParameter(i, p[i]); } } }