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

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;
    

}

转载于:https://www.cnblogs.com/siashan/p/3956287.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值