當我們需要通過xml格式處理sql語句時,經常會用到< ,<=,>,>=等符號,但是很容易引起xml格式的錯誤,這樣會導致后台將xml字符串轉換為xml文檔時報錯,從而導致程序錯誤。
【mybatis可以使用】
<![CDATA[
SQL代碼
]]>
例如:
<select id="getListByElementName" resultType="java.lang.String">
<![CDATA[
select a1.xml_blob as xmlBlob
from (select t.xml_blob, rownum rn
from (select *
from aj_i_transmission j
order by instr('I,RC,D', j.transactioncode)) t
where 1 = 1
and t.object_gid is not null
and t.transactioncode is not null
and t.status = 'E'
and t.insert_date < #{date}
and t.create_date > sysdate - 3
and t.element_name = #{elementName}
and rownum <= #{end}
) a1
where rn > #{start}
]]>
</select>
這樣的問題在iBatiS中或者自定義的xml處理sql的程序中經常需要我們來處理。其實很簡單,我們只需作如下替換即可避免上述的錯誤:
原符號 < <= > >= & ' "
替換符號< <= > >= & ' "
錯誤的xml格式:
<?xml version="1.0" encoding="GBK"?>
<queryForm token="32sdfj-349sfdnfs32-fsdf348imfg323-df34" tableName="HSY_T_CUSTOMER" pageSize="20" pageNo="1" orderColumn="" orderType="">
<sqlCondition> AND START_DATE >= to_date('2013-01-01','yyyy-MM-dd') AND START_DATE <= to_date('2013-01-30','yyyy-MM-dd') </sqlCondition>
</queryForm>
在altova XMLSpy工具中會報錯,xml不符合格式要求。
正確的xml格式:
<?xml version="1.0" encoding="GBK"?>
<queryForm token="32sdfj-349sfdnfs32-fsdf348imfg323-df34" tableName="HSY_T_CUSTOMER" pageSize="20" pageNo="1" orderColumn="" orderType="">
<sqlCondition> AND START_DATE >= to_date('2013-01-01','yyyy-MM-dd') AND START_DATE <= to_date('2013-01-30','yyyy-MM-dd') </sqlCondition>
</queryForm>
在altova XMLSpy工具中驗證通過。
T