如何在MyBatis中检查空字符串?

ary*_*ary 9 mybatis

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

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

par*_*lov 19

在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>
Run Code Online (Sandbox Code Playgroud)


Hye*_*ong 6

英语说得不好。谢谢你的耐心。

它是函数 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>
Run Code Online (Sandbox Code Playgroud)

它是映射器 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>
Run Code Online (Sandbox Code Playgroud)

想想这个技巧怎么样...

在此输入链接描述