CDATA Usage in the Ibatis configuration XML file

Source: Internet
Author: User
Tags cdata
Ibatis as a semi-automatic or mapping tool, its flexibility is increasingly manifested and more and more people are inclined to use it in projects. Because characters that conflict with the XML specification often have an impact on the legality of XML mapping files in SQL. Many people are aware of the use of <!         [cdata[]]> tags to avoid collisions, but there are some details that need special attention when there are dynamic statements in the SQL configuration.) When using Ibatis, you often need to configure the SQL statements to be executed. Friends who have used Ibatis know that the inevitable will encounter some incompatible, conflicting characters, most people also know that the use of <! The [cdata[]]> tag avoids the effect of characters in SQL that conflict with XML specifications on the legality of XML mapping files. However, if dynamic statements are used in Ibatis, there are some details to be noted. Here's an example: Environment: Oracle, Ibatis, java error Example 1: the symbol "<=" will affect the legality of the XML map file
<select id= "Find" parameterclass= "Java.util.Map" resultclass= "Java.lang.Long" >
<![ cdata[   
Select id
from 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< c9/></isnotnull>    
 </dynamic>  
  ]]>
</select>


Error Example 2: Use the entire SQL statement with <! [cdata[]]> tag to avoid conflicts is generally feasible, but because of the dynamic statement (where part) in the SQL configuration, the system will not recognize the dynamic judgment part, causing the entire SQL statement to be illegal.
<select id= "Find" parameterclass= "Java.util.Map" resultclass= "Java.lang.Long" >  
select IDs from
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>


Correct approach: Narrow the scope, only to have the character conflict part of the legitimacy adjustment.

<select id= "Find" parameterclass= "Java.util.Map" resultclass= "Java.lang.Long" >  
select IDs from
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>


Summary: When using CDATA, just include the code that can produce a conflict, and don't blindly include it.


Reference from: http://hi.baidu.com/fwy434822/blog/item/614417f33a074bc00b46e047.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.