【MyBatis】<if test=""></if>标签的条件判断(Boolean类型参数)

版权声明:陪伴你的每一天 https://blog.csdn.net/cwhuang1993/article/details/81981764

在MyBatis 中,动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 3 之前的版本中,有很多元素需要花时间了解。而MyBatis 3 大大精简了元素种类,只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。


<!-- <if test=" "></if>标签的使用实例-->

<select id="findActiveBlogWithTitleLike" resultType="Blog">
    SELECT
        * 
    FROM 
        BLOG 
    WHERE
        state = ‘ACTIVE’ 
        <if test="title != null">
            AND title like #{title,jdbcType=VARCHAR}
        </if>
</select>

<!-- <if test=""></if>标签 -->

<!-- 对于字符串类型参数,可以用如下写法: -->
<if test="username" != null>
    username=#{username, jdbcType=VARCHAR}
</if>

<!-- 或者 -->
<if test="username != null and 'John' == username">
    username=#{username, jdbcType=VARCHAR}
</if>

<!-- 但是,对于非字符串类型的参数(如Boolean),就需要写成: -->
<if test="flag != null and 'true'.toString() == flag.toString()">
    flage=#{flag, jdbcType=BOOLEAN}
</if>

猜你喜欢

转载自blog.csdn.net/cwhuang1993/article/details/81981764
今日推荐