• 获取范型类的子类的实际类型的方法


    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;
        

    }

  • 相关阅读:
    SQL SERVER NVARCHAR字段INSERT 中文乱码问题解决
    解析$(this).data('type');
    使用HttpWebRequest发送自定义POST请求
    C#使用WebClient下载文件到本地目录
    linux开发时,想用的链接暂存
    程序进行重定位的技术按重定位的时机分类
    文件分类
    快表、页表
    操作系统的发展与分类
    网络操作系统和分布式操作系统的区别
  • 原文地址:https://www.cnblogs.com/siashan/p/3956287.html
Copyright © 2020-2023  润新知