2013-03-15 13 views
7

Come posso verificare una stringa vuota nell'SQL dinamico di MyBatis? Trovo il codice qui sotto nel documentaiton, ma voglio verificare la stringa vuota, non nulla.Come verificare una stringa vuota in MyBatis?

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

risposta

11

In MyBatis è possibile utilizzare != '' da confrontare con stringa vuota, così nella query che sarebbe qualcosa di simile:

<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

ringraziamento, lavorando –

+0

Sei il benvenuto. – partlov

+0

come su questo? "title.length()> 0". –

0

Dont't parla bene l'inglese. Grazie per la tua pazienza.

È un file xml di funzioni.

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

È un file xml mapper.

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

ne dite che questo tip ...

enter link description here

Problemi correlati