The usage of <![CDATA[]]> in mybatis

table of Contents

1. Introduction

2. Matters needing attention

Three, use examples


1. <![CDATA[]]> Introduction

In the xml mapping file of mybatis, if there are some special characters in the sql written, 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.

<![CDATA[ ]]> What is it, this is XML syntax. All content inside CDATA will be ignored by the parser.

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

2. Matters needing attention

Note: <if test=""> </if> <where> </where> <choose> </choose> <trim> </trim> etc. These tags will not be in <![CDATA[ ]]> Is parsed,

So we only put statements with special characters outside <![CDATA[ ]]>, and try to narrow the scope of <![CDATA[ ]]>.

Three, use examples

<select id="getAllInfoList" parameterType="java.util.HashMap" resultMap="staffInfo">
<![CDATA[
SELECT name,info,create_date FROM staff_info WHERE 1=1 AND create_date> #{startTime} AND create_date <= #{endTime}
]]>
<if test="name!=''">
AND name=#{staffName}
</if>
</select>
Because there are ">" "<=" special characters so use< ![CDATA[ ]]> to comment, but there is an <if> tag, so put the <if> outside

Guess you like

Origin blog.csdn.net/Beyond_Xuz/article/details/109645368