CDATA[ ] in mybatis

When using mybatis, our sql is written in the xml mapping file. If there are some special characters in the written sql, it will be escaped when parsing the xml file, but we do not want it to be escaped, so we have to use <![CDATA[ ]]> to solve.

What is <![CDATA[ ]]> , this is XML syntax. Everything inside CDATA is ignored by the parser.

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

But there is a problem that these tags such as <if test=""> </if> <where> </where> <choose> </choose> <trim> </trim> will not be parsed, so we only Put statements with special characters in <![CDATA[ ]]> and try to narrow the scope of <![CDATA[ ]]>.

Examples are as follows:

  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!=''">  
  6.    AND newsEdit=#{etidName}  
  7.   </if>  
  8.  </select>  
<select id="allUserInfo" parameterType="java.util.HashMap" resultMap="userInfo1">
  <![CDATA[
  SELECT newsEdit,newsId, newstitle FROM shoppingGuide  WHERE 1=1  AND  newsday > #{startTime} AND newsday <= #{endTime}
  ]]>
  <if test="etidName!=''">
   AND newsEdit=#{etidName}
  </if>
 </select>
Because there are ">" "<=" special characters, use <![CDATA[ ]]> to comment, but there are <if> tags, so put <if> etc.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325753576&siteId=291194637