How to check for an empty string in MyBatis?

aryanRaj_kary picture aryanRaj_kary · Mar 15, 2013 · Viewed 40.4k times · Source

How do I check for an empty string in the dynamic SQL of MyBatis? I find the code below in the documentaiton, but I want to check for empty string, not null.

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

Answer

partlov picture partlov · Mar 15, 2013

In MyBatis you can use != '' to compare with empty string, so in your query it would be something like:

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