一:
package com.wzs.test2.dao;import java.util.List;public interface CommonDAO { publicvoid sava(T entity); // 保存用户,无返回值; public void remove(T entity); // 删除用户 public void update(T entity); // 更新用户 public T findById(Class entityClass, Integer id); // 通过id来查找某一个用户; public List findAll(Class entityclass); // 使用范型List<>。查询全部的用户信息}
package com.wzs.test2.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.wzs.test2.dao.CommonDAO;public class CommonDAOImpl extends HibernateDaoSupport implements CommonDAO { @SuppressWarnings("unchecked") public二:List findAll(Class entityclass) { String hql = "from " + entityclass.getName() + " as a order by a.id desc"; return this.getHibernateTemplate().find(hql); } @SuppressWarnings("unchecked") public T findById(Class entityClass, Integer id) { return (T) this.getHibernateTemplate().get(entityClass, id); } public void remove(T entity) { this.getHibernateTemplate().delete(entity); } public void sava(T entity) { this.getHibernateTemplate().save(entity); } public void update(T entity) { this.getHibernateTemplate().update(entity); }}
package com.wzs.test.dao;import java.io.Serializable;import java.util.Collection;public interface BaseDao{ public void saveEntity(T t); public Collection get(); public void updateEntity(T t); public void deleteEntity(Serializable id); public T getEntityById(Serializable id);}
package com.wzs.test.dao.impl;import java.io.Serializable;import java.lang.reflect.ParameterizedType;import java.util.Collection;import org.springframework.orm.hibernate3.HibernateTemplate;import com.wzs.test.dao.BaseDao;public class BaseDaoImplimplements BaseDao { private HibernateTemplate hibernateTemplate; private Class classt; public BaseDaoImpl() { // ParameterizedType就是泛型,关键部分 ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); this.classt = (Class) type.getActualTypeArguments()[0];// System.out.println(type.getRawType()); } public void deleteEntity(Serializable id) { T t = this.getEntityById(id); hibernateTemplate.delete(t); } public Collection get() { System.out.println(classt.getSimpleName()); return this.hibernateTemplate.find("from " + classt.getSimpleName()); } public T getEntityById(Serializable id) { return (T) this.hibernateTemplate.get(classt, id); } public void saveEntity(T t) { this.hibernateTemplate.save(t); } public void updateEntity(T t) { this.hibernateTemplate.update(t); } // set&&get public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; }}
package com.wzs.test.dao;public interface StudentDaoextends BaseDao {}
package com.wzs.test.dao.impl;import org.springframework.stereotype.Component;import com.wzs.test.dao.StudentDao;import com.wzs.test.dao.bean.Student;@Componentpublic class StudentDaoImpl extends BaseDaoImplimplements StudentDao {}
package com.wzs.test.dao.bean;public class Student {}參考:
http://www.cnblogs.com/shenliang123/archive/2012/04/05/2433134.html
http://blog.csdn.net/qiaoge134/article/details/20228139