CDATA in the MyBatis

Source: Internet
Author: User

When using MyBatis, our SQL is written in the XML mapping file, if there are some special characters in the SQL written, it will be escaped when parsing the XML file, but we do not want him to be escaped, so we have to use <! [cdata[]]> to solve.

<! [Cdata[]]> is what, this is the XML syntax. All content inside CDATA is ignored by the parser.

If the text contains a lot of "<" characters <= and "&" characters--like program code, it's best to put them all in a CDATA widget.

But there's a problem, that's <if test=. > </if> <where> </where> <choose> </choose> <trim> </trim> and so on these tags will not be parsed, so we only put the statements with special characters in the <! [cdata[]]> as narrow as possible <! [Range of cdata[]]>.

Examples are as follows:

[Java]View plain copy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >  print?
    1. <select id= "Alluserinfo"   Parametertype= "Java.util.HashMap"  resultmap= "UserInfo1" >   
    2.   <![ cdata[  
    3.   select newsedit,newsid, newstitle from  Shoppingguide  where 1=1  and   newsday > #{starttime} and newsday <= #{endtime}  
    4.   ]]>  
    5.   <if test = "etidname!=" ">&NBSP;&NBSP;
    6.    and newsedit =#{etidname}  
    7.   </IF>&NBSP;&NBSP;
    8.  </select>  

Because there are ">" "<=" special characters, so use <! [cdata[]]> to comment, but there are <if> tags, so put <if> wait outside

CDATA in the MyBatis

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.