在Mybatis中,sql語句的 大於號(>),小於號(<),在xml文件中是特殊字符(< >),所以需要做轉義。
使用xml的轉義字符方法:<![CDATA[ 特殊字符 ]]>。
示例:
<select id="getListCount" resultType="int" parameterType="java.util.HashMap">
SELECT * FROM V_gsuserbanding AS tm WHERE 1=1
<if test="UserName !=null and UserName !=''">
and UserName like "%"#{UserName}"%"
</if>
<if test="BeginTime !=null and BeginTime !=''">
and CreateTime <![CDATA[ >= ]]> #{BeginTime} -- 轉義 >=
</if>
<if test="EndTime !=null and EndTime !=''">
and CreateTime <![CDATA[ <= ]]> #{EndTime} -- 轉義 <=
</if>
</select>