dao층의 범용 실현(2가지 방법)
3917 단어 dao층의 범형 실현 2가지 방법
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);
}
}
2:
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