hql Hibernate.gethibernatetemplate()

來源:互聯網
上載者:User

標籤:io   ar   使用   for   sp   on   art   cti   amp   

1. find(String hql);  //普通查詢

   樣本:this.gethibernateTemplate().find("from User");

 

2. find(String hql,Object value);//一個查詢條件

   樣本:this.gethibernateTemplate().find("from User u where u.name=?","test");

 

3. find(String hql,Object[] values);// 多個查詢條件

   樣本:this.gethibernateTemplate().find("from User u where u.name=? and u.pwd=?",new String[]{"test","123"});

4. findByExample(Object exampleEntity,int firstResult, int maxResults)//分頁使用

   樣本:

   User user= new User(); u.setActive("Active");

   List list=this.getHibernateTemplate().findByExample(user,firstResult,maxResults);  

   查詢結果:狀態為Active的使用者(對象從0到20 計數)  

 

5. findByNamedParam(String hql,String paramName,Object value); //一個查詢條件

   樣本:

   hql="from User u where u.name=:parName ";

   paramName= "parName";

   value="bb"

   List list = this.getHibernateTemplate.findByNamedParam(hql,paramName,value);

   查詢結果:姓名為bb的使用者

 

6. findByNamedParam(String queryString , String[] paramName , Object[] value) //多個查詢條件

   樣本:

   hql="from User u where u.name=:myname and u.pwd =:mypwd ";

   String[] paramName= new String[]{"myname","mypwd"};

   Sring[] value=new Strign[]{"bb","123"};

   List list = this.getHibernateTemplate.findByNamedParam(hql,paramName,value);

   查詢結果:姓名為bb密碼為123的使用者

 

7.分頁HQL樣本

   public List excuteHqlPage(final String hqlStr, final int startRow,final int rowCount) throws DaoException {

   List<Object[]> list;

   try {

       list = getHibernateTemplate().executeFind(new HibernateCallback() {

       public Object doInHibernate(Session session)

       throws HibernateException, SQLException {

       org.hibernate.Query query = (org.hibernate.Query) session.createQuery(hqlStr);

       query.setFirstResult(startRow);// 定義從第幾條開始查詢

       query.setMaxResults(rowCount);// 定義返回的記錄數

       List list = query.list();

       return list;

     }

    });

   } catch (Exception e) {

   throw new DaoException(DaoException.ERRORCODE_EXCUTEHQL);

   }

return list;

}

 

8. 根據HQL/SQL 查詢

   public List queryByHql(final String hql, final Object[] prams,final String sql) {

     return (List)  getHibernateTemplate().execute(new HibernateCallback(){

       public Object doInHibernate(Session session)

         throws HibernateException, SQLException {

            if(hql!=null && hql.length()>0){

           Query query=session.createQuery(hql);

           if(prams!=null && prams.length>0){

           for(int i=0;i<prams.length;i++){

           query.setParameter(i,prams[i]);

         }

      }

     return query.list();

   }else{

   SQLQuery sqlquery=session.createSQLQuery(sql);

   return sqlquery.list();

 }

}

});

}

 

9. 儲存/更新

   public String saveOrUpdateObject(ISuperVO vo) throws DaoException {

     try {

         String id = null;

         if (StringUtil.isEmpty(vo.getPid())) {

            getHibernateTemplate().save(vo);

        } else {

           getHibernateTemplate().merge(vo);

       }

        id = vo.getPid();

        return id;

     } catch (Exception e) {

          e.printStackTrace();

}

}

 

10. getHibernateTemplate().delete(vo);  //刪除

 

11. 根據條件刪除

   public Integer deleteObjectsByWherePart(final Class voClass,final String wherePart, final Object[] parmaters)throws DaoException {

   try {

      Integer count = (Integer) getHibernateTemplate().execute( new HibernateCallback() {

     public Object doInHibernate(Session session)throws HibernateException, SQLException {

     Integer coun = null;

     String hql = "delete from " + voClass.getName()+ " where 1=1 ";

     if (wherePart != null && wherePart.trim().length() > 0) {

         hql = hql + " and " + wherePart;

     }

     Query query = session.createQuery(hql);

     Object obj = null;

     if (parmaters != null && parmaters.length > 0) {

         for (int i = 0; i < parmaters.length; i++) {

            obj = parmaters[i];

           query.setParameter(i, obj);

        }

      }

     coun = query.executeUpdate();

     return coun;

    }

   });

   return count;

  } catch (Exception e) {

  e.printStackTrace();

}

}

hql Hibernate.gethibernatetemplate()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.