mybatis ![CDATA[ ]] 使用避免特殊字符

川长思鸟来 2022-08-29 15:48 242阅读 0赞

一 概述

在Mybatis的xml文件中编写SQL的时候经常会使用类似:< > =等特殊字符,但是在解析xml文件的时候它们会被转义,当我们不希望出现这种情况的时候,我们可以通过加上防止转义的内容:<![CDATA[ ]]>.

上述为xml的语法,在<![CDATA[ ]]>中的内容都会被xml解析器所忽略,此时如果我们将 等标签语言方法其中时,这些标签会失去本身的功能,所以我们需要明确<![CDATA[ ]]>的作用范围,将那些不需要转义的语句或者字符放入其中。

二 特殊符号转义与转义对比






















< <= > >= &
&lt; &lt;= &gt; &gt;= &amp; &apos; &quot;

<![CDATA[ ]]>使用**示例**

  1. <if test="startTime != null and startTime.length > 0">
  2. and record.create_time <![CDATA[>=]]> #{startTime}
  3. </if>
  4. <if test="endTime != null and endTime.length > 0">
  5. and record.create_time <![CDATA[<=]]> #{endTime}
  6. </if>
  7. <if test="distributionStartTime != null and distributionStartTime.length > 0">
  8. and crecord.create_time <![CDATA[>=]]> #{distributionStartTime}
  9. </if>
  10. <if test="distributionEndTime != null and distributionEndTime.length > 0">
  11. and crecord.create_time <![CDATA[<=]]> #{distributionEndTime}
  12. </if>
  13. <if test="shouldPayStartTime != null and shouldPayStartTime.length > 0">
  14. and orderinfo.due_time <![CDATA[>=]]> #{shouldPayStartTime}
  15. </if>
  16. <if test="shouldPayEndTime != null and shouldPayEndTime.length > 0">
  17. and orderinfo.due_time <![CDATA[<=]]> #{shouldPayEndTime}
  18. </if>

发表评论

表情:
评论列表 (有 0 条评论,242人围观)

还没有评论,来说两句吧...

相关阅读