mybatis sql语句配置大于号小于号的处理(元素内容必须由格式正确的字符数据或标记组成)

原因 : Mapper.xml 文件里  < (小于)号 ,   >(大于)号 ,成对出现会被认为是括号,需要额外注意

 

错误示例:

select * from XXX
        where "DELETE" = 1
        <if test="id != null">
            and ID = #{id}
        </if>
        <if test="userIds != null and userIds.size()!=0">
            and USER_ID in
            <foreach item="item" index="index" collection="userIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime!=null">
            and CREATE_TIME >=#{startTime}

        </if>
        <if test="endTime!=null">
            and CREATE_TIME <= #{endTime} 
        </if>

 

解决:

1,将 < 号换成  &lt;     > 号 换成&gt; 

 

2,使用<![CDATA[  ]]> 将sql包裹

 

正确示例:

select * from XXX
        where "DELETE" = 1
        <if test="id != null">
            and ID = #{id}
        </if>
        <if test="userIds != null and userIds.size()!=0">
            and USER_ID in
            <foreach item="item" index="index" collection="userIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime!=null">
            and CREATE_TIME >=#{startTime}

        </if>
        <if test="endTime!=null">
            <![CDATA[ and CREATE_TIME <= #{endTime} ]]>
        </if>

 

posted @ 2021-01-07 14:09  风子磊  阅读(384)  评论(1编辑  收藏  举报