How to check empty string in MyBatis?

How to check empty string in dynamic SQL MyBatis? I found the code below in documentaiton, but I want to check for an 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> 
+7
source share
2 answers

In MyBatis, you can use != '' To compare with an empty string, so your request 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> 
+11
source

Do not speak English well. Thanks for your patient.

It works with xml file.

 <mapper namespace="org.jacknie.mybatis.Functions"> <sql id="isBlank"> <bind name="isBlank" value=":[@ org.apache.commons.lang3.StringUtils@isBlank (#this)]" /> </sql> <sql id="sysout"> <bind name="sysout" value=":[@ System@out.println (#this)]" /> </sql> </mapper> 

It displays an XML file.

 <mapper namespace="org.jacknie.test.TestMapper"> <select id="selectTest" resultType="_int"> <include refid="org.jacknie.mybatis.Functions.isBlank" /> <include refid="org.jacknie.mybatis.Functions.sysout" /> SELECT '1' FROM DUAL <if test="#fn = isBlank, not(#fn(map.name))"> <bind name="forLogging" value="#fn = sysout, #fn('Hello' + map.name)" /> </if> </select> </mapper> 

How about the thought of this advice ...

enter the link here

0
source

All Articles