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

[java] view plaincopyprint?
  1. public static void main(String[] args) {
  2. Session session = null;
  3. Transaction tx = null;
  4. List list = null;
  5. Criteria criteria = null;
  6. try {
  7. session = HibernateSessionFactory.getSession();
  8. tx = session.beginTransaction();
  9. DetachedCriteria detachedCriteria = DetachedCriteria
  10. .forClass(InfoTab.class);
  11. String sql=" 1=1 ";
  12. Integer pareaId = 0; // 父地区;
  13. Integer careaId = 0; // 子地区;
  14. Integer categoryId = 0; // 类别;
  15. String infoPrivider = "中介"; // 来源;
  16. String houseType= "地下室"; // 房屋类型;
  17. Integer hxBedRoom=0; // 室;
  18. Integer hxLivingRoom=0; // 厅;
  19. String hzHouseStatus="有房出租"; // 合租类型;
  20. String hzRequestSex="男"; // 性别要求;
  21. String fixUp="尚未"; // 装修程度;
  22. Integer lcHeightMolecuse=0; // 楼层;
  23. String orientation="东南"; // 朝向要求;
  24. Integer buildArea=2000; // 建筑面积;
  25. Integer useArea=80; // 使用面积;
  26. Integer rentalDigit=2000; // 租金/价格;
  27. String title= "出租"; // 标题;
  28. if(pareaId!=0)
  29. {
  30. sql+="pareaId=" + pareaId;
  31. }
  32. if(careaId!=0)
  33. {
  34. sql+=" and careaId=" + careaId;
  35. }
  36. if(categoryId!=0)
  37. {
  38. sql+=" and categoryId=" + categoryId;
  39. }
  40. if(!infoPrivider.equals(""))
  41. {
  42. sql+=" and infoPrivider='" + infoPrivider + "'";
  43. }
  44. if(!houseType.equals(""))
  45. {
  46. sql+=" and houseType='" + houseType +"'";
  47. }
  48. if(hxBedRoom!=0)
  49. {
  50. sql+=" and hxBedRoom=" + hxBedRoom;
  51. }
  52. if(hxLivingRoom!=0)
  53. {
  54. sql+=" and hxLivingRoom=" + hxLivingRoom;
  55. }
  56. if(!hzHouseStatus.equals(""))
  57. {
  58. sql+=" and hzHouseStatus='" + hzHouseStatus + "'";
  59. }
  60. if(!hzRequestSex.equals(""))
  61. {
  62. sql+=" and hzRequestSex='" + hzRequestSex +"'";
  63. }
  64. if(!fixUp.equals(""))
  65. {
  66. sql+=" and fixUp='" + fixUp + "'";
  67. }
  68. if(lcHeightMolecuse!=0)
  69. {
  70. sql+=" and lcHeightMolecuse=" + lcHeightMolecuse;
  71. }
  72. if(!orientation.equals(""))
  73. {
  74. sql+=" and orientation='" + orientation + "'";
  75. }
  76. if(buildArea!=0)
  77. {
  78. sql+=" and buildArea=" + buildArea;
  79. }
  80. if(useArea!=0)
  81. {
  82. sql+=" and useArea=" + useArea;
  83. }
  84. if(rentalDigit!=0)
  85. {
  86. sql+=" and rentalDigit=" + rentalDigit;
  87. }
  88. if(!title.equals(""))
  89. {
  90. sql+=" and title like '%" + title + "%'";
  91. }
  92. sql+=" order by id desc";
  93. System.out.println(sql);
  94. detachedCriteria.add(Restrictions.sqlRestriction(sql));
  95. criteria = detachedCriteria.getExecutableCriteria(session);
  96. list = criteria.list();
  97. for(int i=0;i<list.size();i++)
  98. {
  99. InfoTab infoTab = (InfoTab)list.get(i);
  100. System.out.println(infoTab.getTitle() +" "+ infoTab.getCategoryId() +" "+ infoTab.getPareaName() +" "+ infoTab.getCareaName() +" " + infoTab.getHouseType() +" " + infoTab.getInfoPrivider());
  101. }
  102. tx.commit();
  103. } catch (HibernateException he) {
  104. he.printStackTrace();
  105. }
  106. }

