/** * */ package com.wolfgang.dao; 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; import com.wolfgang.VO.PageVO; /** * @author Administrator * */ public class BaseDaoImp extends HibernateDaoSupport implements IBaseDao { public Object get(Integer id) { return getHibernateTemplate().get(Object.class, id); } /* * (non-Javadoc) * * @see com.wolfgang.dao.IBaseDao#save(java.lang.Object) */ public void save(Object obj) { getHibernateTemplate().save(obj); } /* * (non-Javadoc) * * @see com.wolfgang.dao.IBaseDao#update(java.lang.Object) */ public void update(Object obj) { getHibernateTemplate().update(obj); } /* * (non-Javadoc) * * @see com.wolfgang.dao.IBaseDao#delete(java.lang.Object) */ public void delete(Object obj) { getHibernateTemplate().delete(obj); } /* * (non-Javadoc) * * @see com.wolfgang.dao.IBaseDao#findById(int) */ public <T> T findById(Class<T> className, int id) { return (T) getHibernateTemplate().get(className, id); } /* * (non-Javadoc) * * @see com.wolfgang.dao.IBaseDao#getAll(java.lang.Class) */ public <T> List<T> getAll(Class<T> className) { return getHibernateTemplate().loadAll(className); } /* * (non-Javadoc) * * @see com.wolfgang.dao.IBaseDao#findPage(java.lang.String, int, int, * java.lang.Object[]) */ public PageVO findPage(final String hql, int offset, int pageSize, final Object... params) { final String totalHql = getTotalHql(hql); Long total = (Long) getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery(totalHql); return query.uniqueResult(); } }); List data = (List) getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery(hql); if (params != null) { for (int i = 0; i < params.length; i++) { query.setParameter(i, params[i]); } } return query.list(); } }); PageVO pageVO = new PageVO(); pageVO.setTotal(total); pageVO.setData(data); return pageVO; } private String getTotalHql(String hql) { int index = hql.indexOf(hql); if (index == -1) { return null; } else { return "select count(*)" + hql.substring(index); } } }