hibernate5 公用方法

hibernate5 公用方法

package cn.anniweiya.dao;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public interface QueryDao {
/**
* 查询记录数量
* @param hql
* @return
*/
public int getCount(final String hql);

/**
* 通过hql查询记录数,hql查询参数格式:a=? and b=?
* @param hql
* @param params
* @return
*/
public int getCount(final String hql, final Object params[]);

/**
* 通过hql查询记录数,hql查询参数格式:name=:name and sex=:sex
* @param hql
* @param params
* @return
*/
public int getCount(final String hql, final Map<String, Object> paramMap);

/**
* 获取某一实体记录数量
* @param clazz
* @return
*/
public int getCount(Class<?> clazz);

/**
* 查询符合某一条件的实体记录数量
* @param clazz
* @param fieldName
* @param fieldValue
* @return
*/
public int getCount(Class<?> clazz, String fieldName, Object fieldValue);

/**
* 查询符合某一条件的实体记录数量
* @param clazz
* @param fieldName
* @param fieldValue
* @return
*/
public int getCount(Class<?> clazz, String[] fieldNames, Object[] fieldValues);

public int getCount(Class<?> clazz, Map<String, Object> parmaMap);

/**
* 实体列表查询
*/
public <T> List<T> getList(final String hql);

public <T> List<T> getList(final String hql, final Object params[]);

public <T> List<T> getList(final String hql, final Map<String, Object> paramMap);

public <T> List<T> getList(final String hql, final int start, final int limit);

public <T> List<T> getList(final String hql, final Object params[], final int start, final int limit);

public <T> List<T> getList(final String hql, final Map<String, Object> paramMap, final int start, final int limit);

public <T> List<T> getList(final String hql, final PaginatedHelper paginatedHelper);

public <T> List<T> getList(final String hql, final Object params[], final PaginatedHelper paginatedHelper);

public <T> List<T> getList(final String hql, final Map<String, Object> paramMap, final PaginatedHelper paginatedHelper);

public <T> List<T> getList(Class<?> clazz);

public <T> List<T> getList(Class<?> clazz, LinkedHashMap<String, String> orderBy);

public <T> List<T> getList(Class<?> clazz, final int start, final int limit, LinkedHashMap<String, String> orderBy);

public <T> List<T> getList(Class<?> clazz, PaginatedHelper paginatedHelper, LinkedHashMap<String, String> orderBy);

public <T> List<T> getList(Class<? extends T> clazz, String fieldName, Object value);

public <T> List<T> getList(Class<? extends T> clazz, String fieldName, Object value, LinkedHashMap<String, String> orderBy);

public <T> List<T> getList(Class<? extends T> clazz, String[] fields, Object values[]);

public <T> List<T> getList(Class<? extends T> clazz, String[] fields, Object values[], LinkedHashMap<String, String> orderBy);

public <T> List<T> getList(Class<?> clazz, final Map<String, Object> paramMap);

public <T> List<T> getList(Class<?> clazz, final Map<String, Object> paramMap, LinkedHashMap<String, String> orderBy);

public <T> List<T> getList(Class<?> clazz, final Map<String, Object> paramMap, final int start, final int limit);

public <T> List<T> getList(Class<?> clazz, final Map<String, Object> paramMap, final int start, final int limit, LinkedHashMap<String, String> orderBy);

public <T> List<T> getList(Class<?> clazz, final Map<String, Object> paramMap, PaginatedHelper paginatedHelper);

public <T> List<T> getList(Class<?> clazz, final Map<String, Object> paramMap, PaginatedHelper paginatedHelper, LinkedHashMap<String, String> orderBy);

public <T> List<T> getList(Class<?> clazz, PaginatedHelper paginatedHelper);

public <T> List<T> getList(Class<?> clazz, int start, int limit);

/**
* 获取实体的某一个属性
* @param clazz
* @param fieldName
* @return
*/
public <T> List<T> getFieldList(Class<?> clazz, String fieldName);

public <T> List<T> getFieldList(Class<?> clazz, String fieldName, Map<String, Object> paramMap);

public <T> List<T> getFieldList(Class<?> clazz, String fieldName, Map<String, Object> paramMap, LinkedHashMap<String, String> orderBy);

public boolean isFieldExist(Class<?> clazz, String fieldName, Object fieldValue, String pkName, Object pkVal);

public boolean isFieldExist(Class<?> clazz, String[] fieldNames, Object fieldValues[], String pkName, Object pkVal);

}

