成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Hibernate多條件查詢方法收錄

開(kāi)發(fā) 后端
本文介紹了兩種Hibernate多條件查詢的方法。一個(gè)是通用方法,另一個(gè)則是用sql拼接,將搜索的多個(gè)條件在外部(即調(diào)用方)封裝在了數(shù)組中。

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

  1. //value[i]為第i個(gè)查詢條件propertyName[i]的值          (本方法已通過(guò)測(cè)試)  
  2.  
  3. /*多條件查詢,查詢條件的值為空時(shí)自動(dòng)除去該條件  
  4. * rigor為true時(shí)采用精確查詢  
  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. }  

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

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

  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// 父地區(qū);     
  20.            Integer careaId = 0// 子地區(qū);     
  21.            Integer categoryId = 0// 類(lèi)別;     
  22.            String infoPrivider = "中介"// 來(lái)源;     
  23.            String houseType= "地下室"// 房屋類(lèi)型;     
  24.            Integer hxBedRoom=0// 室;     
  25.            Integer hxLivingRoom=0// 廳;     
  26.                 
  27.            String hzHouseStatus="有房出租"// 合租類(lèi)型;     
  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// 租金/價(jià)格;     
  35.            String title= "出租"// 標(biāo)題;     
  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.     }    

【編輯推薦】

  1. Hibernate中g(shù)enerator屬性的意義
  2. hibernate Key Generator 主鍵生成方式
  3. Hibernate的主鍵生成機(jī)制
  4. hibernate的Query cache
  5. Hibernate中hbm的generator屬性
     
責(zé)任編輯:book05 來(lái)源: 和訊博客
相關(guān)推薦

2009-06-08 10:20:01

Hibernate查詢

2009-06-17 15:52:23

Hibernate查詢

2009-09-15 09:33:46

linq多條件查詢

2010-06-10 17:59:05

2009-09-15 11:34:47

Linq多條件查詢

2019-11-15 10:01:07

MySQL數(shù)據(jù)庫(kù)數(shù)據(jù)

2009-06-17 14:17:40

Criteria條件查Hibernate

2010-11-09 15:18:37

SQL Server多

2013-05-27 10:11:25

路由器查詢方式路由器遞歸查詢路由器撲朔圖

2012-07-30 09:50:28

MongoDB

2010-11-15 16:26:46

Oracle系統(tǒng)時(shí)間

2010-10-29 11:22:23

Oracle用戶會(huì)話

2010-09-25 16:42:45

sql語(yǔ)句

2009-01-27 21:00:00

服務(wù)器數(shù)據(jù)庫(kù)SQL Server

2009-05-21 09:24:42

表空間查詢Oracle

2010-11-25 16:40:11

MySQL大表重復(fù)字段

2010-05-06 14:11:55

Oracle多條件查詢

2009-07-21 14:15:00

iBATIS.NET多

2024-03-04 11:13:29

Django數(shù)據(jù)庫(kù)Python

2022-06-21 08:13:34

MySQL查詢數(shù)據(jù)庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 大学生a级毛片免费视频 | 麻豆国产一区二区三区四区 | 天天操天天操 | 日韩一级免费电影 | 一区在线播放 | 免费观看一级特黄欧美大片 | 一区二区福利视频 | 成人免费视频网站在线观看 | 九九九视频在线观看 | 午夜视频免费在线观看 | 中文字幕亚洲精品在线观看 | 欧美色a v| 九九伦理电影 | 亚洲高清成人在线 | 国产精品99久 | 国内精品久久久久久 | www精品 | 91免费在线播放 | 亚洲国产成人精品在线 | 色婷婷一区二区三区四区 | 成人午夜av | 日韩精品免费在线观看 | 福利二区 | 亚洲欧美成人影院 | 精品一区二区电影 | 亚洲精品一区二区三区在线 | 欧美日韩在线一区二区三区 | 日韩精品无码一区二区三区 | 成人免费一区二区三区视频网站 | 日韩一二三区 | 国产高清视频在线观看 | 日韩av一区二区在线 | 色眯眯视频在线观看 | 欧美一区二区三区久久精品 | 亚洲在线视频 | 亚洲午夜精品久久久久久app | 国产精品黄 | 99re在线视频免费观看 | 精品一区二区三区在线观看 | 久久合久久 | 久热精品在线播放 |