在MyBatis 中,动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 3 之前的版本中,有很多元素需要花时间了解。而MyBatis 3 大大精简了元素种类,只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
<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="username" != null>
username=#{username, jdbcType=VARCHAR}
</if>
<if test="username != null and 'John' == username">
username=#{username, jdbcType=VARCHAR}
</if>
<if test="flag != null and 'true'.toString() == flag.toString()">
flage=#{flag, jdbcType=BOOLEAN}
</if>