Mybatis-IN语句包含大量值

方案1

<select id="xxx" resultType="java.lang.String">
    SELECT *
    FROM testTable t
    <where>
        1=1
        <!-- 加choose:防止外部传入空数据,获取了全量数据 -->
        <choose>
            <when test="ids!=null and ids.size()>0">
                AND (t.ID IN
                <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
                    <!-- 在遍历的过程中,每N个数据需要添加一个IN,并采用or连接 -->
                    <if test="(index % 999) == 998"> NULL ) OR t.ID IN (</if>#{item}
                </foreach>)
            </when>
            <otherwise>
                AND t.ID IN(NULL)
            </otherwise>
        </choose>
    </where>
</select>

最后的语句可能是这样:where 1=1 and (id in(...) or id in(...))
是否需要加视情况而定。

posted @ 2021-05-20 17:09  code汤  阅读(464)  评论(0)    收藏  举报