dao封装hql查询list_1

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();
			}
			
		});
		
	}
}

 写好hql,加入map参数,就可以查询泛型POJO列表

posted @ 2014-04-17 09:52  石湖山庄  阅读(175)  评论(0编辑  收藏  举报