基於hibernate4的BaseDao接口

2018.9.17  由於項目備份丟失,僅存以下代碼

接口類

 

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;

import xxxxx.bean.Page;

public interface BaseDao {
	
	/**
	 * 是否存在指定條件數據。
	 * @param entityClass 映射類
	 * @param whereClause 條件
	 * @param values 條件值
	 * @return true:存在,false:不存在
	 */
	public boolean isExist(Class<?> entityClass,String whereClause, Map<String, Object> params );
	
	/**
	 * 執行update,delete,insert語句。
	 * @param hql
	 * 
	 * @return 執行影響的數量
	 */
	public int execute(final String hql, final Map<String, Object> params);
	
	/**
	 * 持久化
	 * @param entity
	 */
	public void save(Object entity);

	/**
	 * 更新
	 * @param entity
	 */
	public void update(Object entity);

	/**
	 * 保存或更新。
	 * @param entity
	 */
	public void saveOrUpdate(Object entity);
	
	/**
	 * 刪除操作。
	 * @param entity
	 */
	public void delete(Object entity);
	
	/**
	 * 按照屬性(id)條件查找數據。
	 * @param clasz 待查找的實體類
	 * @param id    根據條件id的值匹配實體類的id
	 * @return E 查找到的實體
	 */
	public <E> E getById(Class<E> clasz, Serializable id);

	/**
	 * 查找某實體類的第一條記錄。
	 * @param clasz 待查找的實體類
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz);

	/**
	 * 查找某實體類的第一條記錄。
	 * @param clasz 待查找的實體類
	 * @param includes  條件參數
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String[] includes);
     
	/**
	 * 根據條件查找某實體類的第一條記錄。
	 * @param clasz 待查找的實體類
	 * @param conditions 條件
	 * @return  E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions);
  
	/**
	 * 查找某實體類的第一條記錄。 
	 * @param clasz 待查找的實體類
     * @param conditions    條件
     * @param includes   需要關聯取出的外鍵對象
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, String[] includes);
     
	/**
	 * 查找某實體類的第一條記錄。   
	 * @param clasz 待查找的實體類
     * @param conditions     條件
     * @param args    條件參數
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params);

	/**
	 * 查找某實體類的第一條記錄。  
	 * @param clasz 待查找的實體類
     * @param conditions    條件
     * @param args    參數條件
     * @param includes   需要關聯取出的外鍵對象
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params,
			String[] includes);
    
	/**
	 * 查找某實體類的第一條記錄。 
	 * @param clasz 待查找的實體類
     * @param conditions   條件
     * @param args   參數
     * @param order  排序條件(如"id desc,code")
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params, String order);
    
	/**
	 *  查找某實體類的第一條記錄。 
	 * @param clasz 待查找的實體類
     * @param conditions   條件
     * @param includes   需要關聯取出的外鍵對象
     * @param args    參數
     * @param order  排序條件
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params,
			String order, String[] includes);
  
	/**
	 * 查找某實體類的第一條記錄。 
	 * @param clasz 待查找的實體類
     * @param conditions    條件
     * @param includes    需要關聯取出的外鍵對象
     * @param args    參數
     * @param order  排序條件(如"id desc,code")
     * @param start  分頁開始條數
     * @param limit  分頁每頁顯示條數
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params,
			String order, int start, String[] includes) ;
	/**
	 * 查找全部實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz) ;
	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 * @param includes     需要關聯取出的外鍵對象
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String[] includes) ;
	/**
	 * 根據條件查找符合條件全部實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions    條件
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions);
	/**
	 * 根據條件查找符合條件全部實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions     條件
	 *  @param includes    需要關聯取出的外鍵對象)
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			String[] includes) ;

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions    條件
	 *  @param args             參數
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions, Map<String, Object> params) ;

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions   條件
	 *  @param args            參數
	 *  @param includes     需要關聯取出的外鍵對象
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			Map<String, Object> params, String[] includes);

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions   條件
	 *  @param args            參數
	 *  @param order           排序條件(如"id desc,code")
	 *  @return E                  查找到的實體
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			Map<String, Object> params, String order) ;

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions 條件
	 *  @param args          參數
	 *  @param order        排序條件(如"id desc,code")
	 *  @param includes    需要關聯取出的外鍵對象)
	 *  @return E                查找到的實體
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			Map<String, Object> params, String order, String[] includes) ;
	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions    條件
	 *  @param args             參數
	 *  @param order            排序條件(如"id desc,code")
	 *  @param limit             分頁每頁顯示條數
	 *  @return E                   查找到的實體
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			Map<String, Object> params, String order, int limit);

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions    條件
	 *  @param args             參數
	 *  @param order         排序條件 (如"id desc,code")
	 *  @param limit            分頁每頁顯示條數
	 *  @param includes   需要關聯取出的外鍵對象
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			Map<String, Object> params, String order, int limit, String[] includes);

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions  條件
	 *  @param args         參數
	 *  @param order       排序條件(如"id desc,code")
	 *  @param   start      分頁開始條數
	 *  @param limit         分頁每頁顯示條數
	 *   @param includes    需要關聯取出的外鍵對象
	 *  @return E 查找到的實體
	 */
	public <E> List<E> findAll(final Class<E> clasz, final String conditions, final Map<String, Object> params, final String order, final int start, final int limit, final String[] includes);

