其他-如何获取父类中的泛型T的类型
2017-09-04 15:07
246 查看
package cn.itcast.elec.dao.impl; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; import cn.itcast.elec.dao.inter.ICommonDao; @Transactional public abstract class CommonDaoImpl<T> implements ICommonDao<T> { @Resource(name="sessionFactory") private SessionFactory sessionFactory; public Object findObjectById(Serializable id) { //泛型转换
//获得父类 ParameterizedType pt=(ParameterizedType) this.getClass().getGenericSuperclass();
//获得泛型的类型 Class entity=(Class) pt.getActualTypeArguments()[0]; Session session=sessionFactory.getCurrentSession(); return session.get(entity, id); } //======================================================================== public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
相关文章推荐
- java 反射机制 (获取父类泛型的类型)getGenericSuperclass
- post multipart/form-data 类型表单如何获取File外其他参数
- 使用反射获取当前new的对象的 带泛型的父类的 类型
- DAO中,如何获取子类带泛型的父类的参数
- Java 运行时如何获取泛型参数的类型
- 如何获取继承类中的泛型T的类型
- 通过带有泛型类型的父类获取实体类型
- java 反射机制 (获取父类泛型的类型)getGenericSuperclass
- 一、获取子类DAO继承父类DAO的泛型参数类型的Class对象
- Class的 getSuperclass与getGenericSuperclass区别和如何获取泛型参数类型
- 父类如何获取子类传递的泛型
- 如何获取继承中泛型T的类型
- 通过反射获取父类泛型类型
- java反射,获得Class是否为基本数据类型,是否其他类的父类。获得泛型。
- 思想:java中,父类的方法中传入的形参的数据类型是泛型,子类的方法的形参想只要一种确定的数据类型,子类该如何做呢?
- 关于如何获取泛型参数的实体类型
- 通过父类的泛型获取子类的类型Class.getGenericSuperclass
- Java反射通过class获取父类泛型类型
- 如何在运行时获取泛型的类型
- 如何获取方法参数的泛型类型