• 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);
            }
  • 相关阅读:
    [探索][管理]《现在,发现你的优势》
    【成功智慧】010.依靠忍耐度过困难时期
    爱情五十九课,就差一句话
    VSS2005 托管 VS2010代码
    一个网站的金字塔战略
    【成功智慧】013.脚踏实地的去做,没有完不成的任务
    MU.Bread 麦卡优娜
    【成功智慧】012.要有耐心去等待成功的到来
    【成功智慧】009.要能够承受所发生的事情
    【成功智慧】014.一日复一日的度过难关
  • 原文地址:https://www.cnblogs.com/q149072205/p/11050541.html
Copyright © 2020-2023  润新知