mybatis 的

时间:2023-03-09 20:27:53
mybatis 的<![CDATA[ ]]>

示例:

xml文件:

<!-- 获取条数 -->
<select id="getCount" parameterType="Map" resultType="long">
select count(*) from orders o,user u,linkman l where o.flag=1
and o.uid=u.uid and l.lid=o.uid
<if test="fdate!=null">
<![CDATA[and fdate <= #{fdate}]]>
</if>
</select>

这样做的目的主要是因为在 XML 元素中,"<" 和 "&" 是非法的。"<" 会产生错误,因为解析器会把该字符解释为新元素的开始。"&" 也会产生错误,因为解析器会把该字符解释为字符实体的开始。而sql语句或者脚本语句中可能会存在 "<" 或 "&" 字符。为了避免错误,可以将sql语句定义为 CDATA。CDATA 部分中的所有内容都会被解析器忽略。