hibernateDaoSupport中的一些公用常用方法

來源:互聯網
上載者:User

標籤:io   os   ar   for   sp   資料   div   on   art   

 /**
   * 刪除記錄
   * @param entity BO的對象
   * <pre>
   * this.getHibernateTemplate().delete(entity);
   * 按照規範,調用ECLRecordDelete記錄刪除的BO資訊;
   * </pre>
   */
  public void delete(Object entity){
   this.getHibernateTemplate().delete(entity);
  }
  
  
  
  /**
   * 通過id獲得記錄
   * @param Class entityClass,物件類型
   * @param Serializable id,BO的id值
   * <pre>
   * return hibernateTemplate.get(entityClass,id);
   * </pre>
   */
  public Object findById(Class entityClass, Serializable id) {
   return this.getHibernateTemplate().get(entityClass,id);
  }
  
  /**
   * 獲得消極式載入資料
   * @param Object object,需要載入的對象
   * <pre>
   * Hibernate.initialize(object);
   * </pre>
   */
  public void getLazyData(Object object) {
   this.getHibernateTemplate().initialize(object);
  }
  
  /**
   * 增加記錄
   * @param Object entity,需要儲存的BO
   * <pre>
   * hibernateTemplate.save(entity);
   * return entity;
   * </pre>
   */
  public Object save(Object entity) {
   Object id = (Object)this.getHibernateTemplate().save(entity);
   return id;
  }
  
  /**
   * 修改記錄
   * @param Object,要修改的BO
   * <pre>
   * hibernateTemplate.update(entity);
   * </pre>
   */
  public void update(Object entity) {
   this.getHibernateTemplate().update(entity);
  }

 

 

  //通過傳遞多個條件查詢表記錄

  public List findByHQLCondition(Class entityClass,List propertyNameList,List propertyValueList){
   StringBuffer sqlStr = new StringBuffer();
   sqlStr.append("from ").append(entityClass.getName()).append(" as bo");
   sqlStr.append(" where 1=1 ");
   if(propertyNameList !=null && propertyValueList!=null && propertyNameList.size() == propertyValueList.size()){
    for(int i=0;i<propertyNameList.size();i++){
     sqlStr.append(" and bo.").append(propertyNameList.get(i)).append(" = :").append(propertyNameList.get(i));
    }
   }else{
    return null;
   }
   final String sql = sqlStr.toString();
   final List propertyNList = propertyNameList;
   final List propertyVList = propertyValueList;
   return(List) getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
          throws HibernateException, SQLException {
     List li = new ArrayList();
     Query query = session.createQuery(sql);
     for(int i=0;i<propertyNList.size();i++){
      query.setParameter(propertyNList.get(i).toString(),propertyVList.get(i));
     }
     
     Iterator it = query.iterate();
     while(it.hasNext()){
      li.add(it.next());
     }
     return li;
    }
   });
  }

 

 //根據傳遞的HQL語句,來查詢資料庫

  public List findByHQL(String hql){
   return this.getHibernateTemplate().find(hql);
  }

 

//只根據一個條件來查詢資料庫欄位

  public List findByOneHQLCondition(Class entityClass,String propertyName,String propertyValue) {
   StringBuffer sqlStr = new StringBuffer();
   sqlStr.append("from ").append(entityClass.getName()).append(" as bo where bo.");
   sqlStr.append(propertyName);
   sqlStr.append(" = :");
   sqlStr.append(propertyName);

   final String sql = sqlStr.toString();

   return getHibernateTemplate().findByNamedParam(sql, propertyName,propertyValue);

hibernateDaoSupport中的一些公用常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.