2013-03-15 14 views
7

如何检查MyBatis的动态SQL中的空字符串?我在documentaiton中找到下面的代码,但我想检查空字符串,而不是null。如何检查MyBatis中的空字符串?

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

回答

11

在MyBatis的,你可以使用!= ''与空字符串比较,所以在您的查询会是这样的:

<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> 
+0

谢谢你,工作 – 2013-03-20 09:48:24

+0

欢迎您。 – partlov 2013-03-20 09:50:45

+0

这个怎么样? “title.length()> 0”。 – 2016-06-24 05:51:19

0

Dont't讲英语。感谢您的耐心。

这是函数的xml文件。

<mapper namespace="org.jacknie.mybatis.Functions"> 
    <sql id="isBlank"> 
    <bind name="isBlank" value=":[@[email protected](#this)]" /> 
    </sql> 
    <sql id="sysout"> 
    <bind name="sysout" value=":[@[email protected](#this)]" /> 
    </sql> 
</mapper> 

这是mapper xml文件。

<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> 

如何想,这提示...

enter link description here