hql封装dao_2
import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.Map.Entry; 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; public class SealDao<POJO> extends HibernateDaoSupport{ /** * description:hql获取list * @param hql * @param map<String,Object> * @return List<POJO> * * */ public List<POJO> getList(final String hql,final Map<String,Object>keyValue)throws Exception{ return getHibernateTemplate().execute(new HibernateCallback<List<POJO>>(){ @Override public List<POJO> doInHibernate(Session session) throws HibernateException, SQLException { // TODO Auto-generated method stub Query query=session.createQuery(hql); for(Entry<String, Object> entry : keyValue.entrySet()){ query.setParameter(entry.getKey(),entry.getValue()); } return query.list(); } }); } /** * description:hql分页获取list * @param hql * @param map<String,Object> * @param start * @param end * @return List<POJO> * * */ public List<POJO> getListByPage(final String hql,final Map<String,Object>keyValue,final Integer start,final Integer end)throws Exception{ return getHibernateTemplate().execute(new HibernateCallback<List<POJO>>(){ @Override public List<POJO> doInHibernate(Session session) throws HibernateException, SQLException { Query query=session.createQuery(hql); for(Entry<String, Object> entry : keyValue.entrySet()){ query.setParameter(entry.getKey(),entry.getValue()); } query.setFirstResult(start); query.setMaxResults(end); return query.list(); } }); } /** * description:hql获取POJO * @param hql * @param map<String,Object> * @return POJO * * */ public POJO getOne(final String hql, final Map<String, Object> keyVal) { // TODO Auto-generated method stub return getHibernateTemplate().execute(new HibernateCallback<POJO>() { @Override public POJO doInHibernate(Session session) throws HibernateException, SQLException { // TODO Auto-generated method stub Query query = session.createQuery(hql); for(Entry<String, Object> entry : keyVal.entrySet()) { query.setParameter(entry.getKey(), entry.getValue()); } return (POJO)query.uniqueResult(); } }); } /** * description:hql更新 * @param hql * @param map<String,Object> * @return * * */ public void update(final String hql, final Map<String, Object> keyVal) { // TODO Auto-generated method stub getHibernateTemplate().execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { // TODO Auto-generated method stub Query query = session.createQuery(hql); for(Entry<String, Object> entry : keyVal.entrySet()) { query.setParameter(entry.getKey(), entry.getValue()); } query.executeUpdate(); return null; } }); } }
hql封装dao_2