MyBatis 在xml文件中處理大於號小於號的方法

第一種方法:用轉義字符(注:對大小寫敏感! )java

用了轉義字符把>和<替換掉,而後就沒有問題了。sql

SELECT * FROM test WHERE 1 = 1 AND start_date  &lt;= CURRENT_DATE AND end_date &gt;= CURRENT_DATEspa

附:XML轉義字符  code

顯示結果 描述 實體名稱 實體編號
  空格 &nbsp; &#160;
< 小於號 &lt; &#60;
> 大於號 &gt; &#62;
& 和號 &amp; &#38;
" 引號 &quot; &#34;
' 撇號  &apos; (IE不支持) &#39;

第二種方法:<![CDATA[ ]]>xml

由於這個是xml格式的,因此不容許出現相似「>」這樣的字符,可是均可以使用<![CDATA[ ]]>符號進行說明,將此類符號不進行解析 ,以下:get

<update id="reduceNumber">
    UPDATE seckill
    SET number = number-1
    WHERE seckill_id=#{seckillId}
    AND start_time <![CDATA[ <= ]]> #{killTime} 
    AND end_time >= #{killTime}
    AND number > 0;
</update>

注意點:有動態語句(where,if)的狀況,where,if 條件不能放在<![CDATA[ ]]>中,不然將致使沒法識別動態判斷部分,致使整個sql語句非法.應該縮小範圍,只對有字符衝突部分進行合法性調整table

錯誤的形式:如下where 放在<![CDATA[ ]]>中class

<select id="getAccountErrorCount" resultType="int" parameterType="map">  
    <![CDATA[ 
    select count(*) from t_acctreg_accounterror 
    <where> 
        <if test="enddate != null and enddate != ''"> 
            createdate <= #{enddate} 
        </if> 
        <if test="acctno != null and acctno != ''"> 
            AND acctno LIKE '%'#{acctno}'%' 
        </if> 
    </where> 
    ]]>  
</select>  

正確的形式:test

<select id="getAccountErrorCount" resultType="int" parameterType="map">  
    select count(*) from t_acctreg_accounterror  
    <where>  
        <if test="enddate != null and enddate != ''">  
            <![CDATA[createdate <= #{enddate}]]>  
        </if>  
        <if test="acctno != null and acctno != ''">  
            <![CDATA[AND acctno LIKE CONCAT('%',#{acctno},'%')]]>  
        </if>  
    </where>  
</select>

補充說明:like的以上兩種寫法,都是能夠的date

LIKE '%'#{acctno}'%'

LIKE CONCAT('%',#{acctno},'%')

相關文章
相關標籤/搜索