【mybatis】mybatis-xml中开发过程中的小小坑

 

场景3:mybatis-xml动态查询条件中实现if else的效果

SQL写法

        <where>
            <if test="sInfoWindcode != null and sInfoWindcode != ''">
                AND a.s_info_windcode = #{sInfoWindcode}
            </if>
            <if test="sInfoWindcode == null || sInfoWindcode == ''">
                <![CDATA[ LIMIT 50 ]]>
            </if>>
        </where>

或者

        <choose>
            <when test="processStatus != null && processStatus != '' && processStatus != '-110'">
               and process_status = #{processStatus,jdbcType=VARCHAR} 
            </when>
            <otherwise>
                and process_status != 1
            </otherwise>
        </choose>

 

场景1:mybatis-xml查询条件字段的值是一个数组,也即前端的条件筛选的值可以多个

参数定义:

private String[] marketListBoardName;

SQL写法:

<if test="marketListBoardName != null and marketListBoardName != ''">
  AND ad.MARKET_LISTBOARDNAME IN
  <foreach collection="marketListBoardName" index="index" item="item" open="(" separator="," close=")">
    #{item}
  </foreach>
</if>

场景2:mybatis-xml查询条件字段的值是数字字符串如1或者y,如下写法的判断条件将不起作用

<if test="type == '1'">
  AND a.S_DIV_PROGRESS != '3'
</if>
<if test="type == 'y'">
  AND a.S_DIV_PROGRESS != '3'
</if>

更改为:

<if test='type == "1"'>
  AND a.S_DIV_PROGRESS != '3'
</if>
<if test="type == '1'.toString()">
  AND a.S_DIV_PROGRESS != '3'
</if>
<if test='type == "y"'>
  AND a.S_DIV_PROGRESS != '3'
</if>

 

【后续更新,请移步至:https://zorkelvll.cn/blogs/zorkelvll/articles/2018/11/15/1542213094869

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值