Hibernate中怎么實(shí)現(xiàn)多條件查詢

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Hibernate中怎么實(shí)現(xiàn)多條件查詢,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)保靖免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

1. Hibernate多條件查詢通用方法

//value[i]為第i個(gè)查詢條件propertyName[i]的值          (本方法已通過(guò)測(cè)試)   /*多條件查詢,查詢條件的值為空時(shí)自動(dòng)除去該條件  * rigor為true時(shí)采用精確查詢  */ public List searchByPropertys(String model,String[]propertyName,Object[] value,int page,boolean rigor){        StringBuffer sqlBuffer = new StringBuffer();      String ralation=" like ";      if(rigor){       ralation=" = ";      }      sqlBuffer.append("from "+model+" as model\n");      int len=propertyName.length;      List list=new ArrayList();      boolean first=true;      for(int i=0;i< len;i++){       if(value[i]!=null){       if(first){            sqlBuffer.append(" where "+ "model."+ propertyName[i] + ralation+" ?\n");            list.add(value[i]);        first=false;       }else{            sqlBuffer.append(" and "+ "model."+ propertyName[i] +ralation+ " ?\n");            list.add(value[i]);       }      }      }           try {                  Session session=getSession();               Query queryObject = session.createQuery(sqlBuffer.toString());               for(int i=0;i< list.size();i++){               if(rigor){                queryObject.setParameter(i, list.get(i));               }else{                queryObject.setParameter(i, "%"+list.get(i)+"%");               }                     }                          list=queryObject.list();              closeSession(session);        return list;           } catch (RuntimeException re) {              log.error("find by property name failed", re);              throw re;           }   }

2:hibernate多條件組合查詢 之 sql 拼接

這個(gè)方法與上面第一節(jié)中的相同,只不過(guò)上面的方法是將搜索的多個(gè)條件在外部(即調(diào)用方)封裝在了數(shù)組中。

public static void main(String[] args) {                        Session session = null;            Transaction tx = null;            List list = null;            Criteria criteria = null;                  try {                      session = HibernateSessionFactory.getSession();                tx = session.beginTransaction();                      DetachedCriteria detachedCriteria = DetachedCriteria                       .forClass(InfoTab.class);                                                String sql=" 1=1 ";                                Integer pareaId = 0; // 父地區(qū);                Integer careaId = 0; // 子地區(qū);                Integer categoryId = 0; // 類別;                String infoPrivider = "中介"; // 來(lái)源;                String houseType= "地下室"; // 房屋類型;                Integer hxBedRoom=0; // 室;                Integer hxLivingRoom=0; // 廳;                                String hzHouseStatus="有房出租"; // 合租類型;                String hzRequestSex="男"; // 性別要求;                String fixUp="尚未"; // 裝修程度;                Integer lcHeightMolecuse=0; // 樓層;                String orientation="東南"; // 朝向要求;                Integer buildArea=2000; // 建筑面積;                Integer useArea=80; // 使用面積;                Integer rentalDigit=2000; // 租金/價(jià)格;                String title= "出租"; // 標(biāo)題;                                if(pareaId!=0)                {                   sql+="pareaId=" + pareaId;                }                if(careaId!=0)                {                   sql+=" and careaId=" + careaId;                }                if(categoryId!=0)                {                   sql+=" and categoryId=" + categoryId;                }                if(!infoPrivider.equals(""))                {                   sql+=" and infoPrivider='" + infoPrivider + "'";                }                if(!houseType.equals(""))                {                   sql+=" and houseType='" + houseType +"'";                }                if(hxBedRoom!=0)                {                   sql+=" and hxBedRoom=" + hxBedRoom;                }                if(hxLivingRoom!=0)                {                   sql+=" and hxLivingRoom=" + hxLivingRoom;                }                if(!hzHouseStatus.equals(""))                {                   sql+=" and hzHouseStatus='" + hzHouseStatus + "'";                }                if(!hzRequestSex.equals(""))                {                   sql+=" and hzRequestSex='" + hzRequestSex +"'";                }                if(!fixUp.equals(""))                {                   sql+=" and fixUp='" + fixUp + "'";                }                if(lcHeightMolecuse!=0)                {                   sql+=" and lcHeightMolecuse=" + lcHeightMolecuse;                }                if(!orientation.equals(""))                {                   sql+=" and orientation='" + orientation + "'";                }                if(buildArea!=0)                {                    sql+=" and buildArea=" + buildArea;                }                if(useArea!=0)                {                   sql+=" and useArea=" + useArea;                }                if(rentalDigit!=0)                {                   sql+=" and rentalDigit=" + rentalDigit;                }                if(!title.equals(""))                {                   sql+=" and title like '%" + title + "%'";                }                sql+=" order by id desc";                                System.out.println(sql);                      detachedCriteria.add(Restrictions.sqlRestriction(sql));                      criteria = detachedCriteria.getExecutableCriteria(session);                      list = criteria.list();                                for(int i=0;i< list.size();i++)                {                   InfoTab infoTab = (InfoTab)list.get(i);                   System.out.println(infoTab.getTitle() +" "+ infoTab.getCategoryId() +" "+ infoTab.getPareaName() +" "+ infoTab.getCareaName() +" " + infoTab.getHouseType() +" " + infoTab.getInfoPrivider());                }                      tx.commit();                  } catch (HibernateException he) {                he.printStackTrace();            }         }

上述就是小編為大家分享的Hibernate中怎么實(shí)現(xiàn)多條件查詢了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享文章:Hibernate中怎么實(shí)現(xiàn)多條件查詢
文章來(lái)源:http://bm7419.com/article32/jjcisc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站營(yíng)銷、企業(yè)網(wǎng)站制作電子商務(wù)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司