1. 程式人生 > >【mybatis】mybatis-xml中開發過程中的小小坑

【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>