在使用mapper.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>
在使用 MyBatis 的 mapper.xml 文件中编写 SQL 时,需要注意比较符号的转义。错误的写法可能导致语法错误,例如在时间比较中直接使用 < 符号会导致问题。正确的做法是将比较符号转义为 &lt;。本文详细介绍了如何正确转义 SQL 中的比较操作符,包括 <, >, ≤, ≥, & 和其他特殊字符。
1026





