通用接口定义
package com.gzjh.basic.common.dao;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
public interface IBaseDao {
public Serializable save(T t);
public void update(T t);
public void delete(T t);
public void deleteAll (Collection> entities);
public T get(Class entityClass, PK pk);
public List getAll(Class entityClass);
public T load(Class entityClass, PK pk);
public List loadAll(Class entityClass);
public void saveOrUpdate(T t);
public List> findByCriteria(DetachedCriteria criteria);
public List> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults);
public List createNativeQuery(String sqlString, Class resultClass);
public List createNativeQuery(String sqlString, Class resultClass, int firstResult, int maxResult);
public List createNativeQuery(String sqlString, Object[] params, Class resultClass, int firstResult, int maxResult);
public List> createNativeQuery(String sqlString);
public List> createNativeQuery(String sqlString, Object[] params);
public List findByHql(String hqlString, Object[] params, Class resultClass);
public List> findByHql(String hqlString, Object[] params);
public List findByHql(String hqlString, Object[] params, Class resultClass, int firstResult, int maxResult);
public List> findByHql(String hqlString, Object[] params, int firstResult, int maxResult);
}
实现类
package com.gzjh.basic.common.dao;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.alibaba.druid.util.StringUtils;
public class BaseDao extends HibernateDaoSupport implements IBaseDao {
@Override
public Serializable save(T t) {
return getHibernateTemplate().save(t);
}
@Override
public void update(T t) {
getHibernateTemplate().update(t);
}
@Override
public void delete(T t) {
getHibernateTemplate().delete(t);
}
@Override
public T get(Class entityClass, PK pk) {
return getHibernateTemplate().get(entityClass, pk);
}
@Override
public List getAll(Class entityClass) {
return null;
}
@Override
public T load(Class entityClass, PK pk) {
return getHibernateTemplate().load(entityClass, pk);
}
@Override
public List loadAll(Class entityClass) {
return getHibernateTemplate().loadAll(entityClass);
}
@Override
public void saveOrUpdate(T t) {
getHibernateTemplate().saveOrUpdate(t);
}
@Override
public void deleteAll(Collection> entities) {
getHibernateTemplate().deleteAll(entities);
}
@Override
public List> findByCriteria(DetachedCriteria criteria) {
return getHibernateTemplate().findByCriteria(criteria);
}
@Override
public List> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) {
return getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults);
}
@Override
public List createNativeQuery(String sqlString, Class resultClass) {
if(StringUtils.isEmpty(sqlString)) {
throw new NullPointerException("查询的sql语句不能为空");
}
NativeQuery nativeQuery = currentSession().createNativeQuery(sqlString, resultClass);
return nativeQuery.getResultList();
}
@Override
public List> createNativeQuery(String sqlString) {
if(StringUtils.isEmpty(sqlString)) {
throw new NullPointerException("查询的sql语句不能为空");
}
NativeQuery> nativeQuery = currentSession().createNativeQuery(sqlString);
return nativeQuery.getResultList();
}
@Override
public List> createNativeQuery(String sqlString, Object[] params) {
if(StringUtils.isEmpty(sqlString)) {
throw new NullPointerException("查询的sql语句不能为空");
}
NativeQuery> nativeQuery = currentSession().createNativeQuery(sqlString);
int position = 0;
if(params != null) {
for (Object param : params) {
nativeQuery.setParameter(position, param);
}
}
return nativeQuery.getResultList();
}
@Override
public List createNativeQuery(String sqlString, Class resultClass, int firstResult, int maxResult) {
if(StringUtils.isEmpty(sqlString)) {
throw new NullPointerException("查询的sql语句不能为空");
}
NativeQuery nativeQuery = currentSession().createNativeQuery(sqlString, resultClass);
nativeQuery.setFirstResult(firstResult);
nativeQuery.setMaxResults(maxResult);
return nativeQuery.getResultList();
}
@Override
public List createNativeQuery(String sqlString, Object[] params, Class resultClass, int firstResult, int maxResult) {
if(StringUtils.isEmpty(sqlString)) {
throw new NullPointerException("查询的sql语句不能为空");
}
NativeQuery nativeQuery = currentSession().createNativeQuery(sqlString, resultClass);
nativeQuery.setFirstResult(firstResult);
nativeQuery.setMaxResults(maxResult);
int position = 0;
if(params != null) {
for (Object param : params) {
nativeQuery.setParameter(position, param);
}
}
return nativeQuery.getResultList();
}
@Override
public List findByHql(String hqlString, Object[] params, Class resultClass) {
if(StringUtils.isEmpty(hqlString)) {
throw new NullPointerException("查询的hql语句不能为空");
}
Query query = currentSession().createQuery(hqlString, resultClass);
int position = 0;
if(params != null) {
for (Object param : params) {
query.setParameter(position, param);
}
}
return query.getResultList();
}
@Override
public List findByHql(String hqlString, Object[] params, Class resultClass, int firstResult, int maxResult) {
if(StringUtils.isEmpty(hqlString)) {
throw new NullPointerException("查询的hql语句不能为空");
}
Query query = currentSession().createQuery(hqlString, resultClass);
query.setFirstResult(firstResult);
query.setMaxResults(maxResult);
int position = 0;
if(params != null) {
for (Object param : params) {
query.setParameter(position, param);
}
}
return query.getResultList();
}
@Override
public List> findByHql(String hqlString, Object[] params) {
if(StringUtils.isEmpty(hqlString)) {
throw new NullPointerException("查询的hql语句不能为空");
}
Query> query = currentSession().createQuery(hqlString);
int position = 0;
if(params != null) {
for (Object param : params) {
query.setParameter(position, param);
}
}
return query.getResultList();
}
@Override
public List> findByHql(String hqlString, Object[] params, int firstResult, int maxResult) {
if(StringUtils.isEmpty(hqlString)) {
throw new NullPointerException("查询的hql语句不能为空");
}
Query> query = currentSession().createQuery(hqlString);
query.setFirstResult(firstResult);
query.setMaxResults(maxResult);
int position = 0;
if(params != null) {
for (Object param : params) {
query.setParameter(position, param);
}
}
return query.getResultList();
}
}