MyBatisの動的SQLでchoose, when, otherwise構文を使用

MyBatisの条件式

MyBatisの動的SQLでif elseを使用したい場合、choose, when, otherwise構文を使用する。 MyBatisにはifはあるがelseはない。elseと同様の動きを実現するためにはchoose, when, otherwise構文を使用する。

www.mybatis.org

書き方

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <!-- testパラメータがnullではない -->
    <when test="title != null">
      AND title like #{title}
    </when>
    <!-- when句に当てはまらなかった場合(else) -->
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>