• 反射获取指定类型


    package dao;
    
    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import javax.annotation.Resource;
    
    import org.hibernate.Query;
    import org.hibernate.SessionFactory;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    /**
     * BaseDaoImpl不能在类型未确定前直接实例化
     * @author zh
     * @param <T>
     */
    public class BaseDaoImpl<T extends Serializable> extends HibernateDaoSupport implements IbaseDao<T> {
    	
    	@Resource(name="sessionFactory")
    	public void setSupperSessionFactory(SessionFactory sessionFactory){
    			super.setSessionFactory(sessionFactory);
    	}
    	
    
    	@SuppressWarnings("rawtypes")
    	private Class Tclass;
    	@SuppressWarnings("rawtypes")
    	public BaseDaoImpl(){
    		 Type type = this.getClass().getGenericSuperclass();  
    	        if (type.toString().indexOf("BaseDao") != -1) {  
    	            ParameterizedType type1 = (ParameterizedType) type;  
    	            Type[] types = type1.getActualTypeArguments();  
    	            setTclass((Class) types[0]);  
    	        }else{  
    	            type = ((Class)type).getGenericSuperclass();  
    	            ParameterizedType type1 = (ParameterizedType) type;  
    	            Type[] types = type1.getActualTypeArguments();  
    	            setTclass((Class) types[0]);  
    	        } 
    	 }
    	
    	/**
    	 * 保存对象
    	 * 
    	 * @param Object
    	 */
    
    	public void save(T t) {
    		//this.getHibernateTemplate().save(t)
    		getSession().save(t);
    		
    	}
    
    	/**
    	 * 更新对象内容
    	 * 
    	 * @param Object
    	 */
    	public void update(T t) {
    		//this.getHibernateTemplate().update(t);
    		getSession().update(t);
    	}
    	
    	/**
    	 * 删除对象
    	 * @param t
    	 */
    	public void delete(T t){
    		//this.getHibernateTemplate().delete(t);
    		getSession().delete(t);
    	}
    	
    	public void deleteById(String hql, Object[] params){
    		Query query = this.getSession().createQuery(hql);
    		for (int i = 0; i < params.length; i++) {
    			query.setParameter(i, params[i]);
    		}
    		query.executeUpdate();
    	}
    	
    	/**
    	 * ͨ通过ID 得到对象
    	 * @param Serializable
    	 */
    	@SuppressWarnings("unchecked")
    	public T get(Serializable id) {
    		//return (T) this.getHibernateTemplate().get(getTclass(), id);
    		return (T)getSession().get(getTclass(), id);
    	}
    	
    	
    
    	/**
    	 * 取得所有对象
    	 * 
    	 */
    	@SuppressWarnings("unchecked")
    	public List<T> listAll() {
    		String hql = "from "+getTclass().getSimpleName();
    		return this.getSession().createQuery(hql).list();
    	}
    
    	
    
    	/**
    	 * hql解决方案
    	 * 
    	 * @param String hql
    	 *          
    	 * @param Object[] params
    	 *           参数列表  顺序不能颠倒
    	 */
    
    	@SuppressWarnings("unchecked")
    	public List<T> listByHql(String hql, Object[] params) {
    		Query query = this.getSession().createQuery(hql);
    		if(null == params || params.length == 0){
    			return query.list();
    		}else{
    			for (int i = 0; i < params.length; i++) {
    				query.setParameter(i, params[i]);
    			}
    		}
    		return query.list();
    	}
    
    	/**
    	 * 同类分页
    	 * 
    	 * @param int firstResult
    	 *           从第几条开始
    	 * @param int maxResults
    	 *           要取几条
    	 */
    	@SuppressWarnings({ "unchecked" })
    	public List<T> listFenYe(int firstResult, int maxResults) {
    		String hql = "from "+getTclass().getSimpleName();
    		Query query = this.getSession().createQuery(hql).setMaxResults(maxResults).setFirstResult(firstResult);
    		return query.list();
    	}
    	
    	/**
    	 * 取得行数
    	 */
    
    	public long getCount() {
    		String hql = "select count(*) from "+getTclass().getSimpleName();
    		return (Long) this.getSession().createQuery(hql).uniqueResult();
    	}
    	/**
    	 * 懒加载load
    	 */
    	@SuppressWarnings("unchecked")
    	public T load(Serializable id) {
    		//return (T) this.getHibernateTemplate().load(this.getClass(), id);
    		return (T) getSession().load(this.getClass(), id);
    	}
    
    	@SuppressWarnings("rawtypes")
    	public Class getTclass() {
    		return Tclass;
    	}
    	@SuppressWarnings("rawtypes")
    	public void setTclass(Class tclass) {
    		Tclass = tclass;
    	}
    
    	@SuppressWarnings("unchecked")
    	public T getByHql(String hql, Object[] params) {
    		Query query = this.getSession().createQuery(hql);
    		for (int i = 0; i < params.length; i++) {
    			query.setParameter(i, params[i]);
    		}
    		return (T) query.uniqueResult();
    	}
    	
    	@SuppressWarnings("unchecked")
    	public List<T> getByHqlNotIn(String hql, Object[] params) {
    		System.out.println("Tclass:"+Tclass);
    		Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
    		List<T> list = null ;
    		for (int i = 0; i < params.length; i++) {
    			query.setParameter(i, params[i]);
    		}
    		list = query.list();
    		return list;
    	}
    
    	@SuppressWarnings("unchecked")
    	public List<T> listFenYeAddparams(int firstResult, int maxResults,
    			String hql, Object[] params) {
    		Query query = this.getSession().createQuery(hql);
    		List<T> list = null ;
    		if(null == params || params.length == 0){
    			list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    		}
    		if(null != params || params.length != 0){
    			for (int i = 0; i < params.length; i++) {
    				System.out.println("params:"+params[i]);
    				query.setParameter(i, params[i]);
    			}
    			list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    		}
    		return list;
    	}
    
    	@SuppressWarnings("unchecked")
    	public List<T> listFenYeNotIn(int firstResult, int maxResults,
    			String hql, Object[] params) {
    		Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
    		List<T> list = null ;
    		if(null == params || params.length == 0){
    			list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    		}
    		if(null != params || params.length != 0){
    			for (int i = 0; i < params.length; i++) {
    				System.out.println("params:"+params[i]);
    				query.setParameter(i, params[i]);
    			}
    			list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    		}
    		return list;
    	}
    	
    	@SuppressWarnings("unchecked")
    	public List<T> listNotIn(String hql, Object[] params) {
    		Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
    		List<T> list = null ;
    		if(null != params || params.length != 0){
    			for (int i = 0; i < params.length; i++) {
    				System.out.println("params:"+params[i]);
    				query.setParameter(i, params[i]);
    			}
    		}
    		list = query.list();
    		return list;
    	}
    	
    	
    	
    	public long getCount(String hql, Object[] params) {
    		Query query = this.getSession().createQuery(hql);
    		if(null != params || params.length != 0){
    			for (int i = 0; i < params.length; i++) {
    				query.setParameter(i, params[i]);
    			}
    		}
    		return (Long) query.uniqueResult();
    	}
    	@SuppressWarnings("unchecked")
    	public List<T> queryPage(Map map,int firstResult, int maxResults,
    			String hql) {
    		StringBuffer buffer = new StringBuffer(hql);
    		StringBuffer wherestring = new StringBuffer();
    		Iterator paramnames  = map.keySet().iterator();
    		while(paramnames .hasNext())
    		{
    			String paramname = (String) paramnames.next();
    			String value = null;
    			value = String.valueOf(map.get(paramname));
    			if(value!=null)
    			{
    				value = value.trim();
    				if(value.equals(""))
    				{
    					continue;
    				}
    			}
    			if(wherestring.length()==0)
    			{
    				wherestring.append(" where ");
    			}else{
    				wherestring.append(" and ");
    			}
    			wherestring.append(paramname).append("=").append(value);
    			buffer.append(wherestring);
    		}
    		Query query = this.getSession().createQuery(buffer.toString());
    		System.out.println(buffer.toString());
    		List<T> list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
    		return list;
    	}
    	public long getCount(String hql, Map map) {
    		StringBuffer buffer = new StringBuffer(hql);
    		StringBuffer wherestring = new StringBuffer();
    		Iterator paramnames  = map.keySet().iterator();
    		while(paramnames .hasNext())
    		{
    			String paramname = (String) paramnames.next();
    			String value = null;
    			//value = (String) map.get(paramname);
    			value = String.valueOf(map.get(paramname));
    			if(value!=null)
    			{
    				value = value.trim();
    				if(value.equals(""))
    				{
    					continue;
    				}
    			}
    			if(wherestring.length()==0)
    			{
    				wherestring.append(" where ");
    			}else{
    				wherestring.append(" and ");
    			}
    			wherestring.append(paramname).append("=").append(value);
    			buffer.append(wherestring);
    		}
    		System.out.println(buffer.toString());
    		Query query = this.getSession().createQuery(buffer.toString());
    		return (Long) query.uniqueResult();
    	}
    
    	public int update(String hql, Object[] params) {
    		Query query = this.getSession().createQuery(hql);
    		if(params == null){
    			return query.executeUpdate();
    		}
    		for (int i = 0; i < params.length; i++) {
    			query.setParameter(i, params[i]);
    		}
    		return query.executeUpdate();
    	}
    
    	public void save(String hql, Object[] params) {
    		Query query = this.getSession().createQuery(hql);
    		for (int i = 0; i < params.length; i++) {
    			query.setParameter(i, params[i]);
    		}
    		query.executeUpdate();
    	}
    
    }
    

      

  • 相关阅读:
    VS中编码格式的问题(待总结)
    第一次搭建Eureka服务器的虐心之路
    Eureka相关
    【移动开发】SparseArray替代HashMap
    【移动开发】plurals
    皮尔森相似度计算举例(R语言)
    据说,年薪百万的程序员,都是这么开悟的
    RDD:基于内存的集群计算容错抽象
    使用IDEA运行Spark程序
    scala for spark
  • 原文地址:https://www.cnblogs.com/a757956132/p/4737026.html
Copyright © 2020-2023  润新知