MyBatisのwhere句の記述方法

MyBatisのwhere句の記述方法

MyBatisのwhere句の記述方法です。

<where>~</where>の間に記述します。

<select id="selectCount" resultType="Integer">
SELECT COUNT(*)
FROM TBL
<where>
  <if test="search.state != null and search.state != ''">
    A.STATE = #{search.state, jdbcType=CHAR}
  </if>
  <if test="search.title != null and search.title != ''">
    AND A.TITLE = #{search.title, jdbcType=CHAR}
  </if>
</where>

WHEREと記述する必要はありません。内包するタグのどれかが結果を返す時のみWHEREを付加してくれます。

アノテーションでwhereを記述する

@Select,@Insert,@Update,@Deleteアノテーションなどの中でwhereを記述したいときはscriptタグを入れたら記述することが可能です。

Spring + MyBatisの@Insertアノテーションでバルクインサートする方法」参照ください。

コメント

タイトルとURLをコピーしました