其他-如何获取父类中的泛型T的类型

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  implements ICommonDao {
	@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;
	}
	
}

你可能感兴趣的:(其他)