文档章节

hibernate5 公用方法

a
 anniweiya
发布于 2017/05/24 22:06
字数 3191
阅读 20
收藏 1

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);
	}
	
}

© 著作权归作者所有

上一篇: Liquibase使用
a
粉丝 0
博文 11
码字总数 11589
作品 0
广州
私信 提问
Spring4+SpringMVC+Hibernate4整合,你可能会遇到的问题

1、java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean 缺少spring-orm jar包,如下: <dependency> <groupId>org.springframework</groupId> ......

阿阿阿阿阿局
2016/08/16
98
0
J2Cache 新增 Hibernate 5 支持,感谢 @tandy 贡献

想了解 J2Cache 是一个什么开源项目,请阅读 这篇博客。 J2Cache 刚新增对 Hibernate 5 的支持,提供了 j2cache-hibernate5 模块,可以方便在 Hibernate 5 中启用 J2Cache 缓存。 该模块由 ...

红薯
2018/09/28
1K
10
activiti 集成进hibernate5 jpa 项目时报错?

@咖啡兔 你好,想跟你请教个问题: activiti 集成进现有的hibernate5作为orm底层框架的项目,使用的是jpa ,然后根据您博客上的讲解配进项目中,可以启动成功,可以发布流程,但是调用activ...

jiong_li
2017/06/01
698
0
jackson-datatype-hibernate4

今天测试时遇到 {"errorMessage":"Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/bytecode/internal/javassist/FieldHandler","status":"er......

have2cow
2018/02/28
0
0
Hibernate5-常用方法

1.创建项目,项目名称hibernatedemo4,目录结构如图所示 2.在项目中创建lib目录存储jar文件,目录结构如图所示 3.在src目录中创建实体Bean Forum,包名(com.mycompany.demo.bean),如图所示 4.实体...

技术小美
2017/11/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Angular 英雄编辑器

应用程序现在有了基本的标题。 接下来你要创建一个新的组件来显示英雄信息并且把这个组件放到应用程序的外壳里去。 创建英雄组件 使用 Angular CLI 创建一个名为 heroes 的新组件。 ng gener...

honeymoose
今天
5
0
Kernel DMA

为什么会有DMA(直接内存访问)?我们知道通常情况下,内存数据跟外设之间的通信是通过cpu来传递的。cpu运行io指令将数据从内存拷贝到外设的io端口,或者从外设的io端口拷贝到内存。由于外设...

yepanl
今天
6
0
hive

一、hive的定义: Hive是一个SQL解析引擎,将SQL语句转译成MR Job,然后再在Hadoop平台上运行,达到快速开发的目的 Hive中的表是纯逻辑表,就只是表的定义,即表的元数据。本质就是Hadoop的目...

霉男纸
今天
5
0
二、Spring Cloud—Eureka(Greenwich.SR1)

注:本系列文章所用工具及版本如下:开发工具(IDEA 2018.3.5),Spring Boot(2.1.3.RELEASE),Spring Cloud(Greenwich.SR1),Maven(3.6.0),JDK(1.8) Eureka: Eureka是Netflix开发...

倪伟伟
昨天
17
0
eclipse常用插件

amaterasUML https://takezoe.github.io/amateras-update-site/ https://github.com/takezoe/amateras-modeler modelGoon https://www.cnblogs.com/aademeng/articles/6890266.html......

大头鬼_yc
昨天
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部