Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for an empty string in MyBatis?

Tags:

mybatis

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>
like image 426
aryanRaj_kary Avatar asked Mar 15 '13 15:03

aryanRaj_kary


People also ask

Does MyBatis return null?

@МаксимРыбалкин MyBatis does not return null with that mapper. There must be something else (a plugin, for example) changing the result. Try setting a breakpoint on org.

How do I set null value in MyBatis?

MyBatis uses #{propertyName} to define a property. If you use the name 'NULL' it ever looks for getNull() and setNull(...) or named parameter or map. However if the value is ever null in your case, you can omit the value, just if you have no default value for that column in the database. thanks...

What is resultMap in MyBatis?

The resultMap element is the most important and powerful element in MyBatis. It's what allows you to do away with 90% of the code that JDBC requires to retrieve data from ResultSet s, and in some cases allows you to do things that JDBC does not even support.

What is TypeHandler in MyBatis?

typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.


1 Answers

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>
like image 116
partlov Avatar answered Sep 18 '22 13:09

partlov