中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Hibernate中怎么實現多條件查詢

發布時間:2021-08-11 15:32:03 來源:億速云 閱讀:138 作者:Leah 欄目:編程語言

這期內容當中小編將會給大家帶來有關Hibernate中怎么實現多條件查詢,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

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

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();            }         }

上述就是小編為大家分享的Hibernate中怎么實現多條件查詢了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

桃江县| 老河口市| 文山县| 泰州市| 绥宁县| 桐城市| 镇安县| 南昌市| 霍州市| 蛟河市| 兴隆县| 同德县| 嘉荫县| 文昌市| 治多县| 墨玉县| 时尚| 绥滨县| 厦门市| 肃宁县| 武夷山市| 邻水| 循化| 永登县| 淅川县| 荔浦县| 金寨县| 竹北市| 体育| 隆回县| 和林格尔县| 天祝| 怀来县| 武隆县| 通榆县| 鲜城| 巢湖市| 门头沟区| 邓州市| 江口县| 布尔津县|