Hibernate5 Dao层基类

接口:

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 <T>
 * @param <ID>
 */
public interface BaseDao<T, ID extends Serializable> {
    
    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<T> entities);

    int excuteHql(String hqlString);

    int excuteHql(String hqlString, Map<String, ?> params);

    int excuteSql(String sqlString);

    int excuteSql(String sqlString, Map<String, ?> params);
    
    T load(ID id);

    T get(ID id);

    T getByHQL(String hqlString, Map<String, ?> params);
    
    <V> V getByHQL(String hqlString, Map<String, ?> params, Class<V> theClass);
    
    T getBySQL(String sqlString, Map<String, ?> params);
    
    <V> V getBySQL(String sqlString, Map<String, ?> params, Class<V> theClass);

    List<T> listByHQL(String hqlString);
    
    <V> List<V> listByHQL(String hqlString, Class<V> theClass);

    List<T> listByHQL(String hqlString, Map<String, ?> params);
    
    <V> List<V> listByHQL(String hqlString, Map<String, ?> params, Class<V> theClass);

    List<T> listByHQL(String hqlString, int firstResult, int maxResult);
    
    <V> List<V> listByHQL(String hqlString, int firstResult, int maxResult, Class<V> theClass);

    List<T> listByHQL(String hqlString, Map<String, ?> params, int firstResult, int maxResult);
    
    <V> List<V> listByHQL(String hqlString, Map<String, ?> params, int firstResult, int maxResult, Class<V> theClass);

    List<T> listBySQL(String sqlString);
    
    <V> List<V> listBySQL(String sqlString, Class<V> theClass);

    List<T> listBySQL(String sqlString, Map<String, ?> params);
    
    <V> List<V> listBySQL(String sqlString, Map<String, ?> params, Class<V> theClass);

    void refresh(T t);

    void update(T t);

    default Query<?> setParameterToQuery(Query<?> query, Map<String, ?> 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 <T>
 * @param <ID>
 */
public abstract class AbstractBaseDao<T, ID extends Serializable> implements BaseDao<T, ID> {

    protected SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    private Class<T> theClass = (Class<T>) ((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<T> 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<String, ?> 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<String, ?> 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<String, ?> params) {
        Query<T> query = this.getSession().createQuery(hqlString, theClass);
        this.setParameterToQuery(query, params);
        return query.uniqueResult();
    }
    

    @Override
    public <V> V getByHQL(String hqlString, Map<String, ?> params, Class<V> theClass) {
        Query<V> query = this.getSession().createQuery(hqlString, theClass);
        this.setParameterToQuery(query, params);
        return query.uniqueResult();
    }

    @Override
    public T getBySQL(String sqlString, Map<String, ?> params) {
        NativeQuery<T> query = this.getSession().createNativeQuery(sqlString, theClass);
        this.setParameterToQuery(query, params);
        return query.uniqueResult();
    }
    
    @Override
    public <V> V getBySQL(String sqlString, Map<String, ?> params, Class<V> theClass) {
        NativeQuery<V> query = this.getSession().createNativeQuery(sqlString, theClass);
        this.setParameterToQuery(query, params);
        return query.uniqueResult();
    }

    @Override
    public List<T> listByHQL(String hqlString) {
        return this.getSession().createQuery(hqlString, theClass).list();
    }
    
    @Override
    public <V> List<V> listByHQL(String hqlString, Class<V> theClass) {
        return this.getSession().createQuery(hqlString, theClass).list();
    }

    @Override
    public List<T> listByHQL(String hqlString, Map<String, ?> params) {
        Query<T> query = this.getSession().createQuery(hqlString, theClass);
        this.setParameterToQuery(query, params);
        return query.list();
    }
    
    @Override
    public <V> List<V> listByHQL(String hqlString, Map<String, ?> params, Class<V> theClass) {
        Query<V> query = this.getSession().createQuery(hqlString, theClass);
        this.setParameterToQuery(query, params);
        return query.list();
    }

    @Override
    public List<T> listByHQL(String hqlString, int firstResult, int maxResult) {
        Query<T> query = this.getSession().createQuery(hqlString, theClass);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResult);
        return query.list();
    }
    
    @Override
    public <V> List<V> listByHQL(String hqlString, int firstResult, int maxResult, Class<V> theClass) {
        Query<V> query = this.getSession().createQuery(hqlString, theClass);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResult);
        return query.list();
    }

    @Override
    public List<T> listByHQL(String hqlString, Map<String, ?> params, int firstResult,
            int maxResult) {
        Query<T> query = this.getSession().createQuery(hqlString, theClass);
        this.setParameterToQuery(query, params);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResult);
        return query.list();
    }
    
    @Override
    public <V> List<V> listByHQL(String hqlString, Map<String, ?> params, int firstResult, int maxResult,
            Class<V> theClass) {
        Query<V> query = this.getSession().createQuery(hqlString, theClass);
        this.setParameterToQuery(query, params);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResult);
        return query.list();
    }

    @Override
    public List<T> listBySQL(String sqlString) {
        return this.getSession().createNativeQuery(sqlString, theClass).list();
    }
    
    @Override
    public <V> List<V> listBySQL(String sqlString, Class<V> theClass) {
        return this.getSession().createNativeQuery(sqlString, theClass).list();
    }

    @Override
    public List<T> listBySQL(String sqlString, Map<String, ?> params) {
        NativeQuery<T> query = this.getSession().createNativeQuery(sqlString, theClass);
        this.setParameterToQuery(query, params);
        return query.list();
    }
    
    @Override
    public <V> List<V> listBySQL(String sqlString, Map<String, ?> params, Class<V> theClass) {
        NativeQuery<V> 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<T, ID extends Serializable> extends AbstractBaseDao<T, ID> {

    @Override
    @Resource(name = "xxxx_sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.sessionFactory = sessionFactory;
    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值