如何在MyBatis的动态SQL中检查空字符串?我在文档中找到了下面的代码,但我想检查空字符串,而不是null。
<select id="findActiveBlogWithTitleLike" parameterType="Blog" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>
发布于 2013-03-16 03:02:25
在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>
发布于 2016-12-24 16:35:03
英语说得不好。谢谢你的病人。
它的函数是xml文件。
<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>
它是映射器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>
不如想想这篇小贴士。
https://stackoverflow.com/questions/15436712
复制相似问题