Hibernate的dao层封装

版权声明:内容记录学习过成文章,仅供参考 https://blog.csdn.net/qq_40195958/article/details/84303428
package com.jit.basics.dao;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDao
  extends HibernateDaoSupport
{
  private static final Log log = LogFactory.getLog(BaseDao.class);
  
  protected void initDao() {}
  
  public <T> T findById(Class<T> clazz, Serializable id)
  {
    log.debug("getting " + clazz + " instance with id: " + id);
    try
    {
      return getHibernateTemplate().get(clazz, id);
    }
    catch (RuntimeException re)
    {
      log.error("get failed", re);
      throw re;
    }
  }
  
  public List findByHql(String strHQL)
  {
    return getHibernateTemplate().find(strHQL);
  }
  
  public List findByHql(String hql, Object... param)
  {
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    Query query = session.createQuery(hql);
    if ((param != null) && (param.length > 0)) {
      for (int i = 0; i < param.length; i++) {
        query.setParameter(i, param[i]);
      }
    }
    return query.list();
  }
  
  public List findByHql(String hql, List param)
  {
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    Query query = session.createQuery(hql);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return query.list();
  }
  
  public List findByHql(String hql, int page, int rows, List param)
  {
    Query query = getHibernateTemplate().getSessionFactory()
      .getCurrentSession().createQuery(hql);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return 
      query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
  }
  
  public List findByHql(String hql, int page, int rows, Object... param)
  {
    Query query = getHibernateTemplate().getSessionFactory()
      .getCurrentSession().createQuery(hql);
    if ((param != null) && (param.length > 0)) {
      for (int i = 0; i < param.length; i++) {
        query.setParameter(i, param[i]);
      }
    }
    return 
      query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
  }
  
  public List findBySQL(String strSQL, List param)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return query.list();
  }
  
  public List findBySQL(String strSQL, int page, int rows, List param)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return 
      query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
  }
  
  public List findBySQL(String strSQL, int page, int rows, List param, Class obj)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL).addEntity(obj);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return 
      query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
  }
  
  public List findBySQL(String strSQL)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL);
    return query.list();
  }
  
  public List findBySQL(String strSQL, Class obj)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL).addEntity(obj);
    return query.list();
  }
  
  public int executeByHQL(String strHQL)
  {
    Query query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createQuery(strHQL);
    return query.executeUpdate();
  }
  
  public int executeByHQL(String strHQL, List param)
  {
    Query query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createQuery(strHQL);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return query.executeUpdate();
  }
  
  public int executeByHQL(String strHQL, Object... param)
  {
    Query query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createQuery(strHQL);
    if ((param != null) && (param.length > 0)) {
      for (int i = 0; i < param.length; i++) {
        query.setParameter(i, param[i]);
      }
    }
    return query.executeUpdate();
  }
  
  public int exexuteBySQL(String strSQL, Object... param)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL);
    if ((param != null) && (param.length > 0)) {
      for (int i = 0; i < param.length; i++) {
        query.setParameter(i, param[i]);
      }
    }
    return query.executeUpdate();
  }
  
  public int exexuteBySQL(String strSQL, List param)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return query.executeUpdate();
  }
  
  public int exexuteBySQL(String strSQL)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL);
    return query.executeUpdate();
  }
  
  public List findsql(final String sql)
  {
    getHibernateTemplate().executeFind(new HibernateCallback()
    {
      public Object doInHibernate(org.hibernate.Session session)
        throws HibernateException, SQLException
      {
        return session.createSQLQuery(sql).list();
      }
    });
  }
  
  public List countsql(final String sql)
  {
    getHibernateTemplate().executeFind(new HibernateCallback()
    {
      public Object doInHibernate(org.hibernate.Session session)
        throws HibernateException, SQLException
      {
        return session.createSQLQuery(sql).list();
      }
    });
  }
  
  public void save(Object instance)
  {
    try
    {
      getHibernateTemplate().save(instance);
    }
    catch (RuntimeException re)
    {
      log.error("save failed", re);
    }
  }
  
  public void merge(Object instance)
  {
    try
    {
      getHibernateTemplate().merge(instance);
    }
    catch (RuntimeException re)
    {
      log.error("merge failed", re);
    }
  }
  
  public Boolean update(String strHQL)
  {
    boolean bo = false;
    try
    {
      int count = getHibernateTemplate().bulkUpdate(strHQL);
      bo = count > 0;
    }
    catch (RuntimeException re)
    {
      bo = false;
      log.error("update failed", re);
      throw re;
    }
    return Boolean.valueOf(bo);
  }
  
  public void update(Object instance)
  {
    try
    {
      getHibernateTemplate().saveOrUpdate(instance);
    }
    catch (RuntimeException re)
    {
      log.error("update failed", re);
      throw re;
    }
  }
  
  public void delete(Object instance)
  {
    try
    {
      getHibernateTemplate().delete(instance);
    }
    catch (RuntimeException re)
    {
      log.error("delete failed", re);
    }
  }
  
  public List find(String hql, Object... param)
  {
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    Query query = session.createQuery(hql);
    if ((param != null) && (param.length > 0)) {
      for (int i = 0; i < param.length; i++) {
        query.setParameter(i, param[i]);
      }
    }
    return query.list();
  }
  
  public List find(String hql, List param)
  {
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    Query query = session.createQuery(hql);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return query.list();
  }
  
  public Long count(String hql, Object... param)
  {
    Query query = getHibernateTemplate().getSessionFactory()
      .getCurrentSession().createQuery(hql);
    if ((param != null) && (param.length > 0)) {
      for (int i = 0; i < param.length; i++) {
        query.setParameter(i, param[i]);
      }
    }
    return (Long)query.uniqueResult();
  }
  
  public Long count(String hql, List param)
  {
    Query query = getHibernateTemplate().getSessionFactory()
      .getCurrentSession().createQuery(hql);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return (Long)query.uniqueResult();
  }
  
  public List find(String hql, int page, int rows, List param)
  {
    Query query = getHibernateTemplate().getSessionFactory()
      .getCurrentSession().createQuery(hql);
    if ((param != null) && (param.size() > 0)) {
      for (int i = 0; i < param.size(); i++) {
        query.setParameter(i, param.get(i));
      }
    }
    return 
      query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
  }
  
  public List find(String hql, int page, int rows, Object... param)
  {
    Query query = getHibernateTemplate().getSessionFactory()
      .getCurrentSession().createQuery(hql);
    if ((param != null) && (param.length > 0)) {
      for (int i = 0; i < param.length; i++) {
        query.setParameter(i, param[i]);
      }
    }
    return 
      query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
  }
  
  public String reProCall(String procedure, List param)
  {
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    Connection conn = session.connection();
    CallableStatement proc = null;
    String acc = null;
    try
    {
      proc = conn.prepareCall(procedure);
      if ((param != null) && (param.size() > 0)) {
        for (int i = 1; i < param.size() + 1; i++) {
          proc.setString(i, (String)param.get(i - 1));
        }
      }
      proc.registerOutParameter(2, 12);
      proc.execute();
      acc = proc.getString(2);
      proc.close();
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    return acc;
  }
  
  public void saveOrUpdate(Object entity)
  {
    getHibernateTemplate().saveOrUpdate(entity);
  }
  
  public List findBySQL(String strSQL, int page, int rows, Map param)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL);
    if (param != null)
    {
      Set<String> keySet = param.keySet();
      for (String key : keySet)
      {
        Object obj = param.get(key);
        if ((obj instanceof Collection)) {
          query.setParameterList(key, (Collection)obj);
        } else if ((obj instanceof Object[])) {
          query.setParameterList(key, (Object[])obj);
        } else {
          query.setParameter(key, obj);
        }
      }
    }
    return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
  }
  
  public List findBySQL(String strSQL, Map param)
  {
    SQLQuery query = null;
    org.hibernate.Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
    query = session.createSQLQuery(strSQL);
    if (param != null)
    {
      Set<String> keySet = param.keySet();
      for (String key : keySet)
      {
        Object obj = param.get(key);
        if ((obj instanceof Collection)) {
          query.setParameterList(key, (Collection)obj);
        } else if ((obj instanceof Object[])) {
          query.setParameterList(key, (Object[])obj);
        } else {
          query.setParameter(key, obj);
        }
      }
    }
    return query.list();
  }
}

其中涉及到LogFactory 包为

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.3</version>
</dependency>

猜你喜欢

转载自blog.csdn.net/qq_40195958/article/details/84303428