注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 《网络规划设计师教程》..
 帮助

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


2007-07-13 09:35:44
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://maomao.blog.51cto.com/115985/33883
    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();
       }
    }

本文出自 “maomao” 博客,请务必保留此出处http://maomao.blog.51cto.com/115985/33883



类别:Hibernate 技术圈()阅读() ┆ 评论()推送到技术圈返回首页  


    文章评论
 
2007-10-14 16:27:48
好难看到做程序的mm!!!~~~可否认识一下???
QQ 651206131

2007-12-21 20:16:50
不错,好想法
有一点美中不足,sql拼节面临数据库兼容问题,比如
Date类型字段,oracle 需要用函数把字符串转成Date类型才可以查询

2008-01-08 13:17:47
这样对中文有效么

2008-01-09 00:40:22
有效

2008-02-17 14:21:55
if 判断太多了点吧
举个极端的例子:要是这张表有100个字段呢?
可否改成 映射来循环遍历属性??
不过大体思路不错啊..也许可以解决我现在头疼的问题

2008-05-23 09:46:28
> if 判断太多了点吧
> 举个极端的例子:要是这张表有100个字段呢?
> 可否改成 映射来循环遍历属性??

建议骗一下自己那一堆if不是代码,而是映射表就好了 ~_*

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: