zoukankan      html  css  js  c++  java
  • java类的泛型DAO

    @Transactional
    public abstract class DAOSupport<T> implements DAO<T> {
        protected Class<T> clazz = GenericUtil.getGenericSuperClass(this.getClass());
        @Resource
        protected SessionFactory sessionFactory;
    
        public T find( Integer id) {
            
            
            return (T)sessionFactory.getCurrentSession().get(clazz, id);
        }
    }
    View Code
    public class GenericUtil {
    
        public static Class getGenericSuperClass(
                Class clazz) {
        
            Type type = clazz.getGenericSuperclass();
            if(!(type instanceof ParameterizedType)){
                throw new RuntimeException();
            }
            ParameterizedType parameterizedType=(ParameterizedType)type;
            Type [] types = parameterizedType.getActualTypeArguments();
            
            return (Class)types[0];
        }
    
    }
    View Code

    java里貌似不能如.net typeof(T)来获取,hibernate居然也没有session.get<T>(object id)这样的方法。。。,而必须时session.get(clazz,object)这样,貌似就要像上面这样转个圈才能获取。

  • 相关阅读:
    Python学习-if条件语句
    Python学习-变量
    认识Python
    win7分盘
    mysql环境变量配置
    mysql的下载及配置
    c# excel xls保存
    js 在线引用
    js layer.js
    vue day3 bootstrap 联动下拉
  • 原文地址:https://www.cnblogs.com/wdfrog/p/3244958.html
Copyright © 2011-2022 走看看