	/**
	 * 根據條件查找第一個符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param hql  hql語句
	 *  @param args    參數
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> E findFirstByHQL(Class<E> clasz, final String hql, final Map<String, Object> params) ;

	/**
	 * HQL查詢根據條件查找符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param hql  hql語句(必須完整寫出 from Class)
	 *  @param args    hql 條件(Object數組)
	 *  @return E 查找到的實體(List集合)
	 */
	public  <E> List<E> findByHQL(Class<E> clasz, final String hql, final Map<String, Object> params) ;

	/**
	 * 標量
	 * HQL查詢根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args   參數
	 *  @return Integer  標量值
	 */
	public Integer findInt(String hql, Map<String, Object> params);

	/**
	 * 標量
	 * HQL查詢根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args   參數
	 *  @return Long  標量值
	 */
	public Long findLong(String hql, Map<String, Object> params);

	/**
	 * 標量
	 * 根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args    參數
	 *  @return Double 標量值
	 */
	public Double findDouble(String hql, Map<String, Object> params) ;

	/**
	 * 標量
	 * HQL查詢根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args   參數
	 *  @return BigDecimal 標量值
	 */
	public BigDecimal findBigDecimal(String hql, Map<String, Object> params);
	/**
	 * 標量
	 * HQL查詢根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args   參數
	 *  @return String 標量值
	 */
	public String findString(String hql, Map<String, Object> params);


	/**
	 * 分頁條件查詢 
	 *  @param DetachedCriteria  dc   條件
	 *  @param start    分頁開始顯示數
	 *  @param limit      分頁每頁顯示條數
	 *  @return Page   分頁對象
	 */
	public  <E> Page<E> findPage(final DetachedCriteria dc, final int start,
			final int limit) ;

	public <T> Page<T> findPageByHQL(Class<T> clasz, final String hql, final Map<String, Object> params,
			final int start, final int limit) ;
	
	
	/**
	 * 根據hql語句查找實體信息
	 *  @param sql    createSQLQuery條件(hql語句)
	 *  @param args     參數
     *  @param start    分頁開始顯示數
	 *  @param limit      分頁每頁顯示條數
	 *  @return  page   分頁對象
	 */
	public <T> Page<T> findPageBySQL(final Class<T> clasz,final String sql, final Map<String, Object> params,final int start, final int limit) ;
	
	/**
	 * 根據hql語句查找實體信息
	 *  @param sql    createSQLQuery條件(hql語句)
	 *  @param args    參數
	 *  @param count_sql
     *  @param start    分頁開始顯示數
	 *  @param limit      分頁每頁顯示條數
	 *  @return  page  分頁對象
	 */
	public <T> Page<T> findPageBySQL(final Class<T> clasz,final String sql,String count_sql, final Map<String, Object> params,final int start, final int limit) ;
	
	/**
	 * 根據hql語句查找實體信息
	 *  @param sql    createSQLQuery條件(hql語句)
	 *  @param args    條件參數
	 *  @return List 集合
	 */
	public List<?> findListBySQL(final String sql,final Map<String, Object> params);
	
	
	/**
	 * 根據hql語句查找第一個實體信息
	 *  @param sql    createSQLQuery條件(hql語句)
	 *  @param args    條件 參數
	 *  @return Object
	 */
	public Object findFirstBySQL(final String sql, final Map<String, Object> params);
	
	
	/**
	 * SQL查詢並將結果集自動轉換成POJO的list
	 * @param clazz 最終轉換成的類
	 * @param sql 查詢條件
	 * @return
	 */
	public <T> List<T> findObjListBySql(final Class<T> clazz,final String sql);
	
	
	/**
	 * SQL查詢並將結果集自動轉換成POJO的list
	 * @param clazz 最終轉換成的類
	 * @param sql 查詢條件
	 * @return
	 */
	public <T> List<T> findObjListBySql(final Class<T> clazz,final String sql,final Integer start,final Integer limit);
}

 

