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.  



posted @ 2013-08-14 09:12  huangt  阅读(431)  评论(0编辑  收藏  举报