接口:
package cn.xxx.xxxx;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.query.Query;
/**
* Dao基类接口
*
* @author Shi Zezhu
* @date 2017年6月12日 上午11:19:48
* @param
* @param
*/
public interface BaseDao {
Session getSession();
void save(T t);
void saveOrUpdate(T t);
boolean contains(T t);
void delete(T t);
boolean deleteById(ID Id);
void deleteAll(Collection entities);
int excuteHql(String hqlString);
int excuteHql(String hqlString, Map params);
int excuteSql(String sqlString);
int excuteSql(String sqlString, Map params);
T load(ID id);
T get(ID id);
T getByHQL(String hqlString, Map params);
V getByHQL(String hqlString, Map params, Class theClass);
T getBySQL(String sqlString, Map params);
V getBySQL(String sqlString, Map params, Class theClass);
List listByHQL(String hqlString);
List listByHQL(String hqlString, Class theClass);
List listByHQL(String hqlString, Map params);
List listByHQL(String hqlString, Map params, Class theClass);
List listByHQL(String hqlString, int firstResult, int maxResult);
List listByHQL(String hqlString, int firstResult, int maxResult, Class theClass);
List listByHQL(String hqlString, Map params, int firstResult, int maxResult);
List listByHQL(String hqlString, Map params, int firstResult, int maxResult, Class theClass);
List listBySQL(String sqlString);
List listBySQL(String sqlString, Class theClass);
List listBySQL(String sqlString, Map params);
List listBySQL(String sqlString, Map params, Class theClass);
void refresh(T t);
void update(T t);
default Query setParameterToQuery(Query query, Map params) {
if (params != null) {
params.forEach((key, value) -> {
if (value instanceof Object[]) {
query.setParameterList(key, (Object[]) value);
} else if (value instanceof Collection) {
query.setParameterList(key, (Collection) value);
} else {
query.setParameter(key, params.get(key));
}
});
}
return query;
}
}
抽象类:
package cn.xxx.xxx;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
/**
* Dao基类实现
*
* @author Shi Zezhu
* @date 2017年5月31日 下午5:53:57
* @param
* @param
*/
public abstract class AbstractBaseDao implements BaseDao {
protected SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
private Class theClass = (Class) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
public Session getSession() {
return sessionFactory.getCurrentSession();
}
@Override
public void save(T t) {
this.getSession().save(t);
}
@Override
public void saveOrUpdate(T t) {
this.getSession().saveOrUpdate(t);
}
@Override
public boolean contains(T t) {
return this.getSession().contains(t);
}
@Override
public void delete(T t) {
this.getSession().delete(t);
}
@Override
public boolean deleteById(ID Id) {
T t = this.get(Id);
if (t == null) return false;
this.delete(t);
return true;
}
@Override
public void deleteAll(Collection entities) {
Session session = this.getSession();
for (T entity : entities) {
session.delete(entity);
}
}
@Override
public int excuteHql(String hqlString) {
return this.getSession().createQuery(hqlString).executeUpdate();
}
@Override
public int excuteHql(String hqlString, Map params) {
return this.setParameterToQuery(this.getSession().createQuery(hqlString), params).executeUpdate();
}
@Override
public int excuteSql(String sqlString) {
return this.getSession().createNativeQuery(sqlString).executeUpdate();
}
@Override
public int excuteSql(String sqlString, Map params) {
return this.setParameterToQuery(this.getSession().createNativeQuery(sqlString), params).executeUpdate();
}
@Override
public T load(ID id) {
return this.getSession().load(theClass, id);
}
@Override
public T get(ID id) {
return this.getSession().get(theClass, id);
}
@Override
public T getByHQL(String hqlString, Map params) {
Query query = this.getSession().createQuery(hqlString, theClass);
this.setParameterToQuery(query, params);
return query.uniqueResult();
}
@Override
public V getByHQL(String hqlString, Map params, Class theClass) {
Query query = this.getSession().createQuery(hqlString, theClass);
this.setParameterToQuery(query, params);
return query.uniqueResult();
}
@Override
public T getBySQL(String sqlString, Map params) {
NativeQuery query = this.getSession().createNativeQuery(sqlString, theClass);
this.setParameterToQuery(query, params);
return query.uniqueResult();
}
@Override
public V getBySQL(String sqlString, Map params, Class theClass) {
NativeQuery query = this.getSession().createNativeQuery(sqlString, theClass);
this.setParameterToQuery(query, params);
return query.uniqueResult();
}
@Override
public List listByHQL(String hqlString) {
return this.getSession().createQuery(hqlString, theClass).list();
}
@Override
public List listByHQL(String hqlString, Class theClass) {
return this.getSession().createQuery(hqlString, theClass).list();
}
@Override
public List listByHQL(String hqlString, Map params) {
Query query = this.getSession().createQuery(hqlString, theClass);
this.setParameterToQuery(query, params);
return query.list();
}
@Override
public List listByHQL(String hqlString, Map params, Class theClass) {
Query query = this.getSession().createQuery(hqlString, theClass);
this.setParameterToQuery(query, params);
return query.list();
}
@Override
public List listByHQL(String hqlString, int firstResult, int maxResult) {
Query query = this.getSession().createQuery(hqlString, theClass);
query.setFirstResult(firstResult);
query.setMaxResults(maxResult);
return query.list();
}
@Override
public List listByHQL(String hqlString, int firstResult, int maxResult, Class theClass) {
Query query = this.getSession().createQuery(hqlString, theClass);
query.setFirstResult(firstResult);
query.setMaxResults(maxResult);
return query.list();
}
@Override
public List listByHQL(String hqlString, Map params, int firstResult,
int maxResult) {
Query query = this.getSession().createQuery(hqlString, theClass);
this.setParameterToQuery(query, params);
query.setFirstResult(firstResult);
query.setMaxResults(maxResult);
return query.list();
}
@Override
public List listByHQL(String hqlString, Map params, int firstResult, int maxResult,
Class theClass) {
Query query = this.getSession().createQuery(hqlString, theClass);
this.setParameterToQuery(query, params);
query.setFirstResult(firstResult);
query.setMaxResults(maxResult);
return query.list();
}
@Override
public List listBySQL(String sqlString) {
return this.getSession().createNativeQuery(sqlString, theClass).list();
}
@Override
public List listBySQL(String sqlString, Class theClass) {
return this.getSession().createNativeQuery(sqlString, theClass).list();
}
@Override
public List listBySQL(String sqlString, Map params) {
NativeQuery query = this.getSession().createNativeQuery(sqlString, theClass);
this.setParameterToQuery(query, params);
return query.list();
}
@Override
public List listBySQL(String sqlString, Map params, Class theClass) {
NativeQuery query = this.getSession().createNativeQuery(sqlString, theClass);
this.setParameterToQuery(query, params);
return query.list();
}
@Override
public void refresh(T t) {
this.getSession().refresh(t);
}
@Override
public void update(T t) {
this.getSession().update(t);
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public abstract void setSessionFactory(SessionFactory sessionFactory);
}
实现类:这里是为了区分不同的数据库,不同的数据库不同的实现类,注入相应的sessionFactory
package cn.xxx.xxx;
import java.io.Serializable;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
public class SecurityBaseDaoImpl extends AbstractBaseDao {
@Override
@Resource(name = "xxxx_sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
super.sessionFactory = sessionFactory;
}
}