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>
@МаксимРыбалкин 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.
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...
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With