Mybatis的sql是寫在xml映射文件中的,如果sql中有一些特殊的字符,在解析xml文件的時候會被轉義,使用<![CDATA[ ]]>
就可以讓這些特殊字符不被轉義。
<![CDATA[ ]]>
是xml的語法,放在CDATA[]內部的特殊字符都會被解析器忽略,所以在我們使用<if test="">
、</if>
、<where
>、</where>
、<choose>
、</choose>
、<trim>
、</trim>
等標簽實現動態sql時,我們需要把sql語句中出現的<、<=、&等特殊符號都放在CDATA[]的內部
下面是實例:
<update id="delData" parameterType="com.hx.pojo.BrowsingHistory">
update td_browsing_history set status = '9'
<where>
browsing_user_id = #{browsingUserId}
<if test="startTime != null ">
and browsing_time <![CDATA[ >= ]]> #{startTime}
</if>
<if test="endTime != null ">
and browsing_time <![CDATA[ <= ]]> #{endTime}
</if>
<if test="id != null and id != ''">
and ID = #{id}
</if>
</where>
</update>