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。经过验证是没有问题的。



  • 相关阅读:
    单词小课堂
    js数组
    js规范
    css
    seajs
    IDEA快捷键
    移动端设备禁止页面滑动
    sass中的!default的作用
    【数据分析 R语言实战】学习笔记 第八章 方差分析与R实现
    excel合并单元格
  • 原文地址:https://www.cnblogs.com/juncaoit/p/12342555.html
Copyright © 2011-2022 走看看