Mybatis使用in操作时,超过1000个数据时会报错的解决方法

时间:2025-03-18 21:33:35

1. 问题描述

sql在执行in操作查询时,in()中的参数超过1000条,若不做处理,当in中的参数超过1000条会报错

2. 解决方案

可以使用or连接的方式。例如:select * from 表 where id in(1,2,3,4,…999) or id in (1000,1001,1002…)的方式去解决

3. 案例

mybatis注解版示例

    @Update({
            "<script>",
            "update table1",
            "set name=null",
            "where id in",
            "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>",
            "<if test = 'index%1000 == 999'>") or id in ( "</if>"
            "#{item}",
            "</foreach>",
            "</script>"
    })
    Integer batch(@Param("list") List<String> list);

4. 研究

sql中使用in的执行效率较差,是否有其他优化方案?