BaseDao.java
public interface BaseDao<T> { //增 void save(T t); //删 void delete(T t); //删 void delete(Serializable id); //改 void update(T t); //查 根据id查询 T getById(Serializable id); //查 符合条件的总记录数 Integer getTotalCount(DetachedCriteria dc); //查 查询分页列表数据 List<T> getPageList(DetachedCriteria dc,Integer start,Integer pageSize); }
BaseDaoImpl.java
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> { private Class clazz;//用于接收运行期泛型类型 public BaseDaoImpl() { //获得当前类型的带有泛型类型的父类 ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass(); //获得运行期的泛型类型 clazz = (Class) ptClass.getActualTypeArguments()[0]; } @Override public void save(T t) { getHibernateTemplate().save(t); } @Override public void delete(T t) { getHibernateTemplate().delete(t); } @Override //Serializable 对象序列化的接口,八大基本类型的父类 public void delete(Serializable id) { T t = this.getById(id);//先取,再删 getHibernateTemplate().delete(t); } @Override public void update(T t) { getHibernateTemplate().update(t); } @Override public T getById(Serializable id) { return (T) getHibernateTemplate().get(clazz, id); } @Override public Integer getTotalCount(DetachedCriteria dc) { //设置查询的聚合函数,总记录数 dc.setProjection(Projections.rowCount()); List<Long> list = (List<Long>) getHibernateTemplate().findByCriteria(dc); //清空之前设置的聚合函数 dc.setProjection(null); if(list!=null && list.size()>0){ Long count = list.get(0); return count.intValue(); }else{ return null; } } @Override public List<T> getPageList(DetachedCriteria dc, Integer start, Integer pageSize) { List<T> list = (List<T>) getHibernateTemplate().findByCriteria(dc, start, pageSize); return list; } }
CustomerDao.java
public interface CustomerDao extends BaseDao<Customer>{ }
CustomerDaoImpl.java
public class CustomerDaoImpl extends BaseDaoImpl<Customer> implements CustomerDao{ }