标签:
package org.dao;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 通用数据访问层
*/
public interface BaseDao<T> {
/**
* 保存单个实体
* @param entity
*/
public void save(T entity);
/**
* 级联保存单个实体对象
* @param entity
*/
public void persist(T entity);
/**
* 修改一个实体对象
* @param entity
*/
public void update(T entity);
/**
* 级联修改单个实体对象
* @param entity
*/
public void merge(T entity);
/**
* 删除一个实体对象
* @param id
*/
public void remove(Serializable id);
/**
* 根据id查询单个对象
* @param id 实体唯一标识id,使用Serializable类型的id,实际传入的id类型可以是八种基本数据类型
* @return
*/
public T get(Serializable id);
/**
* 按条件查询单个对象
* @param where 查询条件
* @param params 查询条件中的传入值
* @return 单个实体对象
*/
public T get(String where,Object...params);
/**
* 按条件查询单个对象的单个属性
* @param returnType 属性的类型
* @param propName 属性的名称
* @param where 查询条件
* @param params 查询条件中的传入值
* @return 属性的返回类型
*/
public <E> E get(Class<E> returnType,String propName,String where,Object...params);
/**
* 查询总行数
* @return
*/
public Integer count();
/**
* 按条件查询总行数
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public Integer count(String where,Object...params);
/**
* 按条件查询总行数
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public Integer count(String where,Map<String, Object> params);
/**
* hql查询总行数
* @param hql hql查询语句
* @param params 查询语句中的传入值
* @return
*/
public Integer countByHQL(String hql,Object...params);
/**
* hql查询总行数
* @param hql hql查询语句
* @param params 查询语句中的传入值
* @return
*/
public Integer countByHQL(String hql,Map<String, Object> params);
/**
* 查询所有
* @return
*/
public List<T> qeuryList();
/**
* 按查询条件查询所有
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public List<T> queryList(String where,Object...params);
/**
* 按查询条件查询所有
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public List<T> queryList(String where,Map<String, Object> params);
/**
* 分页查询所有
* @param pageIndex 页码
* @param pageSize 页大小
* @return
*/
public List<T> queryList(Integer pageIndex,Integer pageSize);
/**
* 按查询条件查询,并分页
* @param pageIndex 页码
* @param pageSize 页大小
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public List<T> queryList(Integer pageIndex,Integer pageSize,String where,Object...params);
/**
* 按查询条件查询,并分页
* @param pageIndex 页码
* @param pageSize 页大小
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public List<T> queryList(Integer pageIndex,Integer pageSize,String where,Map<String, Object> params);
/**
* 查询所有并排序
* @param orderBy 键:需要排序的列名; 值:排序的方式,即:降序,升序
* @return
*/
public List<T> queryList(LinkedHashMap<String, String> orderBy);
/**
* 按条件查询所有,并排序
* @param orderBy 键:需要排序的列名; 值:排序的方式,即:降序,升序
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public List<T> queryList(LinkedHashMap<String, String> orderBy,String where,Object...params);
/**
* 按条件查询所有,并排序
* @param orderBy 键:需要排序的列名; 值:排序的方式,即:降序,升序
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public List<T> queryList(LinkedHashMap<String, String> orderBy,String where,Map<String, Object> params);
/**
* 分页查询所有,并排序
* @param pageIndex 页码
* @param pageSize 页大小
* @param orderBy 键:需要排序的列名; 值:排序的方式,即:降序,升序
* @return
*/
public List<T> queryList(Integer pageIndex,Integer pageSize,LinkedHashMap<String, String> orderBy);
/**
* 按条件分页查询,并排序
* @param pageIndex 页码
* @param pageSize 页大小
* @param orderBy 键:需要排序的列名; 值:排序的方式,即:降序,升序
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public List<T> queryList(Integer pageIndex,Integer pageSize,LinkedHashMap<String, String> orderBy,String where,Object...params);
/**
* 按条件分页查询,并排序
* @param pageIndex 页码
* @param pageSize 页大小
* @param orderBy 键:需要排序的列名; 值:排序的方式,即:降序,升序
* @param where 查询条件
* @param params 查询条件中的传入值
* @return
*/
public List<T> queryList(Integer pageIndex,Integer pageSize,LinkedHashMap<String, String> orderBy,String where,Map<String, Object> params);
/**
* hql查询所有
* @param hql hql查询语句
* @param params 查询语句中的传入值
* @return
*/
public List<T> queryListByHQL(String hql,Object...params);
/**
* hql查询所有
* @param hql hql查询语句
* @param params 查询语句中的传入值
* @return
*/
public List<T> queryListByHQL(String hql,Map<String, Object> params);
/**
* hql分页查询
* @param pageIndex 页码
* @param pageSize 页大小
* @param hql hql查询语句
* @param params 查询语句中的传入值
* @return
*/
public List<T> queryListByHQL(Integer pageIndex,Integer pageSize,String hql,Object...params);
/**
* hql分页查询
* @param pageIndex 页码
* @param pageSize 页大小
* @param hql hql查询语句
* @param params 查询语句中的传入值
* @return
*/
public List<T> queryListByHQL(Integer pageIndex,Integer pageSize,String hql,Map<String, Object> params);
}
package org.dao.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dao.BaseDao;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ResolvableType;
@SuppressWarnings({"unchecked"})
public abstract class BaseDaoSupport<T> implements BaseDao<T> {
private Class<?> entityClass = ResolvableType.forClass(this.getClass()).getSuperType().getGeneric(0).resolve();
@Autowired
private SessionFactory sessionFactory;
/**
* 获取当前Session,获取失败则开启一个Session
* @return
*/
protected Session getSession() {
try {
return sessionFactory.getCurrentSession();
} catch (HibernateException e) {
return sessionFactory.openSession();
}
}
protected static class Util {
//构建Where语句
public static String builWhere(String where){
return (where == null || "".equals(where)) ? "" : " where " + where;
}
public static List<String> getParamName(String hql){
List<String> list = new ArrayList<String>();
hql = hql.replaceAll("\\("," ( ").replaceAll("\\)", " ) ");
String[] arrays = hql.split(" ");
for (String str : arrays) {
if (str.contains(":")) {
list.add(str.substring(str.indexOf(":") + 1).replace(",", ""));
}
}
return list;
}
/**
* 设置查询参数
* @param query 查询接口
* @param params 查询参数
*/
private static void setParamValue(Query query,Object...params) {
String hql = query.getQueryString();
List<String> paramNames = getParamName(hql);
int pc = paramNames.size();
int vc = params.length;
if (pc != vc) {
throw new RuntimeException("命名参数个数为" + pc + ",而实参个数为" + vc + "。命名参数个数必须等于实参个数!");
}
for (int i = 0; i < paramNames.size(); i++) {
query.setParameter(paramNames.get(i), params[i]);
}
}
private static void setParamValue(Query query,Map<String, Object> params) {
String hql = query.getQueryString();
List<String> paramNames = getParamName(hql);
int pc = paramNames.size();
int vc = params.size();
if (pc != vc) {
throw new RuntimeException("命名参数个数为" + pc + ",而实参个数为" + vc + "。命名参数个数必须等于实参个数!");
}
Set<String> paramKeys = params.keySet();
for (String paramName : paramNames) {
if (!paramKeys.contains(paramName)) {
throw new RuntimeException("命名参数" + paramName + ",在实参键集合" + paramKeys + "中不存在!");
}
}
for (int i = 0; i < paramNames.size(); i++) {
String paramName = paramNames.get(i);
query.setParameter(paramName, params.get(paramName));
}
}
//构建排序
private static String buildOrderBy(LinkedHashMap<String, String> orderBy){
StringBuffer buffer = new StringBuffer();
if (orderBy != null && orderBy.size() > 0) {
buffer.append(" order by ");
for (String key : orderBy.keySet()) {
buffer.append(key).append(" ").append(orderBy.get(key)).append(",");
buffer.deleteCharAt(buffer.length() - 1);//删除指定位置的字符
}
}
return buffer.toString();
}
/**
* 设置分页参数
* @param query 查询接口
* @param pageIndex 页码
* @param pageSize 页大小
*/
private static void setPagination(Query query,Integer pageIndex,Integer pageSize) {
if (pageIndex != null && pageSize != null) {
query.setFirstResult(pageSize * (pageIndex - 1));
query.setMaxResults(pageSize);
}
}
}
@Override
public void save(T entity) {
getSession().save(entity);
}
@Override
public void persist(T entity) {
getSession().persist(entity);
}
@Override
public void update(T entity) {
getSession().update(entity);
}
@Override
public void merge(T entity) {
getSession().merge(entity);
}
@Override
public void remove(Serializable id) {
T t = get(id);
if (t != null) {
getSession().delete(t);
}
}
@Override
public T get(Serializable id) {
return (T) getSession().get(entityClass, id);
}
@Override
public T get(String where, Object... params) {
String hql = "from " + entityClass.getSimpleName() + Util.builWhere(where);
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
return (T) query.uniqueResult();
}
@Override
public <E> E get(Class<E> returnType, String propName, String where, Object... params) {
String hql = "select " + propName + "from " + entityClass.getSimpleName() + Util.builWhere(where);
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
return returnType.cast(query.uniqueResult());
}
@Override
public Integer count() {
return count(null);
}
@Override
public Integer count(String where, Object... params) {
String hql = "select count(o) from " + entityClass.getSimpleName() +" o " + Util.builWhere(where);
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
return Integer.parseInt(String.valueOf(query.uniqueResult()));
}
@Override
public Integer count(String where, Map<String, Object> params) {
String hql = "select count(o) from " + entityClass.getSimpleName() +" o "+ Util.builWhere(where);
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
return Integer.parseInt(String.valueOf(query.uniqueResult()));
}
@Override
public Integer countByHQL(String hql, Object... params) {
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
return Integer.parseInt(String.valueOf(query.uniqueResult()));
}
@Override
public Integer countByHQL(String hql, Map<String, Object> params) {
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
return Integer.parseInt(String.valueOf(query.uniqueResult()));
}
@Override
public List<T> qeuryList() {
return queryList(null);
}
@Override
public List<T> queryList(String where, Object... params) {
return queryList(null, null, where, params);
}
@Override
public List<T> queryList(String where, Map<String, Object> params) {
return queryList(null, null, where, params);
}
@Override
public List<T> queryList(Integer pageIndex, Integer pageSize) {
return queryList(pageIndex, pageSize, null);
}
@Override
public List<T> queryList(Integer pageIndex, Integer pageSize, String where, Object... params) {
return queryList(pageIndex, pageSize, null, where, params);
}
@Override
public List<T> queryList(Integer pageIndex, Integer pageSize, String where, Map<String, Object> params) {
return queryList(pageIndex, pageSize, null, where, params);
}
@Override
public List<T> queryList(LinkedHashMap<String, String> orderBy) {
return queryList(null, null, orderBy);
}
@Override
public List<T> queryList(LinkedHashMap<String, String> orderBy, String where, Object... params) {
return queryList(null, null, orderBy, where, params);
}
@Override
public List<T> queryList(LinkedHashMap<String, String> orderBy, String where, Map<String, Object> params) {
return queryList(null, null, orderBy, where, params);
}
@Override
public List<T> queryList(Integer pageIndex, Integer pageSize, LinkedHashMap<String, String> orderBy) {
return queryList(pageIndex, pageSize, orderBy, null);
}
@Override
public List<T> queryList(Integer pageIndex, Integer pageSize, LinkedHashMap<String, String> orderBy, String where, Object... params) {
String hql = "from " + entityClass.getSimpleName() +"where " + Util.builWhere(where) + " order by " + Util.buildOrderBy(orderBy);
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
Util.setPagination(query, pageIndex, pageSize);
return query.list();
}
@Override
public List<T> queryList(Integer pageIndex, Integer pageSize, LinkedHashMap<String, String> orderBy, String where, Map<String, Object> params) {
String hql = "from " + entityClass.getSimpleName() +"where " + Util.builWhere(where) + " order by " + Util.buildOrderBy(orderBy);
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
Util.setPagination(query, pageIndex, pageSize);
return query.list();
}
@Override
public List<T> queryListByHQL(String hql, Object... params) {
return queryListByHQL(null, null, hql, params);
}
@Override
public List<T> queryListByHQL(String hql, Map<String, Object> params) {
return queryListByHQL(null, null, hql, params);
}
@Override
public List<T> queryListByHQL(Integer pageIndex, Integer pageSize, String hql, Object... params) {
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
Util.setPagination(query, pageIndex, pageSize);
return query.list();
}
@Override
public List<T> queryListByHQL(Integer pageIndex, Integer pageSize, String hql, Map<String, Object> params) {
Query query = getSession().createQuery(hql);
Util.setParamValue(query, params);
Util.setPagination(query, pageIndex, pageSize);
return query.list();
}
}
package org.dao.impl;
import java.io.Serializable;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Set;
import org.dao.BaseDao;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.ResolvableType;
@SuppressWarnings({"unchecked"})public abstract class BaseDaoSupport<T> implements BaseDao<T> {private Class<?> entityClass = ResolvableType.forClass(this.getClass()).getSuperType().getGeneric(0).resolve();@Autowiredprivate SessionFactory sessionFactory;/** * 获取当前Session,获取失败则开启一个Session * @return */protected Session getSession() {try {return sessionFactory.getCurrentSession();} catch (HibernateException e) {return sessionFactory.openSession();}}protected static class Util {//构建Where语句public static String builWhere(String where){return (where == null || "".equals(where)) ? "" : " where " + where;}public static List<String> getParamName(String hql){List<String> list = new ArrayList<String>();hql = hql.replaceAll("\\("," ( ").replaceAll("\\)", " ) ");String[] arrays = hql.split(" ");for (String str : arrays) {if (str.contains(":")) {list.add(str.substring(str.indexOf(":") + 1).replace(",", ""));}}return list;}/** * 设置查询参数 * @param query 查询接口 * @param params 查询参数 */private static void setParamValue(Query query,Object...params) {String hql = query.getQueryString();List<String> paramNames = getParamName(hql);int pc = paramNames.size();int vc = params.length;if (pc != vc) {throw new RuntimeException("命名参数个数为" + pc + ",而实参个数为" + vc + "。命名参数个数必须等于实参个数!");}for (int i = 0; i < paramNames.size(); i++) {query.setParameter(paramNames.get(i), params[i]);}}private static void setParamValue(Query query,Map<String, Object> params) {String hql = query.getQueryString();List<String> paramNames = getParamName(hql);int pc = paramNames.size();int vc = params.size();if (pc != vc) {throw new RuntimeException("命名参数个数为" + pc + ",而实参个数为" + vc + "。命名参数个数必须等于实参个数!");}Set<String> paramKeys = params.keySet();for (String paramName : paramNames) {if (!paramKeys.contains(paramName)) {throw new RuntimeException("命名参数" + paramName + ",在实参键集合" + paramKeys + "中不存在!");}}for (int i = 0; i < paramNames.size(); i++) {String paramName = paramNames.get(i);query.setParameter(paramName, params.get(paramName));}}//构建排序private static String buildOrderBy(LinkedHashMap<String, String> orderBy){StringBuffer buffer = new StringBuffer();if (orderBy != null && orderBy.size() > 0) {buffer.append(" order by ");for (String key : orderBy.keySet()) {buffer.append(key).append(" ").append(orderBy.get(key)).append(",");buffer.deleteCharAt(buffer.length() - 1);//删除指定位置的字符}}return buffer.toString();}/** * 设置分页参数 * @param query 查询接口 * @param pageIndex 页码 * @param pageSize 页大小 */private static void setPagination(Query query,Integer pageIndex,Integer pageSize) {if (pageIndex != null && pageSize != null) {query.setFirstResult(pageSize * (pageIndex - 1));query.setMaxResults(pageSize);}}}
@Overridepublic void save(T entity) {getSession().save(entity);}
@Overridepublic void persist(T entity) {getSession().persist(entity);}
@Overridepublic void update(T entity) {getSession().update(entity);}
@Overridepublic void merge(T entity) {getSession().merge(entity);
}
@Overridepublic void remove(Serializable id) {T t = get(id);if (t != null) {getSession().delete(t);}}
@Overridepublic T get(Serializable id) {return (T) getSession().get(entityClass, id);}
@Overridepublic T get(String where, Object... params) {String hql = "from " + entityClass.getSimpleName() + Util.builWhere(where);Query query = getSession().createQuery(hql);Util.setParamValue(query, params);return (T) query.uniqueResult();}
@Overridepublic <E> E get(Class<E> returnType, String propName, String where, Object... params) {String hql = "select " + propName + "from " + entityClass.getSimpleName() + Util.builWhere(where);Query query = getSession().createQuery(hql);Util.setParamValue(query, params);return returnType.cast(query.uniqueResult());}
@Overridepublic Integer count() {return count(null);}
@Overridepublic Integer count(String where, Object... params) {String hql = "select count(o) from " + entityClass.getSimpleName() +" o " + Util.builWhere(where);Query query = getSession().createQuery(hql);Util.setParamValue(query, params);return Integer.parseInt(String.valueOf(query.uniqueResult()));}
@Overridepublic Integer count(String where, Map<String, Object> params) {String hql = "select count(o) from " + entityClass.getSimpleName() +" o "+ Util.builWhere(where);Query query = getSession().createQuery(hql);Util.setParamValue(query, params);return Integer.parseInt(String.valueOf(query.uniqueResult()));}
@Overridepublic Integer countByHQL(String hql, Object... params) {Query query = getSession().createQuery(hql);Util.setParamValue(query, params);return Integer.parseInt(String.valueOf(query.uniqueResult()));}
@Overridepublic Integer countByHQL(String hql, Map<String, Object> params) {Query query = getSession().createQuery(hql);Util.setParamValue(query, params);return Integer.parseInt(String.valueOf(query.uniqueResult()));}
@Overridepublic List<T> qeuryList() {return queryList(null);}
@Overridepublic List<T> queryList(String where, Object... params) {return queryList(null, null, where, params);}
@Overridepublic List<T> queryList(String where, Map<String, Object> params) {return queryList(null, null, where, params);}
@Overridepublic List<T> queryList(Integer pageIndex, Integer pageSize) {return queryList(pageIndex, pageSize, null);}
@Overridepublic List<T> queryList(Integer pageIndex, Integer pageSize, String where, Object... params) {return queryList(pageIndex, pageSize, null, where, params);}
@Overridepublic List<T> queryList(Integer pageIndex, Integer pageSize, String where, Map<String, Object> params) {return queryList(pageIndex, pageSize, null, where, params);}
@Overridepublic List<T> queryList(LinkedHashMap<String, String> orderBy) {return queryList(null, null, orderBy);}
@Overridepublic List<T> queryList(LinkedHashMap<String, String> orderBy, String where, Object... params) {return queryList(null, null, orderBy, where, params);}
@Overridepublic List<T> queryList(LinkedHashMap<String, String> orderBy, String where, Map<String, Object> params) {return queryList(null, null, orderBy, where, params);}
@Overridepublic List<T> queryList(Integer pageIndex, Integer pageSize, LinkedHashMap<String, String> orderBy) {return queryList(pageIndex, pageSize, orderBy, null);}
@Overridepublic List<T> queryList(Integer pageIndex, Integer pageSize, LinkedHashMap<String, String> orderBy, String where, Object... params) {String hql = "from " + entityClass.getSimpleName() +"where " + Util.builWhere(where) + " order by " + Util.buildOrderBy(orderBy);Query query = getSession().createQuery(hql);Util.setParamValue(query, params);Util.setPagination(query, pageIndex, pageSize);return query.list();}
@Overridepublic List<T> queryList(Integer pageIndex, Integer pageSize, LinkedHashMap<String, String> orderBy, String where, Map<String, Object> params) {String hql = "from " + entityClass.getSimpleName() +"where " + Util.builWhere(where) + " order by " + Util.buildOrderBy(orderBy);Query query = getSession().createQuery(hql);Util.setParamValue(query, params);Util.setPagination(query, pageIndex, pageSize);return query.list();}
@Overridepublic List<T> queryListByHQL(String hql, Object... params) {return queryListByHQL(null, null, hql, params);}
@Overridepublic List<T> queryListByHQL(String hql, Map<String, Object> params) {return queryListByHQL(null, null, hql, params);}
@Overridepublic List<T> queryListByHQL(Integer pageIndex, Integer pageSize, String hql, Object... params) {Query query = getSession().createQuery(hql);Util.setParamValue(query, params);Util.setPagination(query, pageIndex, pageSize);return query.list();}
@Overridepublic List<T> queryListByHQL(Integer pageIndex, Integer pageSize, String hql, Map<String, Object> params) {Query query = getSession().createQuery(hql);Util.setParamValue(query, params);Util.setPagination(query, pageIndex, pageSize);return query.list();}}
标签:
原文地址:http://www.cnblogs.com/stickitout/p/5207805.html