最近在写项目的时候,碰到了一个巨坑,idea控制台一直报错说sql语法错误,我检查了好几多,sql语句并没有错误,后来在网上搜索资料才明白,这是foerach标签的一个坑,具体如下
SELECT * FROM table where id in
<foreach item="item" collection="ids1" open="(" close=")" index="0" separator=",">
#{item}
</foreach>
此时这样写可以看出sql语句是没有任何问题的,但是等到前端发起请求到后台运行,打印sql语句的时候会报出sql语法错误,这是因为在 <foreach>标签中进行了换行,正确的写法应该如下
SELECT * FROM table where id in
<foreach item="item" collection="ids1" open="(" close=")" index="0" separator=",">#{item}</foreach>
改为一行后,sql语句就不会报错,亲测有效