hibernate 多条件组合查询之sql拼接相关推荐

  1. SQL如何构建多条件组合查询,而且不降低效率

    我们知道,在一般的信息系统中,特别是主要信息表,如客户基本信息,工单受理主界面,用户会用到多条件组合查询. 我看过一些系统,有的人是采用将一个表的栏位给用户选择,然后用户可以针对某一栏位来下条件,但是 ...

  2. 树控件,多条件组合查询与混合数据源

    //实现多条件查询 //...连接数据库的代码 SqlDataAdapter sda = new SqlDataAdapter(sql,con); DataTable dt = new DataTab ...

  3. solr的多条件组合查询和solr的范围查询【转】

    solr的多条件组合查询和solr的范围查询 版权声明:本文为博主原创文章,供大家参考,但不要抄袭哦! 存在问题:为了减轻数据库的访问压力,往往我们将必要的数据存储到solr中,并给部分字段建立索引, ...

  4. 多条件组合查询+分页

    比较麻烦,把他记录下来. 前台UI: 实现的类代码: 程序代码 /// <summary>         /// 多条件组合查询         /// </summary> ...

  5. 亿级数据多条件组合查询——秒级响应解决方案

    1 概述 组合查询为多条件组合查询,在很多场景下都有使用.购物网站中通过勾选类别.价格.销售量范围等属性来对所有的商品进行筛选,筛选出满足客户需要的商品,这是一种典型的组合查询.在小数据量的情况下,后 ...

  6. php js脚本查询php,php结合js实现多条件组合查询

    本文实例为大家分享了php结合js实现多条件组合查询的具体代码,供大家参考,具体内容如下 一.效果图 二.前端代码 Jquery分类 .templinkactive { padding:5px; te ...

  7. 条件查询_多条件组合查询---测试用例设计

    一.假设查询因子:A,B,C,D,E(下拉框+复选框) 1.单独查询:A:B:C:D:E - 确保单独查询的正确性,这也是最基本的. 2.两个组合查询:AB:AC:AD:AE:BC:BD:BE:CD: ...

  8. 《MySQL数据操作与查询》- 维护学生信息、老师信息和成绩信息 支持按多种条件组合查询学生信息和成绩信息

    综合项目需求 一.系统整体功能 系统需支持以下功能: 维护学生信息.老师信息和成绩信息 支持按多种条件组合查询学生信息和成绩信息 学生 Student(id,班级id,学号,姓名,性别,电话,地址,出 ...

  9. solr的多条件组合查询

    今天,再工作中遇到了solr的多条件组合查询,于是在网上查到一些博客--多条件查询,这篇写的不错,但是我也想把我自己的记录下来.话不多说,直接放代码.注意:,在接收的参数由于都是String类型的,所 ...

最新文章

  1. 区分C语言中getch、getche、fgetc、getc、getchar、fgets、gets
  2. (简要介绍)Winograd schema challenge(Winograd question)
  3. [工具]-C语言实现:生成一个全是0x0的二进制文件
  4. php操作redis phpredis扩展
  5. 前端学习(1837):前端面试题之变量提升
  6. 【亲测有效】运行docker ps 出现Got permission denied问题的解决方案
  7. tensorflow之eval
  8. java simpedateformat_java中Date,SimpleDateFormat
  9. 2022上半年软件设计师历年真题
  10. canvas 加载图片
  11. python课设答辩ppt_学生成绩管理系统答辩幻灯片.ppt
  12. 嵌入式通过序列号加密总结及flash…
  13. POJ - 3311- Hie with the Pie ( 状压dp )
  14. 如何在ipone自带邮件上添加网易邮箱
  15. 性能测试:深入理解并发量,线程数,吞吐量,TPS
  16. Coding and Paper Letter(七十六)
  17. Linux下c语言文本方式文件读取
  18. 基于Python的Django框架的二手物品交易平台的设计与实现
  19. 中国羟丙甲纤维素胶囊市场深度研究分析报告
  20. aardio - nanomsg/nnmsg/nnm 消息发送模式图示

热门文章

  1. 字符串经典题之扑克牌的大小
  2. 动态随机存储器的刷新(集中刷新、分散刷新、异步刷新的区别)
  3. KVM复制虚拟机,KVM克隆虚拟机
  4. phpstudy下载、安装、配置、网站部署、卸载(windows下php、apache、nginx环境测试)
  5. C/C++ 去掉字符串首位的空格字符
  6. Python 使用ntplib库同步校准当地时间的方法 (NTP)
  7. Linux C线程同步的三种方法
  8. LeetCode 20. Valid Parentheses(c++)
  9. [转] Oracle学习之创建数据库(新建实例)
  10. 《媒体曝70多万种药品“底价”:普遍超厂价5-6倍》——典型的媒体噱头唬人行为...