實現類

 

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.internal.CriteriaImpl;
import org.hibernate.transform.ResultTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import xxxxx.bean.Page;
import xxxxx.dao.BaseDao;
import xxxxx.framework.constant.SystemConstant;
import xxxxx.util.CustomResultTransformer;
import xxxxx.util.DaoHelper;
import xxxxx.util.ReflectionUtils;

@Repository
public class BaseDaoImpl implements BaseDao{
	
	@Autowired
	private SessionFactory sessionFactory;
	
	/**
	 * 獲得當前事物的session
	 * 
	 * @return org.hibernate.Session
	 */
	
	public Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}
	
	/**
	 * 是否存在指定條件數據。
	 * @param entityClass 映射類
	 * @param whereClause 條件
	 * @param values 條件值
	 * @return true:存在,false:不存在
	 */
	public boolean isExist(Class<?> entityClass,String whereClause, Map<String, Object> params ) {
		String selectHql = "select count(id) from "+ entityClass.getName() + "  where " + whereClause;
		return findLong(selectHql, params) > 0;
	}
	
	
	/**
	 * 執行update,delete,insert語句。
	 * @param hql
	 * 
	 * @return 執行影響的數量
	 */
	public int execute(final String hql, final Map<String, Object> params) {		
		Query query = this.getCurrentSession().createQuery(hql);
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		return query.executeUpdate();
	}
	

	@Override
	public void delete(Object entity) {
		this.getCurrentSession().delete(entity);		
	}
	
	/**
	 * 持久化
	 * @param entity
	 */
	public void save(Object entity) {
		this.getCurrentSession().save(entity);
	}

	/**
	 * 更新
	 * @param entity
	 */
	public void update(Object entity) {
		this.getCurrentSession().update(entity);
	}

	/**
	 * 保存或更新。
	 * @param entity
	 */
	public void saveOrUpdate(Object entity) {
		this.getCurrentSession().saveOrUpdate(entity);
	}
    
	/**
	 * 按照屬性(id)條件查找數據。
	 * @param clasz 待查找的實體類
	 * @param id    根據條件id的值匹配實體類的id
	 * @return E 查找到的實體
	 */
	public <E> E getById(Class<E> clasz, Serializable id) {
		return (E) this.getCurrentSession().get(clasz, id);
	}

	/**
	 * 查找某實體類的第一條記錄。
	 * @param clasz 待查找的實體類
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz) {
		return findFirst(clasz, null, null, null, 0, null);
	}

	/**
	 * 查找某實體類的第一條記錄。
	 * @param clasz 待查找的實體類
	 * @param includes  條件參數
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String[] includes) {
		return findFirst(clasz, null, null, null, 0, includes);
	}
     
	/**
	 * 根據條件查找某實體類的第一條記錄。
	 * @param clasz 待查找的實體類
	 * @param conditions 條件
	 * @return  E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions) {
		return findFirst(clasz, conditions, null, null, 0, null);
	}
  
	/**
	 * 查找某實體類的第一條記錄。 
	 * @param clasz 待查找的實體類
     * @param conditions    條件
     * @param includes   需要關聯取出的外鍵對象
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, String[] includes) {
		return findFirst(clasz, conditions, null, null, 0, includes);
	}
     
	/**
	 * 查找某實體類的第一條記錄。   
	 * @param clasz 待查找的實體類
     * @param conditions     條件
     * @param args    條件參數
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params) {
		return findFirst(clasz, conditions, params, null, 0, null);
	}

	/**
	 * 查找某實體類的第一條記錄。  
	 * @param clasz 待查找的實體類
     * @param conditions    條件
     * @param args    參數條件
     * @param includes   需要關聯取出的外鍵對象
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params,
			String[] includes) {
		return findFirst(clasz, conditions, params, null, 0, includes);
	}
    
	/**
	 * 查找某實體類的第一條記錄。 
	 * @param clasz 待查找的實體類
     * @param conditions   條件
     * @param args   參數
     * @param order  排序條件(如"id desc,code")
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params,
			String order) {
		return findFirst(clasz, conditions, params, order, 0, null);
	}
    
	/**
	 *  查找某實體類的第一條記錄。 
	 * @param clasz 待查找的實體類
     * @param conditions   條件
     * @param includes   需要關聯取出的外鍵對象
     * @param args    參數
     * @param order  排序條件
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params,
			String order, String[] includes) {
		return findFirst(clasz, conditions, params, order, 0, includes);
	}
  
	/**
	 * 查找某實體類的第一條記錄。 
	 * @param clasz 待查找的實體類
     * @param conditions    條件
     * @param includes    需要關聯取出的外鍵對象
     * @param args    參數
     * @param order  排序條件(如"id desc,code")
     * @param start  分頁開始條數
     * @param limit  分頁每頁顯示條數
	 * @return E 查找到的實體
	 */
	public <E> E findFirst(Class<?> clasz, String conditions, Map<String, Object> params,
			String order, int start, String[] includes) {
		List<E> results = (List<E>) findAll(clasz, conditions, params, order, start, 1,includes);
		return results.size() == 0 ? null : results.get(0);
	}
    
	/**
	 * 查找全部實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz) {
		return findAll(clasz, null, null, null, 0, 0, null);
	}
   
	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 * @param includes     需要關聯取出的外鍵對象
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String[] includes) {
		return findAll(clasz, null, null, null, 0, 0, includes);
	}
   
	/**
	 * 根據條件查找符合條件全部實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions    條件
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions) {
		return findAll(clasz, conditions, null, null, 0, 0, null);
	}
	
	/**
	 * 根據條件查找符合條件全部實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions     條件
	 *  @param includes    需要關聯取出的外鍵對象)
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			String[] includes) {
		return findAll(clasz, conditions, null, null, 0, 0, includes);
	}

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions    條件
	 *  @param args             參數
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions, Map<String, Object> params) {
		return findAll(clasz, conditions, params, null, 0, 0, null);
	}

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions   條件
	 *  @param args            參數
	 *  @param includes     需要關聯取出的外鍵對象
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			Map<String, Object> params, String[] includes) {
		return findAll(clasz, conditions, params, null, 0, 0, includes);
	}

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions   條件
	 *  @param args            參數
	 *  @param order           排序條件(如"id desc,code")
	 *  @return E                  查找到的實體
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions, Map<String, Object> params, String order) {
		return findAll(clasz, conditions, params, order, 0, 0, null);
	}

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions 條件
	 *  @param args          參數
	 *  @param order        排序條件(如"id desc,code")
	 *  @param includes    需要關聯取出的外鍵對象)
	 *  @return E                查找到的實體
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions, Map<String, Object> params, String order, String[] includes) {
		return findAll(clasz, conditions, params, order, 0, 0, includes);
	}

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions    條件
	 *  @param args             參數
	 *  @param order            排序條件(如"id desc,code")
	 *  @param limit             分頁每頁顯示條數
	 *  @return E                   查找到的實體
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions, Map<String, Object> params, String order, int limit) {
		return findAll(clasz, conditions, params, order, limit, 0, null);
	}

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions    條件
	 *  @param args             參數
	 *  @param order         排序條件 (如"id desc,code")
	 *  @param limit            分頁每頁顯示條數
	 *  @param includes   需要關聯取出的外鍵對象
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> List<E> findAll(Class<E> clasz, String conditions,
			Map<String, Object> params, String order, int limit, String[] includes) {
		return findAll(clasz, conditions, params, order, 0, limit, includes);
	}

	/**
	 * 根據條件查找全部符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param conditions  條件
	 *  @param args         參數
	 *  @param order       排序條件(如"id desc,code")
	 *  @param   start      分頁開始條數
	 *  @param limit         分頁每頁顯示條數
	 *   @param includes    需要關聯取出的外鍵對象
	 *  @return E 查找到的實體
	 */
	public <E> List<E> findAll(final Class<E> clasz, final String conditions, final Map<String, Object> params, final String order, final int start, final int limit, final String[] includes) {		
		String hql = "from " + clasz.getName() + " as e";
		if (includes != null && includes.length > 0) {
			for (String je : includes) {
				hql += " left outer join  fetch e." + je + " as " + (je.replaceAll("\\.", "")) + " ";
			}
		}
		if (StringUtils.isNotEmpty(conditions)) {
			hql += " where (" + DaoHelper.insertAlias(conditions, clasz) + ")";
		}
		if (StringUtils.isNotEmpty(order))
			hql += " order by e." + order;
		
		Query query = this.getCurrentSession().createQuery(hql);
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		return query.setFirstResult(start).setMaxResults((limit == 0 ? Integer.MAX_VALUE : limit)).list();
	}

	/**
	 * 根據條件查找第一個符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param hql  hql語句
	 *  @param args    參數
	 *  @return E 查找到的實體(List集合)
	 */
	public <E> E findFirstByHQL(Class<E> clasz, final String hql, final Map<String, Object> params) {				
		Query query = this.getCurrentSession().createQuery(hql);		
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		List<?> list = query.setFirstResult(0).setMaxResults(1).list();
		return (list != null && list.size() > 0 )? (E)list.get(0) : null;
	}

	/**
	 * HQL查詢根據條件查找符合條件實體類信息。  
	 * @param clasz 待查找的實體類
	 *  @param hql  hql語句(必須完整寫出 from Class)
	 *  @param args    hql 條件(Object數組)
	 *  @return E 查找到的實體(List集合)
	 */
	public  <E> List<E> findByHQL(Class<E> clasz, final String hql, final Map<String, Object> params) {
		Query query = this.getCurrentSession().createQuery(hql);
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		return query.list();
	}

	/**
	 * 標量
	 * HQL查詢根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args   參數
	 *  @return Integer  標量值
	 */
	public Integer findInt(String hql, Map<String, Object> params) {
		Integer intValue = findFirstByHQL(Integer.class, hql, params);
		return intValue == null ? 0 : intValue;
	}

	/**
	 * 標量
	 * HQL查詢根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args   參數
	 *  @return Long  標量值
	 */
	public Long findLong(String hql, Map<String, Object> params) {
		Long longValue = findFirstByHQL(Long.class, hql, params);
		return longValue == null ? 0L : longValue;
	}

	/**
	 * 標量
	 * 根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args    參數
	 *  @return Double 標量值
	 */
	public Double findDouble(String hql, Map<String, Object> params) {
		Double doubleValue = findFirstByHQL(Double.class, hql, params);
		return doubleValue == null ? 0.00 : doubleValue;
	}

	/**
	 * 標量
	 * HQL查詢根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args   參數
	 *  @return BigDecimal 標量值
	 */
	public BigDecimal findBigDecimal(String hql, Map<String, Object> params) {
		BigDecimal bigDecimalValue = findFirstByHQL(BigDecimal.class, hql, params);
		return bigDecimalValue == null ? new BigDecimal(0) : bigDecimalValue;
	}

	/**
	 * 標量
	 * HQL查詢根據條件查找符合條件第一個標量值。  
	 *  @param hql  hql語句
	 *  @param args   參數
	 *  @return String 標量值
	 */
	public String findString(String hql, Map<String, Object> params) {
		String stringValue = findFirstByHQL(String.class, hql, params);
		return stringValue;
	}


	/**
	 * 分頁條件查詢 
	 *  @param DetachedCriteria  dc   條件
	 *  @param start    分頁開始顯示數
	 *  @param limit      分頁每頁顯示條數
	 *  @return Page   分頁對象
	 */
	public  <E> Page<E> findPage(final DetachedCriteria dc, final int start, final int limit) {
		
		Criteria c = dc.getExecutableCriteria(this.getCurrentSession());
		CriteriaImpl impl = (CriteriaImpl) c;
		Projection projection = impl.getProjection();
		ResultTransformer transformer = impl.getResultTransformer();

		
		List<CriteriaImpl.OrderEntry> orderEntries = (List<CriteriaImpl.OrderEntry>) ReflectionUtils.getFieldValue(impl, "orderEntries");
		ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList<CriteriaImpl.OrderEntry>());
		
		
		// 執行Count查詢
		c.setResultTransformer(CriteriaImpl.DISTINCT_ROOT_ENTITY);
		long total = (Long) c.setProjection(Projections.countDistinct("id")).uniqueResult();
	
		// 將之前的Projection和OrderBy條件重新設回去
		c.setProjection(projection);
		c.setResultTransformer(transformer);
		ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
		

		c.setFirstResult(start);
		c.setMaxResults(limit);

		List<E> list = c.list();
		return  new Page<E>(start, limit, Integer.parseInt(String.valueOf(total)),(list == null ? new ArrayList() : list));
	}
	
	public <T> Page<T> findPageByHQL(Class<T> clasz, final String hql, final Map<String, Object> params, final int start, final int limit) {
		String countQueryString = "select count (*) " + DaoHelper.removeSelect(DaoHelper.removeOrders(hql));
		
		Long count = params ==null ? findLong(countQueryString, null):findLong(countQueryString, params);
		
		Query query = this.getCurrentSession().createQuery(hql);
		// 參數組裝
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		List<?> list =  query.setFirstResult(start).setMaxResults(limit).list();
		
		return new Page(start, limit,count.intValue(),list);
	}
	
	
	/**
	 * 根據hql語句查找實體信息
	 *  @param sql    createSQLQuery條件(hql語句)
	 *  @param args     參數
     *  @param start    分頁開始顯示數
	 *  @param limit      分頁每頁顯示條數
	 *  @return  page   分頁對象
	 */
	public <T> Page<T> findPageBySQL(final Class<T> clasz,final String sql, final Map<String, Object> params,final int start, final int limit) {		
		Query query =  this.getCurrentSession().createSQLQuery(sql);
		// 參數組裝
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		// 結果轉換
		query.setResultTransformer(new CustomResultTransformer(clasz));
		
		List<?> list = query.setFirstResult(start).setMaxResults(limit).list();
		
		String countQueryString = "select count (*) " + DaoHelper.removeSelect(DaoHelper.removeOrders(sql));
		Object o = findFirstBySQL(countQueryString,params);
		
		Long count = 0L;
		if(o!=null&&o instanceof BigDecimal){
			count = ((BigDecimal)o).longValue();
		}else if(o!=null){
			count= Long.valueOf(String.valueOf(o));
		}
		else{
			count = 0L;
		}
		return new Page(start, limit,count.intValue(),list);
	}
	
	/**
	 * 根據hql語句查找實體信息
	 *  @param sql    createSQLQuery條件(hql語句)
	 *  @param args    參數
	 *  @param count_sql
     *  @param start    分頁開始顯示數
	 *  @param limit      分頁每頁顯示條數
	 *  @return  page  分頁對象
	 */
	public <T> Page<T> findPageBySQL(final Class<T> clasz,final String sql,String count_sql, final Map<String, Object> params,final int start, final int limit) {
		Query query =  this.getCurrentSession().createSQLQuery(sql);
		// 參數組裝
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		// 結果轉換
		query.setResultTransformer(new CustomResultTransformer(clasz));
		
		List<T> list = query.setFirstResult(start).setMaxResults(limit).list();	
		
		Long count = (Long)findFirstBySQL(count_sql, params);		
	
		return new Page(start, limit,count.intValue(),list);
	}
	
	/**
	 * 根據hql語句查找實體信息
	 *  @param sql    createSQLQuery條件(hql語句)
	 *  @param args    條件參數
	 *  @return List 集合
	 */
	public List<?> findListBySQL(final String sql,final Map<String, Object> params) {
		Query query =  this.getCurrentSession().createSQLQuery(sql);
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		return  query.list();
	}	
	
	/**
	 * 根據hql語句查找第一個實體信息
	 *  @param sql    createSQLQuery條件(hql語句)
	 *  @param args    條件 參數
	 *  @return Object
	 */
	public Object findFirstBySQL(final String sql, final Map<String, Object> params) {
		 
		Query query = this.getCurrentSession().createSQLQuery(sql);
		if ((params != null) && !params.isEmpty()) {
			for (String key : params.keySet()) {
				query.setParameter(key, params.get(key));
			}
		}
		List<?> list =  query.setFirstResult(0).setMaxResults(1).list();
		
		return (list != null && list.size() > 0 ? list.get(0) : null);
	}
	
	
	/**
	 * SQL查詢並將結果集自動轉換成POJO的list
	 * @param clazz 最終轉換成的類
	 * @param sql 查詢條件
	 * @return
	 */
	public <T> List<T> findObjListBySql(final Class<T> clazz,final String sql){
		Query query = this.getCurrentSession().createSQLQuery(sql);
		query.setResultTransformer(new CustomResultTransformer(clazz));
		return  query.list();
	}
		
	/**
	 * SQL查詢並將結果集自動轉換成POJO的list
	 * @param clazz 最終轉換成的類
	 * @param sql 查詢條件	
	 * @return
	 */
	public <T> List<T> findObjListBySql(final Class<T> clazz,final String sql,final Integer start,final Integer limit){
		Query query =  this.getCurrentSession().createSQLQuery(sql).setFirstResult(start==null?0:start).setMaxResults(limit==null?SystemConstant.DEFAULT_PAGE_SIZE:limit);
		query.setResultTransformer(new CustomResultTransformer(clazz));
		return query.list();
	}

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章