demo BaseDao随笔,hibernate框架

  /**
  * 增加entity
  * 
  * @param Object对象
  * @throws Exception
  */
 public <T> void save(T ob) throws Exception {
  this.getHibernateTemplate().save(ob);
 }
 /**
  * 删除entity
  * 
  * @param Object对象
  * @throws Exception
  */
 public <T> void delete(T ob) throws Exception {
  this.getHibernateTemplate().delete(ob);
 }
 /**
  * 删除entity:根据类,主键值获取entity,再进行删除
  * 
  * @param entityClass
  * @param idValue
  * @throws Exception
  */
 public <T> void delete(Class<T> entityClass, String idValue)
   throws Exception {
  T ob = (T) this.findById(entityClass, idValue);
  this.getHibernateTemplate().delete(ob);
 }
 /**
  * 批量删除:利用传入的参数拼写hql进行批量删除数据<br/>
  * 注:仅适用于单主键entity
  * 
  * @param entityClass
  *            对应的entity类
  * @param idName
  *            entity中的主键名称
  * @param ids
  *            主键值
  * @throws Exception
  */
 public <T> void deleteList(Class<T> entityClass, String idName,
   List<String> ids) throws Exception {
  Session session = null;
  try {
   String hql = "delete " + entityClass.getSimpleName() + " where "
     + idName + " in (";
   if (ids == null || ids.size() <= 0) {
    throw new Exception("batch delete id value is empty.");
   }
   for (int i = 0; ids != null && i < ids.size(); i++) {
    if (i == ids.size() - 1) {
     hql += "'" + ids.get(i) + "'";
    } else {
     hql += "'" + ids.get(i) + "',";
    }
   }
   hql += ")";
   session = this.getHibernateTemplate().getSessionFactory().openSession(); 
   Query query = session.createQuery(hql);
   query.executeUpdate();
  } finally {
   if (session != null)
    session.close();
  }
 }
 /**
  * 修改entity
  * 
  * @param Object对象
  * @throws Exception
  */
 public <T> void update(T ob) throws Exception {
  this.getHibernateTemplate().update(ob);
 }
 /**
  * Criteria查询全部entity
  * 
  * @param class类
  * @throws Exception
  */
 public <T> List<T> findAll(Class<T> cs) throws Exception {
  return this.getHibernateTemplate().loadAll(cs);
 }
 /**
  * 根据HQL语句进行查询
  * 
  * @param Hql语句
  * @throws Exception
  */
 @SuppressWarnings("unchecked")
 public <T> List<T> findByHql(String hql) throws Exception {
  return this.getHibernateTemplate().find(hql);
 }
 
/**
  * 根据SQL语句进行查询
  * 
  * @param Sql语句
  * @throws Exception
  */
 @SuppressWarnings("unchecked")
 public <T> List<T> findBySql(String sql){
  Session session = null;
  try {
   session = this.getHibernateTemplate().getSessionFactory().openSession(); 
   Query query = session.createSQLQuery(sql);
   return query.list();
  } finally {
   if (session != null)
    session.close();
  }
 }
 /**
  * 根据id查询entity
  * 
  * @param Class类
  * @param value
  * @return
  */
 public <T> T findById(Class<T> cs, String value) {
  return this.getHibernateTemplate().get(cs, value);
 }
 /**
  * hql查询唯一值
  * 
  * @param hql
  * @return
  */
 public Object findUniqueByHql(String hql){
  Session session = null;
  try {
   session = this.getHibernateTemplate().getSessionFactory().openSession(); 
   Query query = session.createQuery(hql);
   return query.uniqueResult();
  } finally {
   if (session != null)
    session.close();
  }
 }

 /**
  * hql查询得到唯一的结果,如果参数requried错误hibernate将抛出类型强制转型错误
  * @param <T>
  * @param hql  hql语句
  * @param requried 返回值类型class
  * @return 唯一的结果,如果没有返回null
  */
 @SuppressWarnings("unchecked")
 public <T> T findUniqueResult(String hql,Class<T> requried){
  Session session = null;
  session = this.getHibernateTemplate().getSessionFactory().getCurrentSession(); 
  Query query = session.createQuery(hql);
  Object retObj = query.uniqueResult();
  if (retObj != null) {
   return (T)retObj;
  }
  return null;
 }  
原文地址:https://www.cnblogs.com/songfei90/p/10647001.html