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

来源:互联网 发布:系统还原数据会消失吗 编辑:程序博客网 时间:2024/04/26 18:39

  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. }
原创粉丝点击