- /s篩選去除無效數據
- /* detachedCriteria.add(
- Restrictions.or(
- Restrictions.like("chanpin", "冰箱", MatchMode.ANYWHERE),
- Restrictions.or(
- Restrictions.like("chanpin", "洗衣機", MatchMode.ANYWHERE),
- Restrictions.or(
- Restrictions.like("chanpin", "熱水器", MatchMode.ANYWHERE),
- Restrictions.like("chanpin", "空調", MatchMode.ANYWHERE))))
- );
- */
- Disjunction dis=Restrictions.disjunction();
- dis.add(Restrictions.like("chanpin", "冰箱", MatchMode.ANYWHERE));
- dis.add(Restrictions.like("chanpin", "洗衣機", MatchMode.ANYWHERE));
- dis.add(Restrictions.like("chanpin", "熱水器", MatchMode.ANYWHERE));
- dis.add(Restrictions.like("chanpin", "空調", MatchMode.ANYWHERE));
- detachedCriteria.add(dis);
- //e篩選去除無效數據
用來組合一組邏輯或【or】條件的方法 sql
- Restrictions.disjunction();
用來組合一組邏輯與【and】條件的方法 post
- Restrictions.conjunction();
實際中有sql以下: where transportType=1 and ((pol=1 and pod=2) or (pol=1 and pod=3) or (pol=1 and pod=4))spa
代碼以下code
Criteria criteria = getCriteria(); if(transportType!=null)criteria.add(Restrictions.eq("transportType", transportType)); if(polIdStr!=null && podIdStr!=null) { List<Integer> polIdList = ListUtil.stringToList(polIdStr); List<Integer> podIdList = ListUtil.stringToList(podIdStr); List<Criterion> CriterionList=new ArrayList<Criterion>(); Disjunction dis = Restrictions.disjunction();//多個or能夠拼蛸 for(int polId:polIdList){ for(int podId:podIdList){ Conjunction con=Restrictions.conjunction();//多個and拼接 con.add(Restrictions.eq("polId", polId)); con.add(Restrictions.eq("podId", podId)); dis.add(con); } } criteria.add(dis); }