1. 程式人生 > >Hibernate通用工具類

Hibernate通用工具類

通用介面定義

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<T, PK extends Serializable> {

    public Serializable save(T t);

    public void update
(T t); public void delete(T t); public void deleteAll (Collection<?> entities); public T get(Class<T> entityClass, PK pk); public List<T> getAll(Class<T> entityClass); public T load(Class<T> entityClass, PK pk); public List<T> loadAll
(Class<T> entityClass); public void saveOrUpdate(T t); public List<?> findByCriteria(DetachedCriteria criteria); public List<?> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults); public List<T> createNativeQuery(String sqlString, Class<T> resultClass); public
List<T> createNativeQuery(String sqlString, Class<T> resultClass, int firstResult, int maxResult); public List<T> createNativeQuery(String sqlString, Object[] params, Class<T> resultClass, int firstResult, int maxResult); public List<?> createNativeQuery(String sqlString); public List<?> createNativeQuery(String sqlString, Object[] params); public List<T> findByHql(String hqlString, Object[] params, Class<T> resultClass); public List<?> findByHql(String hqlString, Object[] params); public List<T> findByHql(String hqlString, Object[] params, Class<T> 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<T, PK extends Serializable> extends HibernateDaoSupport implements IBaseDao<T, PK> {

    @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<T> entityClass, PK pk) {
        return getHibernateTemplate().get(entityClass, pk);
    }

    @Override
    public List<T> getAll(Class<T> entityClass) {
        return null;
    }

    @Override
    public T load(Class<T> entityClass, PK pk) {
        return getHibernateTemplate().load(entityClass, pk);
    }

    @Override
    public List<T> loadAll(Class<T> 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<T> createNativeQuery(String sqlString, Class<T> resultClass) {
        if(StringUtils.isEmpty(sqlString)) {
            throw new NullPointerException("查詢的sql語句不能為空");
        }
        NativeQuery<T> 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<T> createNativeQuery(String sqlString, Class<T> resultClass, int firstResult, int maxResult) {
        if(StringUtils.isEmpty(sqlString)) {
            throw new NullPointerException("查詢的sql語句不能為空");
        }
        NativeQuery<T> nativeQuery = currentSession().createNativeQuery(sqlString, resultClass);
        nativeQuery.setFirstResult(firstResult);
        nativeQuery.setMaxResults(maxResult);
        return nativeQuery.getResultList();
    }

    @Override
    public List<T> createNativeQuery(String sqlString, Object[] params, Class<T> resultClass, int firstResult, int maxResult) {
        if(StringUtils.isEmpty(sqlString)) {
            throw new NullPointerException("查詢的sql語句不能為空");
        }
        NativeQuery<T> 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<T> findByHql(String hqlString, Object[] params, Class<T> resultClass) {
        if(StringUtils.isEmpty(hqlString)) {
            throw new NullPointerException("查詢的hql語句不能為空");
        }
        Query<T> 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<T> findByHql(String hqlString, Object[] params, Class<T> resultClass, int firstResult, int maxResult) {
        if(StringUtils.isEmpty(hqlString)) {
            throw new NullPointerException("查詢的hql語句不能為空");
        }

        Query<T> 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();
    }

}