在使用mybatis 时我们sql是写在xml 映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用<![CDATA[ ]]>来解决。
<![CDATA[ ]]> 是什么,这是XML语法。在CDATA内部的所有内容都会被解析器忽略。
如果文本包含了很多的"<"字符 <=和"&"字符——就象程序代码一样,那么最好把他们都放到CDATA部件中。
但是有个问题那就是 <if test=""> </if> <where> </where> <choose> </choose> <trim> </trim> 等这些标签都不会被解析,所以我们只把有特殊字符的语句放在 <![CDATA[ ]]> 尽量缩小 <![CDATA[ ]]> 的范围。
实例如下:
select
<include refid="Base_Column_List" />
from vs_rebate_car_plan_exp_vch vch
<where> org_id = #{orgId,jdbcType=BIGINT}
<if test="createTimeBegin != null">
<![CDATA[
and create_time >= #{createTimeBegin,jdbcType=DATE}
]]>
</if>
<if test="createTimeEnd != null">
<![CDATA[
and create_time < #{createTimeEnd,jdbcType=DATE}
]]>
</if>
<if test="creatorName != null">
and creator_name = #{creatorName,jdbcType=VARCHAR}
</if>
<if test="doneFlag != null and doneFlag == oneFlag">
<![CDATA[
and failed_count <> #{zeroFlag,jdbcType=INTEGER}
]]>
</if>
<if test="doneFlag != null and doneFlag == zeroFlag">
<![CDATA[
and failed_count > #{zeroFlag,jdbcType=INTEGER}
]]>
</if>
</where>
</select>