问题描述
提示:mapper.xml文件的错误Unknown column '2' in 'where clause'
运行如下代码是报错,报错的SQL语句,复制到Navicat中运行,结果正常,但在springboot+mybatis-plus项目中却报如上错误
<select id="selectFileInfoListByOrg" parameterType="com.jhunicom.project.country.domain.FileInfo" resultMap="FileInfoResult">
select f.id as id,f.`name` as `name`,f.file_path as file_path,f.url as url,f.user_id as user_id,f.user_name as user_name,f.update_by as update_by,f.update_time as update_time,f.create_time as create_time
from file_info f
RIGHT JOIN sys_user su
ON f.user_id = su.user_id
<where>
su.dept_id in
<foreach item="orgID" index="index" collection="orgIDList" open="(" separator="," close=")">
#{orgID}
</foreach>
</where>
</select>
解决方案:
需要在<forearch>\标签之前添加<if>判断即可
<select id="selectFileInfoListByOrg" parameterType="com.jhunicom.project.country.domain.FileInfo" resultMap="FileInfoResult">
select f.id as id,f.`name` as `name`,f.file_path as file_path,f.url as url,f.user_id as user_id,f.user_name as user_name,f.update_by as update_by,f.update_time as update_time,f.create_time as create_time
from file_info f
RIGHT JOIN sys_user su
ON f.user_id = su.user_id
<where>
<if test="orgIDList != null">
su.dept_id in
<foreach item="orgID" index="index" collection="orgIDList" open="(" separator="," close=")">
#{orgID}
</foreach>
</if>
</where>
</select>
补充
后续发现把代码还原为最初的没有if判断的代码后,仍然可以用。。。有点奇怪