hibernate Criteria中多个or和and的用法 and ( or or)

  1. /s筛选去除无效数据  
  2. /*      detachedCriteria.add( 
  3.                 Restrictions.or( 
  4.                 Restrictions.like("chanpin", "冰箱", MatchMode.ANYWHERE),  
  5.                 Restrictions.or( 
  6.                 Restrictions.like("chanpin", "洗衣机", MatchMode.ANYWHERE),  
  7.                 Restrictions.or( 
  8.                 Restrictions.like("chanpin", "热水器", MatchMode.ANYWHERE),  
  9.                 Restrictions.like("chanpin", "空调", MatchMode.ANYWHERE)))) 
  10.                 ); 
  11. */  
  12.         Disjunction dis=Restrictions.disjunction();  
  13.         dis.add(Restrictions.like("chanpin", "冰箱", MatchMode.ANYWHERE));  
  14.         dis.add(Restrictions.like("chanpin", "洗衣机", MatchMode.ANYWHERE));  
  15.         dis.add(Restrictions.like("chanpin", "热水器", MatchMode.ANYWHERE));  
  16.         dis.add(Restrictions.like("chanpin", "空调", MatchMode.ANYWHERE));  
  17.         detachedCriteria.add(dis);  
  18.         //e筛选去除无效数据  



用来组合一组逻辑或【or】条件的方法 

Java代码  收藏代码
  1. Restrictions.disjunction();  



用来组合一组逻辑与【and】条件的方法 

Java代码 
  1. 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);
        }

 

posted @ 2019-06-19 12:10  为乐而来  阅读(8851)  评论(0编辑  收藏  举报