zoukankan      html  css  js  c++  java
  • Hibernate:Disjunction&Conjunction构造复杂的查询条件.

                                                                          Hibernate:Disjunction&Conjunction构造复杂的查询条件

    Disjunction和Conjunction是逻辑或和逻辑与,如下:

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

    1.Restrictions.disjunction();

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

    2.Restrictions.conjunction();
    


     

    实例一:构造复杂的SQL查询条件

    private void CheckBsc_lj(Criteria queryCriteria) 
     {
      Disjunction disjunction = Restrictions.disjunction();
      Criterion cirterion = Restrictions.sqlRestriction("SIMULPORTCAPACITY<SIMULPORTCAPACITYOCUPIED".toLowerCase());
      disjunction.add(cirterion);
      cirterion = Restrictions.sqlRestriction("ADSLPORTCAPACITY<ADSLPORTCAPACITYOCCUPIED".toLowerCase());
      disjunction.add(cirterion);
      cirterion = Restrictions.sqlRestriction("LANPORTCAPACITY<LANPORTCAPACITYOCCUPIED".toLowerCase());
      disjunction.add(cirterion);
      
      // ONU端口,至少要录入一种端口
      Conjunction conjunction = Restrictions.conjunction();
      cirterion = Restrictions.eq("lanportcapacity", 0);
      conjunction.add(cirterion);
      cirterion = Restrictions.eq("simulportcapacity", 0);
      conjunction.add(cirterion);
      cirterion = Restrictions.eq("adslportcapacity", 0);
      conjunction.add(cirterion);
    
      disjunction.add(conjunction);
      queryCriteria.add(disjunction);
     }
    
    构造出的条件如下:
    select *
      from aaaa this_
     where (simulportcapacity < simulportcapacityocupied or
           adslportcapacity < adslportcapacityoccupied or
           lanportcapacity < lanportcapacityoccupied or
           (this_.LANPORTCAPACITY = ? and this_.SIMULPORTCAPACITY = ? and
           this_.ADSLPORTCAPACITY = ?)) 

    实例二:构造模糊查询

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



     


     

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    (网络流)ACM Computer Factory --POJ --3436
    (小数化分数)小数化分数2 -- HDU --1717
    (小数化分数)小数化分数2 --HDU --1717
    (网络流 模板 Dinic) Drainage Ditches --POJ --1273
    (网络流 模板 Edmonds-Karp)Drainage Ditches --POJ --1273
    (匹配)Oil Skimming -- hdu --4185
    (匹配 二维建图) Antenna Placement --POJ --3020
    (匹配)Antenna Placement --POJ --3020
    将截断字符串或二进制数据【转】
    C#中Abstract和Virtual 【转】
  • 原文地址:https://www.cnblogs.com/ywx-vashon/p/4895707.html
Copyright © 2011-2022 走看看