hibernate Criteria中多个or和and的用法 and ( or or)
- /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】条件的方法
- Restrictions.disjunction();
用来组合一组逻辑与【and】条件的方法
- Restrictions.conjunction();
实际中有sql如下: where transportType=1 and ((pol=1 and pod=2) or (pol=1 and pod=3) or (pol=1 and pod=4))
代码如下
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); }
欢迎加入JAVA技术交流QQ群:179945282
欢迎加入ASP.NET(C#)交流QQ群:17534377