DAO泛型:
package com.dao;
import java.io.Serializable; import java.util.List; import com.domain.Attend; import com.utils.Pagination; //泛型的接口 /*泛型真正要做的事情就是找到具体的实体类是哪个 * <T>:占位符,代表真正在运行时候的实体类(Employee,Manager,Application....) * <PK>:主键类,必须实现Serializable接口 */ public interface GenerateDao<T,PK extends Serializable> { public Integer save(T entity); public void update(T entity); public void delete(T entity); public void delete(PK id); public List<T> findAll(); public T get(PK id); // 在工具类中定义2个基本方法 // 1.通过HQL和参数返回一个对象 public Object queryObject(final String hql,final Object[] values); // 2.通过HQL和参数返回一个List public List query(final String hql,final Object[] values); // 3.实现分页的一个公共方法 public Pagination<T> getPagination(final String hql,final Object[] values,final int pageNow,final int rowCount,final int pageSize); }
实现以上接口:
package com.dao.impl;
import java.io.Serializable; import java.lang.reflect.ParameterizedType; 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.dao.GenerateDao; import com.domain.Attend; import com.utils.Pagination; /* * 泛型DAO的实现 * (1)继承泛型接口 * (2)HiberanteDaoSupport * (3)得到使用这个方法的实体类 * (4)实现具体的数据操作方法 */ public class GenerateDaoImpl<T,PK extends Serializable> extends HibernateDaoSupport implements GenerateDao<T, PK>{ private final Class<T> clazz; public GenerateDaoImpl(){ // 通过java反射机制,得到在运行时的具体的实体类 clazz = (Class<T>)((ParameterizedType)getClass() .getGenericSuperclass()) .getActualTypeArguments()[0]; } public void delete(T entity) { this.getHibernateTemplate().delete(entity); } public void delete(PK id) { this.getHibernateTemplate().delete(get(id)); } public List<T> findAll() { return this.getHibernateTemplate().find("from "+clazz.getName()); } public Integer save(T entity) { return (Integer)this.getHibernateTemplate().save(entity); } public void update(T entity) { this.getHibernateTemplate().update(entity); } public T get(PK id) { return this.getHibernateTemplate().get(clazz, id); } public List query(final String hql, final Object[] values) { return this.getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session arg0) throws HibernateException, SQLException { Query q = arg0.createQuery(hql); if(values != null){ for(int i=0;i<values.length;i++){ q.setParameter(i, values[i]); } } return q.list(); } }); } public Object queryObject(final String hql, final Object[] values) { return this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session arg0) throws HibernateException, SQLException { Query q = arg0.createQuery(hql); if(values != null){ for(int i=0;i<values.length;i++){ q.setParameter(i, values[i]); } } return q.list(); } }); } public Pagination<T> getPagination(final String hql, final Object[] values, final int pageNow, final int rowCount, final int pageSize) { return (Pagination<T>) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session arg0) throws HibernateException, SQLException { Pagination pagination = new Pagination<T>(pageSize, pageNow, rowCount); Query q = arg0.createQuery(hql); if(values != null){ for(int i=0;i<values.length;i++){ q.setParameter(i, values[i]); } } q.setFirstResult(pageSize*(pageNow-1)); q.setMaxResults(pageSize); List list = q.list(); pagination.setPageList(list); return pagination; } }); } }