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. }  

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.     }    

【编辑推荐】

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

2009-09-15 11:34:47

Linq多条件查询

2010-06-10 17:59:05

2019-11-15 10:01:07

MySQL数据库数据

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系统时间

2010-10-29 11:22:23

Oracle用户会话

2010-09-25 16:42:45

sql语句

2009-01-27 21:00:00

服务器数据库SQL Server

2009-05-21 09:24:42

表空间查询Oracle

2010-11-25 16:40:11

MySQL大表重复字段

2010-05-06 14:11:55

Oracle多条件查询

2009-07-21 14:15:00

iBATIS.NET多

2024-03-04 11:13:29

Django数据库Python

2022-06-21 08:13:34

MySQL查询数据库
点赞
收藏

51CTO技术栈公众号