一:
package com.wzs.test2.dao;
import java.util.List;
public interface CommonDAO {
public <T> void sava(T entity); // 保存用户,无返回值;
public <T> void remove(T entity); // 删除用户
public <T> void update(T entity); // 更新用户
public <T> T findById(Class<T> entityClass, Integer id); // 通过id来查找某一个用户;
public <T> List<T> findAll(Class<T> 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 <T> List<T> findAll(Class<T> entityclass) {
String hql = "from " + entityclass.getName() + " as a order by a.id desc";
return this.getHibernateTemplate().find(hql);
}
@SuppressWarnings("unchecked")
public <T> T findById(Class<T> entityClass, Integer id) {
return (T) this.getHibernateTemplate().get(entityClass, id);
}
public <T> void remove(T entity) {
this.getHibernateTemplate().delete(entity);
}
public <T> void sava(T entity) {
this.getHibernateTemplate().save(entity);
}
public <T> void update(T entity) {
this.getHibernateTemplate().update(entity);
}
}
二:
package com.wzs.test.dao;
import java.io.Serializable;
import java.util.Collection;
public interface BaseDao<T> {
public void saveEntity(T t);
public Collection<T> 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 BaseDaoImpl<T> implements BaseDao<T> {
private HibernateTemplate hibernateTemplate;
private Class classt;
public BaseDaoImpl() {
// ParameterizedType就是泛型,关键部分
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
this.classt = (Class) type.getActualTypeArguments()[0];// <T>
System.out.println(type.getRawType());
}
public void deleteEntity(Serializable id) {
T t = this.getEntityById(id);
hibernateTemplate.delete(t);
}
public Collection<T> 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 StudentDao<Student> extends BaseDao<Student> {
}
package com.wzs.test.dao.impl;
import org.springframework.stereotype.Component;
import com.wzs.test.dao.StudentDao;
import com.wzs.test.dao.bean.Student;
@Component
public class StudentDaoImpl extends BaseDaoImpl<Student> implements StudentDao<Student> {
}
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