hibernate 公用方法实现类

package cn.anniweiya.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.anniweiya.dao.PaginatedHelper;
import cn.anniweiya.dao.QueryDao;
import cn.anniweiya.util.EstimateTypeUtil;

@Repository("queryDaoImpl")
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class QueryDaoImpl implements QueryDao {
	
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	public int getCount(String hql) {
		return getCount(hql, new Object[] {});
	}
	
	public int getCount(final String hql, final Object[] params) {
		Query query = sessionFactory.openSession().createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		return Integer.parseInt(String.valueOf(query.uniqueResult()));
	}
	
	public int getCount(String hql, Map<String, Object> paramMap) {
		Query query = sessionFactory.openSession().createQuery(hql);
		if (paramMap != null && paramMap.size() > 0) {
			for (Map.Entry<String, Object> en : paramMap.entrySet()) {
				String key = en.getKey();
				Object value = en.getValue();
				if (EstimateTypeUtil.isArray(value)) {
					query.setParameterList(key, (Object[]) value);
				} else if (EstimateTypeUtil.isCollection(value)) {
					query.setParameterList(key, (Collection<?>) value);
				} else {
					query.setParameter(key, value);
				}
			}
		}
		return Integer.parseInt(String.valueOf(query.uniqueResult()));
	}
	
	public int getCount(Class<?> clazz) {
		return getCount(clazz, new String[] {}, new String[] {});
	}
	
	public int getCount(Class<?> clazz, String fieldName, Object fieldValue) {
		if (fieldName != null && !fieldName.equals("")) {
			return getCount(clazz, new String[] { fieldName }, new Object[] { fieldValue });
		}
		return getCount(clazz, new String[] {}, new String[] {});
	}
	
	public int getCount(Class<?> clazz, String[] fieldNames, Object[] fieldValues) {
		StringBuffer hql = new StringBuffer();
		hql.append("select count(*) from ").append(clazz.getName()).append(" where 1=1");
		if (fieldNames != null && fieldNames.length > 0 && fieldValues != null && fieldValues.length > 0) {
			for (String name : fieldNames) {
				hql.append(" and ").append(name).append("=?");
			}
		}
		return getCount(hql.toString(), fieldValues);
	}
	
	public int getCount(Class<?> clazz, Map<String, Object> paramMap) {
		StringBuffer hql = new StringBuffer();
		hql.append("select count(*) from ").append(clazz.getName()).append(" where 1=1");
		if (paramMap != null && paramMap.size() > 0) {
			List<String> removeKeys = new ArrayList<String>();
			Map<String, Object> replacedFields = new HashMap<String, Object>();
			for (Map.Entry<String, Object> en : paramMap.entrySet()) {
				String valueName = en.getKey();
				if (en.getKey().indexOf(".") > -1) {
					removeKeys.add(en.getKey());
					valueName = en.getKey().replaceAll("[.]", "_");
					replacedFields.put(valueName, en.getValue());
				}
				if (EstimateTypeUtil.isArray(en.getValue()) || EstimateTypeUtil.isCollection(en.getValue())) {
					hql.append(" and ").append(en.getKey()).append(" in (:").append(valueName).append(")");
				} else {
					hql.append(" and ").append(en.getKey()).append("=:").append(valueName);
				}
			}
			for (String key : removeKeys) {
				paramMap.remove(key);
			}
			paramMap.putAll(replacedFields);
		}
		return getCount(hql.toString(), paramMap);
	}
	
	public <T> List<T> getList(String hql) {
		return getList(hql, -1, -1);
	}
	
	public <T> List<T> getList(String hql, Object[] params) {
		return getList(hql, params, -1, -1);
	}
	
	public <T> List<T> getList(String hql, Map<String, Object> paramMap) {
		return getList(hql, paramMap, -1, -1);
	}
	
	public <T> List<T> getList(String hql, int start, int limit) {
		return getList(hql, new Object[] {}, start, limit);
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getList(String hql, Object[] params, int start, int limit) {
		Query query = sessionFactory.openSession().createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		if (start > -1 && limit > -1) {
			query.setMaxResults(limit);
			query.setFirstResult(start);
		}
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getList(String hql, Map<String, Object> paramMap, int start, int limit) {
		Query query = sessionFactory.openSession().createQuery(hql);
		if (paramMap != null && paramMap.size() > 0) {
			for (Map.Entry<String, Object> en : paramMap.entrySet()) {
				String key = en.getKey();
				Object value = en.getValue();
				if (EstimateTypeUtil.isArray(value)) {
					query.setParameterList(key, (Object[]) value);
				} else if (EstimateTypeUtil.isCollection(value)) {
					query.setParameterList(key, (Collection<?>) value);
				} else {
					query.setParameter(key, value);
				}
			}
		}
		if (start > -1 && limit > -1) {
			query.setMaxResults(limit);
			query.setFirstResult(start);
		}
		return query.list();
	}
	
	public <T> List<T> getList(String hql, PaginatedHelper paginatedHelper) {
		return getList(hql, new Object[] {}, paginatedHelper);
	}
	
	public <T> List<T> getList(String hql, Object[] params, PaginatedHelper paginatedHelper) {
		return getList(hql, params, paginatedHelper.getStartIndex(), paginatedHelper.getPageSize());
	}
	
	public <T> List<T> getList(String hql, Map<String, Object> paramMap, PaginatedHelper paginatedHelper) {
		return getList(hql, paramMap, paginatedHelper.getStartIndex(), paginatedHelper.getPageSize());
	}
	
	public <T> List<T> getList(Class<?> clazz) {
		return getList(clazz, null, -1, -1, null);
	}
	
	public <T> List<T> getList(Class<?> clazz, LinkedHashMap<String, String> orderBy) {
		return getList(clazz, null, -1, -1, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, int start, int limit, LinkedHashMap<String, String> orderBy) {
		return getList(clazz, null, start, limit, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, PaginatedHelper paginatedHelper, LinkedHashMap<String, String> orderBy) {
		return getList(clazz, null, paginatedHelper.getStartIndex(), paginatedHelper.getPageSize(), orderBy);
	}
	
	public <T> List<T> getList(Class<? extends T> clazz, String fieldName, Object value) {
		return getList(clazz, new String[] { fieldName }, new Object[] { value });
	}
	
	public <T> List<T> getList(Class<? extends T> clazz, String fieldName, Object value, LinkedHashMap<String, String> orderBy) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (fieldName != null && !fieldName.equals("") && value != null) {
			paramMap.put(fieldName, value);
		}
		return getList(clazz, paramMap, orderBy);
	}
	
	public <T> List<T> getList(Class<? extends T> clazz, String[] fields, Object[] values) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (fields != null && fields.length > 0) {
			for (int i = 0; i < fields.length; i++) {
				paramMap.put(fields[i], values[i]);
			}
		}
		return getList(clazz, paramMap);
	}
	
	public <T> List<T> getList(Class<? extends T> clazz, String[] fields, Object[] values, LinkedHashMap<String, String> orderBy) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (fields != null && fields.length > 0 && fields.length == values.length && values != null && values.length > 0) {
			for (int i = 0; i < fields.length; i++) {
				paramMap.put(fields[i], values[i]);
			}
		}
		return getList(clazz, paramMap, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap) {
		return getList(clazz, paramMap, -1, -1);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, LinkedHashMap<String, String> orderBy) {
		return getList(clazz, paramMap, -1, -1, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, int start, int limit) {
		return getList(clazz, paramMap, start, limit, null);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, int start, int limit, LinkedHashMap<String, String> orderBy) {
		StringBuffer hql = new StringBuffer();
		hql.append("from ").append(clazz.getName()).append(" where 1=1  ");
		if (paramMap != null && paramMap.size() > 0) {
			Map<String, Object> replacedFields = new HashMap<String, Object>();
			List<String> removeKeys = new ArrayList<String>();
			for (Map.Entry<String, Object> en : paramMap.entrySet()) {
				String valueName = en.getKey();
				if (valueName.indexOf(".") > -1) {
					removeKeys.add(en.getKey());
					valueName = en.getKey().replaceAll("[.]", "_");
					replacedFields.put(valueName, en.getValue());
				}
				if (EstimateTypeUtil.isArray(en.getValue()) || EstimateTypeUtil.isCollection(en.getValue())) {
					hql.append(" and ").append(en.getKey()).append(" in (:").append(valueName).append(")");
				} else {
					hql.append(" and ").append(en.getKey()).append("=:").append(valueName);
				}
			}
			for (String key : removeKeys) {
				paramMap.remove(key);
			}
			paramMap.putAll(replacedFields);
		}
		if (orderBy != null && orderBy.size() > 0) {
			hql.append(" order by ");
			for (Map.Entry<String, String> en : orderBy.entrySet()) {
				hql.append(en.getKey()).append(" ").append(en.getValue()).append(", ");
			}
			hql.deleteCharAt(hql.lastIndexOf(","));
		}
		
		return getList(hql.toString(), paramMap, start, limit);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, PaginatedHelper paginatedHelper) {
		return getList(clazz, paramMap, paginatedHelper.getStartIndex(), paginatedHelper.getPageSize());
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, PaginatedHelper paginatedHelper, LinkedHashMap<String, String> orderBy) {
		return getList(clazz, null, paginatedHelper.getStartIndex(), paginatedHelper.getPageSize(), orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, PaginatedHelper paginatedHelper) {
		return getList(clazz, paginatedHelper, null);
	}
	
	public <T> List<T> getList(Class<?> clazz, int start, int limit) {
		return getList(clazz, start, limit, null);
	}
	
	public <T> List<T> getFieldList(Class<?> clazz, String fieldName) {
		return getFieldList(clazz, fieldName, null);
	}
	
	public <T> List<T> getFieldList(Class<?> clazz, String fieldName, Map<String, Object> paramMap) {
		return getFieldList(clazz, fieldName, paramMap, null);
	}
	
	public <T> List<T> getFieldList(Class<?> clazz, String fieldName, Map<String, Object> paramMap, LinkedHashMap<String, String> orderBy) {
		StringBuffer hql = new StringBuffer();
		hql.append("select ").append(fieldName).append(" from ").append(clazz.getName()).append(" where 1=1 ");
		if (paramMap != null && paramMap.size() > 0) {
			Map<String, Object> replacedFields = new HashMap<String, Object>();
			List<String> removeKeys = new ArrayList<String>();
			for (Map.Entry<String, Object> en : paramMap.entrySet()) {
				String valueName = en.getKey();
				if (valueName.indexOf(".") > -1) {
					removeKeys.add(en.getKey());
					valueName = en.getKey().replaceAll("[.]", "_");
					replacedFields.put(valueName, en.getValue());
				}
				if (EstimateTypeUtil.isArray(en.getValue()) || EstimateTypeUtil.isCollection(en.getValue())) {
					hql.append(" and ").append(en.getKey()).append(" in (:").append(valueName).append(")");
				} else {
					hql.append(" and ").append(en.getKey()).append("=:").append(valueName);
				}
			}
			for (String key : removeKeys) {
				paramMap.remove(key);
			}
			paramMap.putAll(replacedFields);
		}
		
		if (orderBy != null && orderBy.size() > 0) {
			hql.append(" order by ");
			for (Map.Entry<String, String> en : orderBy.entrySet()) {
				hql.append(en.getKey()).append(" ").append(en.getValue()).append(", ");
			}
			hql.deleteCharAt(hql.lastIndexOf(","));
		}
		return getList(hql.toString(), paramMap, -1, -1);
	}
	
	public boolean isFieldExist(Class<?> clazz, String fieldName, Object fieldValue, String pkName, Object pkVal) {
		StringBuffer hql = new StringBuffer("select count(*) from ");
		hql.append(clazz.getName()).append(" where 1=1  and ").append(fieldName).append("=?");
		List<Object> params = new ArrayList<Object>();
		params.add(fieldValue);
		if (pkVal != null) {
			hql.append(" and ").append(pkName).append("!=?");
			params.add(pkVal);
		}
		int count = getCount(hql.toString(), params.toArray());
		return count > 0 ? true : false;
	}
	
	public boolean isFieldExist(Class<?> clazz, String[] fieldNames, Object[] fieldValues, String pkName, Object pkVal) {
		StringBuffer hql = new StringBuffer("select count(*) from " + clazz.getName() + " where 1=1  ");
		List<Object> params = new ArrayList<Object>();
		for (int i = 0; i < fieldNames.length; i++) {
			hql.append(" and ").append(fieldNames[i]).append("=? ");
			params.add(fieldValues[i]);
		}
		
		if (pkVal != null) {
			hql.append(" and ").append(pkName).append("!=?");
			params.add(pkVal);
		}
		int count = getCount(hql.toString(), params.toArray());
		return count > 0 ? true : false;
	}
}

扩展类

package cn.anniweiya.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.anniweiya.dao.PaginatedHelper;
import cn.anniweiya.dao.QueryDao;
import cn.anniweiya.hql.HqlUtil;
import cn.anniweiya.util.EstimateTypeUtil;
import cn.anniweiya.util.VoUtil;

@Transactional(propagation = Propagation.NOT_SUPPORTED)
@Service
public class BaseDao implements QueryDao {
	
	@Resource(name = "queryDaoImpl")
	private QueryDao queryDao;
	
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	/**
	 * 通过主键,获取一个数据库映射的实例对象
	 * 
	 * @param clazz
	 * @param id
	 * @return
	 */
	public <T> T getTarget(Class<T> clazz, Serializable id) {
		return sessionFactory.openSession().get(clazz, id);
	}
	
	/**
	 * 通过某个属性,获取一个数据库映射的实例对象
	 */
	public <T> T getTarget(Class<? extends T> clazz, String uniqueName, Object value) {
		return getTarget(clazz, new String[] { uniqueName }, new Object[] { value });
	}
	
	/**
	 * 通过某个属性,获取一个数据库映射的实例对象
	 */
	public <T> T getTarget(Class<? extends T> clazz, String[] fields, Object values[]) {
		return VoUtil.returnOneTarget(getList(clazz, fields, values));
	}
	
	/**
	 * 保存实体
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void saveTarget(Object entity) {
		sessionFactory.openSession().save(entity);
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void saveOrUpdate(Object entity) {
		sessionFactory.openSession().saveOrUpdate(entity);
	}
	
	/**
	 * 更新实体
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateTarget(Object entity) {
		sessionFactory.openSession().update(entity);
	}
	
	/**
	 * 删除实体
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void delete(Object entity) {
		sessionFactory.openSession().delete(entity);
	}
	
	/**
	 * 通过主键,删除一个数据库实例
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteTarget(Class<?> target, String pkName, Serializable id) {
		StringBuffer queryString = new StringBuffer();
		queryString.append(" DELETE FROM " + target.getName() + " a where a." + pkName + "=? ");
		sessionFactory.openSession().update(queryString.toString(), new Object[] { id });
	}
	
	/**
	 * 通过主键,删除一个数据库实例
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteTarget(Class<?> target, String pkName, Object[] ids) {
		if (ids != null && ids.length > 0) {
			StringBuffer queryString = new StringBuffer();
			queryString.append(" DELETE FROM " + target.getName() + " a where ").append(" a.").append(pkName).append(" in ").append(HqlUtil.createInHql(ids.length));
			sessionFactory.openSession().saveOrUpdate(queryString.toString(), ids);
		}
		
	}
	
	/**
	 * 通过主键,删除数据库实例
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteTarget(Class<?> target, String fieldName, Object fieldValue) {
		if (fieldName != null && !fieldName.equals("") && fieldValue != null) {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put(fieldName, fieldValue);
			deleteTarget(target, paramMap);
		}
		
	}
	
	/**
	 * 删除实体
	 * 
	 * @param clazz
	 * @param paramMap
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteTarget(Class<?> clazz, final Map<String, Object> paramMap) {
		if (paramMap != null && paramMap.size() > 0) {
			final StringBuffer queryString = new StringBuffer();
			queryString.append(" DELETE FROM " + clazz.getName() + " a where 1=1 ");
			/*
			 * for (Map.Entry<String, Object> en : paramMap.entrySet()) {
			 * queryString.append(" and "
			 * ).append(en.getKey()).append("=:").append(en.getKey()); }
			 */
			if (paramMap != null && paramMap.size() > 0) {
				Map<String, Object> replacedFields = new HashMap<String, Object>();
				List<String> removeKeys = new ArrayList<String>();
				for (Map.Entry<String, Object> en : paramMap.entrySet()) {
					String valueName = en.getKey();
					if (valueName.indexOf(".") > -1) {
						removeKeys.add(en.getKey());
						valueName = en.getKey().replaceAll("[.]", "_");
						replacedFields.put(valueName, en.getValue());
					}
					if (EstimateTypeUtil.isArray(en.getValue()) || EstimateTypeUtil.isCollection(en.getValue())) {
						queryString.append(" and ").append(en.getKey()).append(" in (:").append(valueName).append(")");
					} else {
						queryString.append(" and ").append(en.getKey()).append("=:").append(valueName);
					}
				}
				for (String key : removeKeys) {
					paramMap.remove(key);
				}
				paramMap.putAll(replacedFields);
			}
			Query query = sessionFactory.openSession().createQuery(queryString.toString());
			for (Map.Entry<String, Object> en : paramMap.entrySet()) {
				String key = en.getKey();
				Object value = en.getValue();
				if (EstimateTypeUtil.isArray(value)) {
					query.setParameterList(key, (Object[]) value);
				} else if (EstimateTypeUtil.isCollection(value)) {
					query.setParameterList(key, (Collection<?>) value);
				} else {
					query.setParameter(key, value);
				}
			}
			query.executeUpdate();
		}
	}
	
	/**
	 * 清空实体映射表的数据
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void clearTableData(Class<?> target) {
		StringBuffer queryString = new StringBuffer();
		queryString.append(" DELETE FROM " + target.getName() + " a where 1=1 ");
		sessionFactory.openSession().saveOrUpdate(queryString.toString());
	}
	
	public int getCount(String hql) {
		return queryDao.getCount(hql);
	}
	
	public int getCount(String hql, Object[] params) {
		return queryDao.getCount(hql, params);
	}
	
	public int getCount(String hql, Map<String, Object> paramMap) {
		return queryDao.getCount(hql, paramMap);
	}
	
	public int getCount(Class<?> clazz) {
		return queryDao.getCount(clazz);
	}
	
	public int getCount(Class<?> clazz, String fieldName, Object fieldValue) {
		return queryDao.getCount(clazz, fieldName, fieldValue);
	}
	
	public int getCount(Class<?> clazz, String[] fieldNames, Object[] fieldValues) {
		return queryDao.getCount(clazz, fieldNames, fieldValues);
	}
	
	public int getCount(Class<?> clazz, Map<String, Object> parmaMap) {
		return queryDao.getCount(clazz, parmaMap);
	}
	
	/**
	 * [分页]列表查询
	 */
	
	public <T> List<T> getList(String hql) {
		return queryDao.getList(hql);
	}
	
	public <T> List<T> getList(String hql, Object[] params) {
		return queryDao.getList(hql, params);
	}
	
	public <T> List<T> getList(String hql, Map<String, Object> paramMap) {
		return queryDao.getList(hql, paramMap);
	}
	
	public <T> List<T> getList(String hql, int start, int limit) {
		return queryDao.getList(hql, start, limit);
	}
	
	public <T> List<T> getList(String hql, Object[] params, int start, int limit) {
		return queryDao.getList(hql, params, start, limit);
	}
	
	public <T> List<T> getList(String hql, Map<String, Object> paramMap, int start, int limit) {
		return queryDao.getList(hql, paramMap, start, limit);
	}
	
	public <T> List<T> getList(String hql, PaginatedHelper paginatedHelper) {
		return queryDao.getList(hql, paginatedHelper);
	}
	
	public <T> List<T> getList(String hql, Object[] params, PaginatedHelper paginatedHelper) {
		return queryDao.getList(hql, params, paginatedHelper);
	}
	
	public <T> List<T> getList(String hql, Map<String, Object> paramMap, PaginatedHelper paginatedHelper) {
		return queryDao.getList(hql, paramMap, paginatedHelper);
	}
	
	public <T> List<T> getList(Class<?> clazz) {
		return queryDao.getList(clazz);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap) {
		return queryDao.getList(clazz, paramMap);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, int start, int limit) {
		return queryDao.getList(clazz, paramMap, start, limit);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, PaginatedHelper paginatedHelper) {
		return queryDao.getList(clazz, paramMap, paginatedHelper);
	}
	
	public <T> List<T> getList(Class<? extends T> clazz, String fieldName, Object value) {
		return queryDao.getList(clazz, fieldName, value);
	}
	
	public <T> List<T> getList(Class<? extends T> clazz, String[] fields, Object[] values) {
		return queryDao.getList(clazz, fields, values);
	}
	
	public <T> List<T> getList(Class<?> clazz, LinkedHashMap<String, String> orderBy) {
		return queryDao.getList(clazz, orderBy);
	}
	
	public <T> List<T> getList(Class<? extends T> clazz, String fieldName, Object value, LinkedHashMap<String, String> orderBy) {
		return queryDao.getList(clazz, fieldName, value, orderBy);
	}
	
	public <T> List<T> getList(Class<? extends T> clazz, String[] fields, Object[] values, LinkedHashMap<String, String> orderBy) {
		return queryDao.getList(clazz, fields, values, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, LinkedHashMap<String, String> orderBy) {
		return queryDao.getList(clazz, paramMap, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, int start, int limit, LinkedHashMap<String, String> orderBy) {
		return queryDao.getList(clazz, paramMap, start, limit, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, Map<String, Object> paramMap, PaginatedHelper paginatedHelper, LinkedHashMap<String, String> orderBy) {
		return queryDao.getList(clazz, paramMap, paginatedHelper, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, int start, int limit, LinkedHashMap<String, String> orderBy) {
		return queryDao.getList(clazz, start, limit, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, PaginatedHelper paginatedHelper, LinkedHashMap<String, String> orderBy) {
		return queryDao.getList(clazz, paginatedHelper, orderBy);
	}
	
	public <T> List<T> getList(Class<?> clazz, int start, int limit) {
		return queryDao.getList(clazz, start, limit);
	}
	
	public <T> List<T> getList(Class<?> clazz, PaginatedHelper paginatedHelper) {
		return queryDao.getList(clazz, paginatedHelper);
	}
	
	public <T> List<T> getFieldList(Class<?> clazz, String fieldName) {
		return queryDao.getFieldList(clazz, fieldName);
	}
	
	public <T> List<T> getFieldList(Class<?> clazz, String fieldName, Map<String, Object> paramMap) {
		return queryDao.getFieldList(clazz, fieldName, paramMap);
	}
	
	public <T> List<T> getFieldList(Class<?> clazz, String fieldName, Map<String, Object> paramMap, LinkedHashMap<String, String> orderBy) {
		return queryDao.getFieldList(clazz, fieldName, paramMap, orderBy);
	}
	
	public boolean isFieldExist(Class<?> clazz, String fieldName, Object fieldValue, String pkName, Object pkVal) {
		return queryDao.isFieldExist(clazz, fieldName, fieldValue, pkName, pkVal);
	}
	
	public boolean isFieldExist(Class<?> clazz, String[] fieldNames, Object[] fieldValues, String pkName, Object pkVal) {
		return queryDao.isFieldExist(clazz, fieldNames, fieldValues, pkName, pkVal);
	}
	
}

转载于:https://my.oschina.net/u/1252721/blog/908485

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>