持久层抽取之类名的获取
作者:互联网
package com.hdh.bos.dao.base.impl; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; import org.hibernate.Hibernate; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.hdh.bos.dao.base.BaseDao; public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> { private Class<T> entityClass; public BaseDaoImpl(Class<T> entityClass) { //获取BaseDaoImpl<T> 的T类型 ParameterizedType Superclass = (ParameterizedType) this.getClass().getGenericSuperclass(); Type[] actualTypeArguments = Superclass.getActualTypeArguments(); entityClass=(Class<T>) actualTypeArguments[0]; } @Override public void save(T entity) { getHibernateTemplate().save(entity); } @Override public void delete(T entity) { getHibernateTemplate().delete(entity); } @Override public void update(T entity) { getHibernateTemplate().update(entity); } @Override public T fingById(Serializable id) { return getHibernateTemplate().get(entityClass, id); } @Override public List<T> findAll() { String hql = "FROM " + entityClass.getName(); return (List<T>) getHibernateTemplate().find(hql); } }
标签:entityClass,抽取,持久,getHibernateTemplate,entity,Override,之类,import,public 来源: https://www.cnblogs.com/asndxj/p/10659415.html