ibatis配置xml檔案中CDATA的用法

來源:互聯網
上載者:User
         ibatis作為一種半自動化的OR Mapping工具,其靈活性日益體現出來,越來越多的人都傾向於在項目中使用。由於Sql中經常有與xml規範相衝突的字元對xml對應檔的合法性造成影響。許多人都知道使用<![CDATA[   ]]>標記來避免衝突,但是在sql配置中有動態語句的時候,還是有一些細節需要特別注意的。         在使用ibatis時,經常需要配置待執行的sql語句。使用過ibatis的朋友都知道,無可避免的都會碰到一些不相容、衝突的字元,多數人也都知道用<![CDATA[   ]]>標記避免Sql中與xml規範相衝突的字元對xml對應檔的合法性造成影響。但是,如果在ibatis中使用了動態語句的時候,還是有一些細節需要注意。下面舉例說明一下: 環境:oracle、ibatis、java 錯誤例1:符號“<=”會對xml對應檔的合法性造成影響 
<select id="find" parameterClass="java.util.Map" resultClass="java.lang.Long"><![CDATA[   select idfrom tableA a,     tableB b <dynamic prepend="WHERE"> <isNotNull prepend="AND" property="startDate">  a.act_time >= #startDate#   and a.act_time <= #endDate#  and a.id = b.id  </isNotNull>     </dynamic>    ]]></select>


錯誤例2:將整個sql語句用<![CDATA[   ]]>標記來避免衝突,在一般情況下都是可行的,但是由於該sql配置中有動態語句(where部分),將導致系統無法識別動態判斷部分,導致整個sql語句非法。
<select id="find" parameterClass="java.util.Map" resultClass="java.lang.Long">  select idfrom tableA a,     tableB b <dynamic prepend="WHERE"> <isNotNull prepend="AND" property="startDate">  a.act_time >= #startDate#   and a.act_time <= #endDate#  and a.id = b.id  </isNotNull>     </dynamic>  </select>


正確做法:縮小範圍,只對有字元衝突部分進行合法性調整。

<select id="find" parameterClass="java.util.Map" resultClass="java.lang.Long">  select idfrom tableA a,     tableB b <dynamic prepend="WHERE"> <isNotNull prepend="AND" property="startDate">  a.act_time >= #startDate#   <![CDATA[ and a.act_time <= #endDate#  ]]>  and a.id = b.id  </isNotNull>     </dynamic>  </select>


總結: 在用CDATA的時候,只用把可能產生衝突的那段程式碼封裝含就可以了,不要盲目的包含。


參考自:http://hi.baidu.com/fwy434822/blog/item/614417f33a074bc00b46e047.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.