zoukankan      html  css  js  c++  java
  • mybatis中的动态语句中多条件or如何书写

    1.说明

      sql如下:

    SELECT t.contract_id FROM `t_plm_contract_monitor` t WHERE 1=1 ANd (t.whole_id_one in ( 77 ) or t.whole_id_five in ( 77 ) or t.whole_id_six in ( 505602294 ))
    

      说明:

    t.whole_id_one,
    t.whole_id_two,
    t.whole_id_three,
    t.whole_id_four,
    t.whole_id_five,
    t.whole_id_six

    这些参数可能存在,也可能不存在,就要动态的平装语句了。

    2.做法【部分代码进行说明】

              <if test="(model.wholeIdOneList != null and model.wholeIdOneList.size()> 0) or (model.wholeIdTwoList != null and model.wholeIdTwoList.size()> 0) or
                       (model.wholeIdThreeList != null and model.wholeIdThreeList.size()> 0) or (model.wholeIdFourList != null and model.wholeIdFourList.size()> 0) or
                       (model.wholeIdFiveList != null and model.wholeIdFiveList.size()> 0) or (model.wholeIdSixList != null and model.wholeIdSixList.size()> 0)">
                        and (
                    </if>
                    <if test="model.wholeIdOneList != null and model.wholeIdOneList.size()> 0">
                        t.whole_id_one in
                        <foreach collection="model.wholeIdOneList" open="(" close=")" item="departmentId" separator=",">
                            #{departmentId,jdbcType=BIGINT}
                        </foreach>
                    </if>
                    <choose>
                        <when test="(model.wholeIdOneList == null or model.wholeIdOneList.size()==0) and (model.wholeIdTwoList != null and model.wholeIdTwoList.size()> 0)">
                            t.whole_id_two in
                            <foreach collection="model.wholeIdTwoList" open="(" close=")" item="departmentId" separator=",">
                                #{departmentId,jdbcType=BIGINT}
                            </foreach>
                        </when>
                        <when test="(model.wholeIdOneList != null and model.wholeIdOneList.size()> 0) and (model.wholeIdTwoList != null and model.wholeIdTwoList.size()> 0)">
                            OR t.whole_id_two in
                            <foreach collection="model.wholeIdTwoList" open="(" close=")" item="departmentId" separator=",">
                                #{departmentId,jdbcType=BIGINT}
                            </foreach>
                        </when>
                    </choose>

    。。。。。。。

      首先:根据值是否存在,将是否有这段逻辑的最外层的括号加进来

           然后,使用test进行判断list,如果前面的都不存在条件,第n个存在,则不需要加上or连接符

                      如果已经不是第一项了,则需要加上or。

    3.对choose的说明

      在比较规范的标签里,存在when和otherwise。

           但是有些场景下是不需要的,则可以不写otherwise。经过验证是没有问题的。



  • 相关阅读:
    Python练手例子(3)
    Python练手例子(2)
    Python练手例子(1)
    Python学习之旅(三十八)
    《剑指offer》— JavaScript(24)二叉树中和为某一值的路径
    《剑指offer》— JavaScript(23)二叉搜索树的后序遍历序列
    《剑指offer》— JavaScript(22)从上往下打印二叉树
    《剑指offer》— JavaScript(21)栈的压入、弹出序列
    《剑指offer》— JavaScript(20)包含min函数的栈
    《剑指offer》— JavaScript(19)顺时针打印矩阵
  • 原文地址:https://www.cnblogs.com/juncaoit/p/12342555.html
Copyright © 2011-2022 走看看