日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

hql Hibernate.gethibernatetemplate()

發(fā)布時間:2023/12/9 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hql Hibernate.gethibernatetemplate() 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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); ?

???查詢結(jié)果:狀態(tài)為Active的用戶(對象從0到20 計數(shù)) ?

?

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);

???查詢結(jié)果:姓名為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);

???查詢結(jié)果:姓名為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);// 定義返回的記錄數(shù)

???????List list = query.list();

???????return list;

?????}

????});

???} catch (Exception e) {

???throw new DaoException(DaoException.ERRORCODE_EXCUTEHQL);

???}

return list;

}

?

8. 根據(jù)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. 根據(jù)條件刪除

???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();

}

}

總結(jié)

以上是生活随笔為你收集整理的hql Hibernate.gethibernatetemplate()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。