Class<T>在BaseDao<T>作用

1.BaseDao<T>

  1. package org.hzy.dao;  
  2.   
  3. import java.lang.reflect.ParameterizedType;  
  4. import java.lang.reflect.Type;  
  5.   
  6. import org.hibernate.Session;  
  7. import org.hibernate.SessionFactory;  
  8. import org.hibernate.cfg.Configuration;  
  9.   
  10. public class BaseDao<T extends EntitysSuper> { //参数可以是多个<T,K>  
  11.   
  12.     static Configuration config = new Configuration().configure();  
  13.     static SessionFactory fac = config.buildSessionFactory();  
  14.   
  15.     private Class entityclass;  
  16.   
  17.     public Session getSession() {  
  18.         return fac.getCurrentSession();  
  19.     }  
  20.   
  21.     public BaseDao() {  
  22.         // TODO Auto-generated constructor stub  
  23.         this.entityclass = getParameterizedType(this.getClass());  
  24.     }  
  25.   
  26.     protected Class getParameterizedType(Class clazz) {  
  27.         //返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。  
  28.         Type ty = clazz.getGenericSuperclass();  
  29.         // 得到传入进来类的父类型 //BaseDao<Dept>--org.hzy.dao.BaseDao<org.hzy.entity.Dept>  
  30.         // System.out.println(ty);  
  31.         Type[] types=null;  
  32.         if (ty instanceof ParameterizedType) {  
  33.             //注意此处ty必须是有泛型参数 , 得到当前类型的泛型 <Dept>--class org.hzy.entity.Dept  
  34.             types= ((ParameterizedType) ty).getActualTypeArguments();  
  35.         }else{    
  36.             try {    
  37.                 throw new Exception("not find ParameterizedType!");    
  38.             } catch (Exception e) {    
  39.                 // TODO Auto-generated catch block    
  40.                 e.printStackTrace();    
  41.             }  
  42.         }  
  43.         // System.out.println(types[0]);  
  44.           
  45.         // Type t=((ParameterizedType)ty).getRawType();//得到声明这个类型的类或者接口  
  46.         return (Class) types[0];  
  47.     }  
  48.   
  49.     public T get_object(Integer id) {  
  50.         return (T) this.getSession().get(entityclass, id);  
  51.     }  
  52. }  


2.测试,通过不同的类则返回你这个类的类型:

  1. public class DeptImpl extends BaseDao<Dept>{  
  2.     public static void main(String[] args) {  
  3.           
  4.         DeptImpl de=new DeptImpl();  
  5.         Transaction t=de.getSession().beginTransaction();  
  6.         System.out.println(de.get_object(10));  
  7.         t.commit();  
  8.     }  
  9. }  


 

  1. public class EmpImpl extends BaseDao<Emp>{  
  2.     public static void main(String[] args) {  
  3.         EmpImpl em=new EmpImpl();  
  4.         Transaction t=em.getSession().beginTransaction();  
  5.         Emp emp=em.get_object(7369);  
  6.         System.out.println(emp.getEname());  
  7.         t.commit();  
  8.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值