Hibernate多條件查詢方法收錄

Hibernate多條件查詢方法收錄


  • 本文介紹了兩種Hibernate多條件查詢的方法。一個是通用方法,另一個則是用sql拼接,將搜索的多個條件在外部(即調用方)封裝在了數組中。

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

  1. //value[i]爲第i個查詢條件propertyName[i]的值          (本方法已通過測試)  
  2.  
  3. /*多條件查詢,查詢條件的值爲空時自動除去該條件  
  4. * rigor爲true時採用精確查詢  
  5. */ 
  6. public List searchByPropertys(String model,String[]propertyName,Object[] value,int page,boolean rigor){    
  7.     StringBuffer sqlBuffer = new StringBuffer();  
  8.     String ralation=" like ";  
  9.     if(rigor){  
  10.      ralation=" = ";  
  11.     }  
  12.     sqlBuffer.append("from "+model+" as model\n");  
  13.     int len=propertyName.length;  
  14.     List list=new ArrayList();  
  15.     boolean first=true;  
  16.     for(int i=0;i< len;i++){  
  17.      if(value[i]!=null){  
  18.      if(first){      
  19.       sqlBuffer.append(" where ""model."+ propertyName[i] + ralation+" ?\n");      
  20.       list.add(value[i]);  
  21.       first=false;  
  22.      }else{      
  23.       sqlBuffer.append(" and ""model."+ propertyName[i] +ralation+ " ?\n");      
  24.       list.add(value[i]);  
  25.      }  
  26.     }  
  27.     }  
  28.     
  29.      try {            
  30.       Session session=getSession();  
  31.              Query queryObject = session.createQuery(sqlBuffer.toString());  
  32.              for(int i=0;i< list.size();i++){  
  33.              if(rigor){  
  34.               queryObject.setParameter(i, list.get(i));  
  35.              }else{  
  36.               queryObject.setParameter(i, "%"+list.get(i)+"%");  
  37.              }  
  38.              
  39.       }  
  40.             
  41.             list=queryObject.list();  
  42.             closeSession(session);  
  43.       return list;  
  44.          } catch (RuntimeException re) {  
  45.             log.error("find by property name failed", re);  
  46.             throw re;  
  47.          }  
  48.  
  49. }
  50.  

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

這個方法與上面第一節中的相同,只不過上面的方法是將搜索的多個條件在外部(即調用方)封裝在了數組中。

  1. public static void main(String[] args) {     
  2.             
  3.        Session session = null;     
  4.        Transaction tx = null;     
  5.        List list = null;     
  6.        Criteria criteria = null;     
  7.       
  8.        try {     
  9.       
  10.            session = HibernateSessionFactory.getSession();     
  11.            tx = session.beginTransaction();     
  12.       
  13.            DetachedCriteria detachedCriteria = DetachedCriteria     
  14.                   .forClass(InfoTab.class);     
  15.                 
  16.                 
  17.            String sql=" 1=1 ";     
  18.                 
  19.            Integer pareaId = 0// 父地區;     
  20.            Integer careaId = 0// 子地區;     
  21.            Integer categoryId = 0// 類別;     
  22.            String infoPrivider = "中介"// 來源;     
  23.            String houseType= "地下室"// 房屋類型;     
  24.            Integer hxBedRoom=0// 室;     
  25.            Integer hxLivingRoom=0// 廳;     
  26.                 
  27.            String hzHouseStatus="有房出租"// 合租類型;     
  28.            String hzRequestSex="男"// 性別要求;     
  29.            String fixUp="尚未"// 裝修程度;     
  30.            Integer lcHeightMolecuse=0// 樓層;     
  31.            String orientation="東南"// 朝向要求;     
  32.            Integer buildArea=2000// 建築面積;     
  33.            Integer useArea=80// 使用面積;     
  34.            Integer rentalDigit=2000// 租金/價格;     
  35.            String title= "出租"// 標題;     
  36.                 
  37.            if(pareaId!=0)     
  38.            {     
  39.               sql+="pareaId=" + pareaId;     
  40.            }     
  41.            if(careaId!=0)     
  42.            {     
  43.               sql+=" and careaId=" + careaId;     
  44.            }     
  45.            if(categoryId!=0)     
  46.            {     
  47.               sql+=" and categoryId=" + categoryId;     
  48.            }     
  49.            if(!infoPrivider.equals(""))     
  50.            {     
  51.               sql+=" and infoPrivider='" + infoPrivider + "'";     
  52.            }     
  53.            if(!houseType.equals(""))     
  54.            {     
  55.               sql+=" and houseType='" + houseType +"'";     
  56.            }     
  57.            if(hxBedRoom!=0)     
  58.            {     
  59.               sql+=" and hxBedRoom=" + hxBedRoom;     
  60.            }     
  61.            if(hxLivingRoom!=0)     
  62.            {     
  63.               sql+=" and hxLivingRoom=" + hxLivingRoom;     
  64.            }     
  65.            if(!hzHouseStatus.equals(""))     
  66.            {     
  67.               sql+=" and hzHouseStatus='" + hzHouseStatus + "'";     
  68.            }     
  69.            if(!hzRequestSex.equals(""))     
  70.            {     
  71.               sql+=" and hzRequestSex='" + hzRequestSex +"'";     
  72.            }     
  73.            if(!fixUp.equals(""))     
  74.            {     
  75.               sql+=" and fixUp='" + fixUp + "'";     
  76.            }     
  77.            if(lcHeightMolecuse!=0)     
  78.            {     
  79.               sql+=" and lcHeightMolecuse=" + lcHeightMolecuse;     
  80.            }     
  81.            if(!orientation.equals(""))     
  82.            {     
  83.               sql+=" and orientation='" + orientation + "'";     
  84.            }     
  85.            if(buildArea!=0)     
  86.            {     
  87.                sql+=" and buildArea=" + buildArea;     
  88.            }     
  89.            if(useArea!=0)     
  90.            {     
  91.               sql+=" and useArea=" + useArea;     
  92.            }     
  93.            if(rentalDigit!=0)     
  94.            {     
  95.               sql+=" and rentalDigit=" + rentalDigit;     
  96.            }     
  97.            if(!title.equals(""))     
  98.            {     
  99.               sql+=" and title like '%" + title + "%'";     
  100.            }     
  101.            sql+=" order by id desc";     
  102.                 
  103.            System.out.println(sql);     
  104.       
  105.            detachedCriteria.add(Restrictions.sqlRestriction(sql));     
  106.       
  107.            criteria = detachedCriteria.getExecutableCriteria(session);     
  108.       
  109.            list = criteria.list();     
  110.                 
  111.            for(int i=0;i< list.size();i++)     
  112.            {     
  113.               InfoTab infoTab = (InfoTab)list.get(i);     
  114.               System.out.println(infoTab.getTitle() +" "+ infoTab.getCategoryId() +" "+ infoTab.getPareaName() +" "+ infoTab.getCareaName() +" " + infoTab.getHouseType() +" " + infoTab.getInfoPrivider());     
  115.            }     
  116.       
  117.            tx.commit();     
  118.       
  119.        } catch (HibernateException he) {     
  120.            he.printStackTrace();     
  121.        }     
  122.     }  
  123.  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章