Class<T>在BaseDao<T>作用
2012-09-11 15:36
239 查看
1.BaseDao<T>
2.测试,通过不同的类则返回你这个类的类型:
通过使用泛型T减少Dao的冗余代码,当T继承某个对象时(T extends EntityDao)限制了参数类型必须继承该对象(EntityDao),并且ClassT必须要有泛型参数(DeptDaoImpl extends ClassT<Dept>),否则转换失败。
package org.hzy.dao; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class BaseDao<T extends EntitysSuper> { //参数可以是多个<T,K> static Configuration config = new Configuration().configure(); static SessionFactory fac = config.buildSessionFactory(); private Class entityclass; public Session getSession() { return fac.getCurrentSession(); } public BaseDao() { // TODO Auto-generated constructor stub this.entityclass = getParameterizedType(this.getClass()); } protected Class getParameterizedType(Class clazz) { //返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。 Type ty = clazz.getGenericSuperclass(); // 得到传入进来类的父类型 //BaseDao<Dept>--org.hzy.dao.BaseDao<org.hzy.entity.Dept> // System.out.println(ty); Type[] types=null; if (ty instanceof ParameterizedType) { //注意此处ty必须是有泛型参数 , 得到当前类型的泛型 <Dept>--class org.hzy.entity.Dept types= ((ParameterizedType) ty).getActualTypeArguments(); }else{ try { throw new Exception("not find ParameterizedType!"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // System.out.println(types[0]); // Type t=((ParameterizedType)ty).getRawType();//得到声明这个类型的类或者接口 return (Class) types[0]; } public T get_object(Integer id) { return (T) this.getSession().get(entityclass, id); } }
2.测试,通过不同的类则返回你这个类的类型:
public class DeptImpl extends BaseDao<Dept>{ public static void main(String[] args) { DeptImpl de=new DeptImpl(); Transaction t=de.getSession().beginTransaction(); System.out.println(de.get_object(10)); t.commit(); } }
public class EmpImpl extends BaseDao<Emp>{ public static void main(String[] args) { EmpImpl em=new EmpImpl(); Transaction t=em.getSession().beginTransaction(); Emp emp=em.get_object(7369); System.out.println(emp.getEname()); t.commit(); } }
通过使用泛型T减少Dao的冗余代码,当T继承某个对象时(T extends EntityDao)限制了参数类型必须继承该对象(EntityDao),并且ClassT必须要有泛型参数(DeptDaoImpl extends ClassT<Dept>),否则转换失败。
相关文章推荐
- BaseDAO开发及获取泛型的传输数据类型(Class<T>)实例讲解
- Class<T>在BaseDao<T>作用
- <转>Class.forName的作用以及为什么要用它
- Class<T>在BaseDao<T>作用
- The constructor Intent(new View.OnClickListener(){}, Class<ButtonStudyActivity>) is undefined
- <context-param>与<init-param>的区别与作用
- error C2653: 'list<class CSomething*,class std::allocator<class CSomething*> >'...解决方法
- 【<meta name="description" content=">】作用讲解
- html随记---<tbody>的作用
- class A<T> where T:class 这个泛型类中的Where T:class什么意思
- Android错误之android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
- #include <stdlib.h> 这个头文件是什么作用?
- <%@ page isELIgnored="false"%>的作用
- web.xml的配置中<context-param>配置作用
- error C2143: syntax error : missing ';' before '<class-head>'
- [置顶] 从零开始学C++之对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector .
- <综合>java virtual machine launcher 提示 could not find the main class,program will exit 问题
- #include <sys/types.h>的作用
- Hibernate实体类映射文件demo.hbm.xml 中的<generator class="?"></generator>的不同属性含义
- <form>标签只能作用一个<input type="hidden"/>?