如果in的参数过多,mybatis会报错 原来写法:
select * from file_inf a
<where>
and a.status ='0' and a.id in
<foreach collection="ids" open="(" close=")" separator="," item="id"
#{id}
</foreach>
</where
改进写法:
select * file_inf a
<where>
and a.status ='0' and (a.id in
<foreach collection="ids" open="(" close=")" separator="," item="id" index="index">
//只需在原基础上加一个if条件,最外面加上一对括号就可以了
<if test="index != 0 and index % 99 == 0">
#{id}) or a.id in (
</if>
#{id}
</foreach>
)
</where>
本质://只需在原基础上加一个if条件,最外面加上一对括号就可以了
原来没加if条件的情况,in超过99行:
and (a.id in (1,2,3,4,5...998,999,1000,1001...))
加上if条件后,每隔99个位置插入一个#{id}) or a.id in (分割成两个in:
and (a.id in (1,2,3,4,5...98,99) or a.id in (99,100,101...))