Solution to the problem that in in dynamic sql exceeds the upper limit of 999

Solutions

1. First divide the data into a list set that does not exceed the upper limit of 999 in the service layer, and then pass it to the dao layer for query

2. Process directly in sql

I tried the first method and found it troublesome, so here is the second method, which only needs to be processed in dynamic sql.

dynamic sql

   List<AccountsPO> findXXXByOrgIds(@Param("Ids") List<String> Ids);
   <select id="findXXXByIds"
            resultType="com.XXX.XXX.AccountPO">
        select t.* from table t
        <where>
<!--注意:一定要判断Ids.size>0 否则若list为长度为空的 数组 则会失败 -->
            <if test="Ids != null and Ids.size>0">
                (t.id in
                <foreach collection="Ids" item="item" open="("  close=")" index="index">
                    <if test="index != 0">
                        <choose>
                            <when test="index % 1000 == 999">) OR t.id IN(</when>
                            <otherwise>,</otherwise>
                        </choose>
                    </if>
                    #{item}
                </foreach>)
            </if>
        </where>
    </select>

The final splicing effect is

 select t.* from table t where t.id in (0,1,2,...998) or t.id in (999,1000,...)

Guess you like

Origin blog.csdn.net/qq_44716086/article/details/126567776