Hibernate框架,BaseDao


  /**
  * 增加entity
  * 
  * @param Object对象
  * @throws Exception
  */
 public  void save(T ob) throws Exception {
  this.getHibernateTemplate().save(ob);
 }
 /**
  * 删除entity
  * 
  * @param Object对象
  * @throws Exception
  */
 public  void delete(T ob) throws Exception {
  this.getHibernateTemplate().delete(ob);
 }
 /**
  * 删除entity:根据类,主键值获取entity,再进行删除
  * 
  * @param entityClass
  * @param idValue
  * @throws Exception
  */
 public  void delete(Class entityClass, String idValue)
   throws Exception {
  T ob = (T) this.findById(entityClass, idValue);
  this.getHibernateTemplate().delete(ob);
 }
 /**
  * 批量删除:利用传入的参数拼写hql进行批量删除数据
* 注:仅适用于单主键entity * *
@param entityClass * 对应的entity类 * @param idName * entity中的主键名称 * @param ids * 主键值 * @throws Exception */ public void deleteList(Class entityClass, String idName, List 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 void update(T ob) throws Exception { this.getHibernateTemplate().update(ob); } /** * Criteria查询全部entity * * @param class类 * @throws Exception */ public List findAll(Class cs) throws Exception { return this.getHibernateTemplate().loadAll(cs); } /** * 根据HQL语句进行查询 * * @param Hql语句 * @throws Exception */ @SuppressWarnings("unchecked") public List findByHql(String hql) throws Exception { return this.getHibernateTemplate().find(hql); } /** * 根据SQL语句进行查询 * * @param Sql语句 * @throws Exception */ @SuppressWarnings("unchecked") public List 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 findById(Class 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 * @param hql hql语句 * @param requried 返回值类型class * @return 唯一的结果,如果没有返回null */ @SuppressWarnings("unchecked") public T findUniqueResult(String hql,Class 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; }