sql中判断in常用到<foreach></foreach>,可以比较方便的进行拼接。 注意需要考虑集合元素的个数,以免出现sql的异常问题。

举例如下:

-- mapper调用
-- void clearItemNotBelongTo(@Param("belong") String belong, @Param("idSet") Set<String> idSet);
--
<select id="clearItemNotBelongTo">
    DELETE
    FROM
        datav_screen_item_template AS tep
    WHERE
        tep.belong_id = #{belong}
    <if test="idSet !=null and idSet.size !=0">
        AND tep.ID NOT IN
        <foreach collection="idSet" item="id" index="index"
                    open="(" close=")" separator=",">
            #{id}
        </foreach>
    </if>
</select>

参考: https://blog.csdn.net/u011781521/article/details/79669180