MyBatis mapper.xml中SQL处理小于号与大于号
这种问题在xml处理sql的程序中经常需要我们来进行特殊处理。
方法一:使用特殊转义字符
其实很简单,我们只需作如下替换即可避免上述的错误:
| < | <= | > | >= | & | ' | " |
|
< |
<= |
> |
>= |
& |
' |
" |
例如常见的时间比较:
错误写法
<select id="select" parameterType="xxx" resultMap="xxx">
select
distinct
<include refid="Base_Column_List" />
from xxx
<where>
<if test="createDate != null">
create_date <= #{createDate}
</if>
</where>
</select>
正确写法
<select id="select" parameterType="xxx" resultMap="xxx">
select
distinct
<include refid="Base_Column_List" />
from xxx
<where>
<if test="createDate != null">
create_date <= #{createDate}
</if>
</where>
</select>
方法二:使用<![CDATA[ ]]>符号
这里面的内容将不被解析
<if test="beginTime!=null">
AND DATE (os.show_start_time) >= DATE(#{beginTime})
</if>
<if test="endTime!=null">
AND DATE (os.show_start_time) <![CDATA[<=]]> DATE(#{endTime})
</if>

浙公网安备 33010602011771号