zoukankan      html  css  js  c++  java
  • 获取范型类的子类的实际类型的方法

    package cn.itcast.oa.base;

    import java.lang.reflect.ParameterizedType;
    import java.util.List;

    import javax.annotation.Resource;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    @SuppressWarnings("unchecked")
    public abstract class BaseDaoImpl<T> implements BaseDao<T> {
        @Resource
        private SessionFactory sessionFactory;
        
        private Class<T> clazz; //这是一个问题,等待解决
        
        public BaseDaoImpl(){
            //使用反射技术得到T的真实类型
            ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();//获取当前new的对象的泛型的父类类型
            this.clazz = (Class<T>) pt.getActualTypeArguments()[0];//获取第一个类型参数的类型
            System.out.println("clazz-->"+this.clazz);
        }
        
        /**
         * 得到Session
         *
         * @return
         */
        protected Session getSession(){
            return sessionFactory.getCurrentSession();
        }

        @Override
        public void save(T entity) {
            getSession().save(entity);
        }

        @Override
        public void update(T entity) {
            getSession().update(entity);
        }
        
        @Override
        public void delete(Long id) {
            Object obj = getById(id);
            if(obj != null){
                getSession().delete(obj);
            }
        }


        @Override
        public T getById(Long id) {
            return (T) getSession().get(clazz, id);
        }

        @Override
        public List<T> getByIds(Long[] ids) {
            return getSession().createQuery(//
                    "FROM " +clazz.getSimpleName()+ " WHERE id IN (:ids)")//
                    .setParameterList("ids", ids)//
                    .list();
        }


        @Override
        public List<T> findAll() {
            return getSession().createQuery(//
                    "FROM " + clazz.getSimpleName())//
                    .list();
        }

    }

    //=================第二个例子========================//

    package cn.itcast.oa.base;

    import java.lang.reflect.ParameterizedType;

    import javax.annotation.Resource;

    import cn.itcast.oa.domain.Department;
    import cn.itcast.oa.service.DepartmentService;
    import cn.itcast.oa.service.RoleService;

    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;

    public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

        //===============ModelDriven的支持======================//
        protected T model;
        
        public BaseAction(){
            try{
            //通过反射得到model的真实实例
            ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
            Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
            model = clazz.newInstance();
            }catch(Exception e){
                throw new RuntimeException(e);
            }
        }
        
        @Override
        public T getModel() {
            return model;
        }
        //===============Service实例的声明======================//
        @Resource
        protected DepartmentService departmentService;
        
        @Resource
        private RoleService roleService;
        

    }

  • 相关阅读:
    spark 系列之六 SparkStreaming数据源之socket流
    spark 系列之五 SparkStreaming数据源之文件流
    spark 系列之四 Spark数据源之关系型数据库
    spark 系列之三 RDD,DataFrame与DataSet之间的转化
    spark 系列之二 Dataframe的使用
    spark 系列之一 RDD的使用
    Java Int类型与字符,汉字之间的转换
    Java实现栈
    名字的漂亮度
    在字符串中找出连续最长的数字串
  • 原文地址:https://www.cnblogs.com/siashan/p/3956287.html
Copyright © 2011-2022 走看看