想法不错,但真的能简化吗
DAO层跟具体的每张数据表都有关系的

解决方案 »

  1.   

    肯定是能够简化的:
    basedao接口IBaseDAO:
    package org.lidingzhong.util;import java.util.List;public interface IBaseDAO<T ,ID extends java.io.Serializable> {
    public void save(T transientInstance);
    public void delete(T persistentInstance);
    public T findById(ID id);
    public List<T> findByExample(T instance);
    public List<T> findByProperty(String propertyName, Object value);
    public List<T> findByHQLStr(String HQLStr); 
    public List<T> findAll();
    public T merge(T detachedInstance);
    public void attachDirty(T instance);
    public void attachClean(T instance);
    }
    接口实现类:
    package org.lidingzhong.util;import java.util.List;import org.apache.commons.logging.Log;
    import org.hibernate.LockMode;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public abstract class BaseDAOImpl<T ,ID extends java.io.Serializable> 
    extends HibernateDaoSupport implements IBaseDAO<T,ID> {

    private Log log ;

    private Class<?> type ;
    private String getTableClassName(){
    return type.getName().substring(type.getName().lastIndexOf(".")+1);
    } protected void initDao() {
    // do nothing
    } public void save(T transientInstance) {
    log.debug("saving "+getTableClassName()+" instance");
    try {
    getHibernateTemplate().save(transientInstance);
    log.debug("save successful");
    } catch (RuntimeException re) {
    log.error("save failed", re);
    throw re;
    }
    } public void delete(T persistentInstance) {
    log.debug("deleting "+getTableClassName()+" instance");
    try {
    getHibernateTemplate().delete(persistentInstance);
    log.debug("delete successful");
    } catch (RuntimeException re) {
    log.error("delete failed", re);
    throw re;
    }
    } public T findById(ID id) {
    log.debug("getting "+getTableClassName()+" instance with id: " + id);
    try {
    T instance = (T) getHibernateTemplate().get(
    type.getName(), id);
    return instance;
    } catch (RuntimeException re) {
    log.error("get failed", re);
    throw re;
    }
    } public List<T> findByExample(T instance) {
    log.debug("finding "+getTableClassName()+" instance by example");
    try {
    List<T> results = getHibernateTemplate().findByExample(instance);
    log.debug("find by example successful, result size: "
    + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error("find by example failed", re);
    throw re;
    }
    } public List<T> findByProperty(String propertyName, Object value) {
    log.debug("finding "+getTableClassName()+" instance with property: " + propertyName
    + ", value: " + value);
    try {
    String queryString = "from "+getTableClassName()+" as model where model."
    + propertyName + "= ?";
    return getHibernateTemplate().find(queryString, value);
    } catch (RuntimeException re) {
    log.error("find by property name failed", re);
    throw re;
    }
    } public List<T> findAll() {
    log.debug("finding all "+getTableClassName()+" instances");
    try {
    String queryString = "from "+getTableClassName();
    return getHibernateTemplate().find(queryString);
    } catch (RuntimeException re) {
    log.error("find all failed", re);
    throw re;
    }
    } public T merge(T detachedInstance) {
    log.debug("merging TabArticle instance");
    try {
    T result = (T) getHibernateTemplate().merge(
    detachedInstance);
    log.debug("merge successful");
    return result;
    } catch (RuntimeException re) {
    log.error("merge failed", re);
    throw re;
    }
    } public void attachDirty(T instance) {
    log.debug("attaching dirty TabArticle instance");
    try {
    getHibernateTemplate().saveOrUpdate(instance);
    log.debug("attach successful");
    } catch (RuntimeException re) {
    log.error("attach failed", re);
    throw re;
    }
    } public void attachClean(T instance) {
    log.debug("attaching clean TabArticle instance");
    try {
    getHibernateTemplate().lock(instance, LockMode.NONE);
    log.debug("attach successful");
    } catch (RuntimeException re) {
    log.error("attach failed", re);
    throw re;
    }
    } public List<T> findByHQLStr(String HQLStr) {
    log.debug("finding "+getTableClassName()+" instance with HQL: " + HQLStr) ;
    try {
    return getHibernateTemplate().find(HQLStr);
    } catch (RuntimeException re) {
    log.error("find by property name failed", re);
    throw re;
    }
    }

    public void setLog(Log log) {
    this.log = log;
    } public void setType(Class<?> type) {
    this.type = type;
    }
    }
      

  2.   

    某张表:
    public class AnyTableDAO extends BaseDAOImpl<AnyTable ,Integer> {
    private static final Log log = LogFactory.getLog(AnyTableDAO.class); protected void initDao() {
    super.setLog(log);
    super.setType(AnyTable.class); }
    }
      

  3.   

    所谓泛型就是:将算法与其作用的数据结构分离,并将后者尽可能泛化,最大限度地实现算法重用。
    所以可以实现对DAO层进行简化,就看做的粒度了!楼上的代码可行!
      

  4.   

    super.setType(AnyTable.class); 这个方法其实没有必要存在改成这样:public abstract class BaseDAOImpl <T ,ID extends java.io.Serializable> extends HibernateDaoSupport implements IBaseDAO <T,ID> {
    private Class<T> type = getTypeClass();
    private Class<T> getTypeClass() {
    if (type == null) {
    ParameterizedType thisType = (ParameterizedType) getClass().getGenericSuperclass();
    domainClass = (Class<T>) thisType.getActualTypeArguments()[0];
    }
    return type;
    }
    }public class AnyTableDAO extends BaseDAOImpl <AnyTable ,Integer> {
    }
    这样就可以了
      

  5.   

    domainClass = (Class<T>) thisType.getActualTypeArguments()[0];
    改成  type = (Class<T>) thisType.getActualTypeArguments()[0];
    没有修改过来,呵呵