Hibernate多条件查询方法收录

开发 后端
本文介绍了两种Hibernate多条件查询的方法。一个是通用方法,另一个则是用sql拼接,将搜索的多个条件在外部(即调用方)封装在了数组中。

1. Hibernate多条件查询通用方法

//value[i]为第i个查询条件propertyName[i]的值          (本方法已通过测试)  
 
/*多条件查询,查询条件的值为空时自动除去该条件  
* rigor为true时采用精确查询  
*/ 
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;  
         }  
 
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.

2:hibernate多条件组合查询 之 sql 拼接

这个方法与上面第一节中的相同,只不过上面的方法是将搜索的多个条件在外部(即调用方)封装在了数组中。

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// 父地区;     
           Integer careaId = 0// 子地区;     
           Integer categoryId = 0// 类别;     
           String infoPrivider = "中介"// 来源;     
           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// 租金/价格;     
           String title= "出租"// 标题;     
                
           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();     
       }     
    }    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.

【编辑推荐】

  1. Hibernate中generator属性的意义
  2. hibernate Key Generator 主键生成方式
  3. Hibernate的主键生成机制
  4. hibernate的Query cache
  5. Hibernate中hbm的generator属性
     
责任编辑:book05 来源: 和讯博客
相关推荐

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多条件查询

2009-06-17 14:17:40

Criteria条件查Hibernate

2019-11-15 10:01:07

MySQL数据库数据

2010-11-09 15:18:37

SQL Server多

2010-10-29 11:22:23

Oracle用户会话

2012-07-30 09:50:28

MongoDB

2013-05-27 10:11:25

路由器查询方式路由器递归查询路由器扑朔图

2010-11-15 16:26:46

Oracle系统时间

2010-09-25 16:42:45

sql语句

2010-11-25 16:40:11

MySQL大表重复字段

2009-01-27 21:00:00

服务器数据库SQL Server

2009-05-21 09:24:42

表空间查询Oracle

2010-05-06 14:11:55

Oracle多条件查询

2022-06-21 08:13:34

MySQL查询数据库

2024-03-04 11:13:29

Django数据库Python

2009-07-21 14:15:00

iBATIS.NET多
点赞
收藏

51CTO技术栈公众号