良く記述するパターン、
パラメータ変数 String type; に対して、、
WHERE 1=1 <if test="type == null"> AND id = #{id} </if>
<if test="type != null">
文字列比較:空文字
<if test="type == ''">
文字列比較:任意
<if test="type == 'A012'">
以外にもあまり書かないパターンで、
パラメータ変数の型が boolean なら、
boolean flg であれば、
<if test="flg">
で、flg = true で then
<if test="!flg">
で、flg = false で then
パラメータ変数の型が int で以下
<if test="leng > 0">
AND条件は、&& と書けないので、「and」で書く小文字である
<if test="leng == 0 and type == null">
OR条件は、「||」または「or」で書く
<if test="leng == 0 || type == null">
if~else は、choose-when-otherwise の書式を使うしかない。
<choose> <when test="type != null and len == 0"> AND id = #{id} </when> <when test="name != null and len > 0"> AND name = #{name} </when> <otherwise> AND id IS NULL </otherwise> </choose>
trim の使い方
https://mybatis.org/mybatis-3/ja/dynamic-sql.html#trim-where-set
prefix | 要素内に文字列がある場合に、指定したprefixを先頭に付与する |
prefixOverrides | 要素内の文字列が、指定したprefixOverridesではじまる場合、その文字列を削除する。 |
suffix | 要素内に文字列がある場合に、指定したsuffixを末尾に付与する |
suffixOverrides | 要素内の文字列が、指定したsuffixOverridesで終わる場合、その文字列を削除する。 |
※ prefixOverrides と suffixOverrides は、指定する文字列|(パイプ)で複数指定が可能