详情内容查看原文:http://www.ibloger.net/article/286.html
在XML映射SQL的文件中,很多情况下会使用到大于号、小于号等特殊符号,这时候如果不进行控制是无法编译通过的,这时候需要用到<![CDATA[ ]]>符号进行说明,将此类符号不进行解析,还有一种解决方法就是使用转移字符,如,案例2,其实,这个问题不止在MyBatis上通用,而是它通用于任何XML的文件中使用,比如Hibernate、Wabacus、Spring等等等等配置文件中,只要是XML文件就行,此类问题在以后的工作中,经常使用。
案例1代码
<select id="findAllKiaAnalysisByCondition" parameterType="map" resultType="KiaAnalysis">
select * from (select unitname, to_char(rdate,'yyyy-MM') rdate,keytype, scope from KIAANALYSIS
<where>
<if test='startDate!="%null%"'> and rdate >= to_date(#{startDate},'yyyy-mm')</if>
<if test='endDate!="%null%"'>
<![CDATA[ and rdate <= to_date(#{endDate},'yyyy-mm') ]]>
</if>
<if test='unitname!="%null%"'>and unitname=#{unitName}</if>
</where>
) pivot (sum(scope) for keytype in(${themes}))
</select>
案例2代码
<select id="findDyAnalysisByCondition" parameterType="map" resultType="DyAnalysis">
select rdate,keytype,${vl} from (select unitname,to_char(rdate,'yyyy-MM-dd') rdate,keytype,scope from kiaanalysis
<if test='themes!=null && themes!=""'>
where keytype=#{themes}
</if>
<if test='startDate!= null && startDate!=""'>
and rdate >= to_date(#{startDate},'yyyy-mm-dd')
</if>
) pivot (sum(scope) for unitname in(${sta_type}))
</select>
在ibatis配置文件写SQL语句的时候对于一些比如“<”,">","<>","&"," ' "," " "是不能够识别的,并且会抛异常。
一般可以如下改写:
< <
> >
<> <>
& &
' '
" "