1. 写一个基础的接口和类来做基本的操作
/** * */ package com.wolfgang.dao; import java.util.List; /** * @author Administrator * */ public interface IBaseDao { public void save(Object obj); public void update(Object obj); public void delete(Object obj); public <T> T findById(Class<T> className, int id); public <T> List<T> getAll(Class<T> className); }
/** * */ package com.wolfgang.dao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /** * @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) */ @Override public void save(Object obj) { getHibernateTemplate().save(obj); } /* (non-Javadoc) * @see com.wolfgang.dao.IBaseDao#update(java.lang.Object) */ @Override public void update(Object obj) { getHibernateTemplate().update(obj); } /* (non-Javadoc) * @see com.wolfgang.dao.IBaseDao#delete(java.lang.Object) */ @Override public void delete(Object obj) { getHibernateTemplate().delete(obj); } /* (non-Javadoc) * @see com.wolfgang.dao.IBaseDao#findById(int) */ @Override 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) */ @Override public <T> List<T> getAll(Class<T> className) { return getHibernateTemplate().loadAll(className); } }
2. 具体的接口和类都继承基础的类和接口
/** * */ package com.wolfgang.dao; /** * @author Administrator * */ public interface IPartyDao extends IBaseDao{ }
package com.wolfgang.dao; import org.springframework.stereotype.Repository; @Repository("partyDao") public class PartyDaoImpl extends BaseDaoImp implements IPartyDao { }