情况1、#{}引用参数后,外面多加了双引号“”
举例,my.xml接口sql实现如下:
<select id="selectByEndWord" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from idiom
where
pinyin = "#{endKey}"
and
name not in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</select>
上面代码中判断语句pinyin = #{endKey}错写成了pinyin = "#{endKey}"
,就会报该问题。
情况2、模糊查询拼接出错
还是使用上面的代码,这次是根据pinyin字段进行模糊查询,如下:
<select id="selectByEndWord" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from idiom
where
pinyin like "#{endKey}%"
and
name not in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</select>
正确写法是把上面模糊查询pinyin like “#{endKey}%”, 修改为pinyin like "${endKey}%"
情况3、sql中存在注释代码
如下:
<select id="selectByEndWord" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from idiom
where
-- pinyin like "#{endKey}%"
pinyin like "${endKey}%"
and
name not in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</select>
上面代码有一行多余注释代码
-- pinyin like "#{endKey}%"
也会导致mybatis索引解析出错。
这个错误比较恶心,共勉、避坑。