mybatis批量删除操作

1.复杂条件批量删除

建议增加联合索引

<!--通过条件删除数据-->
<delete id="batchDeleteByCondition">
    delete from test where
    <foreach collection="list" item="item" separator=" or " index="index">
        (name = #{item.name} and code= #{item.code} and type = #{item.type})
    </foreach>
</delete>

2. 复杂条件批量删除 IN操作

<delete id="deleteBatch" parameterType="java.util.List">
    delete from test
    where (NAME, CODE) in
    <foreach item="item" index="index" collection="list" separator="," open="(" close=")">
(#{item.name},#{item.code})
	</foreach>
</delete>

3. 批量删除常规

<delete id="batchDelete">
        delete from test where id in (
        <foreach collection="list" item="id" separator=",">
            #{id}
        </foreach>
        )  
 </delete>

4.Oracle IN超过1000删除

//方案1:
	DELETE FROM test
    WHERE id IN
        <!-- 处理in的集合超过1000条时Oracle不支持的情况 -->
        <trim suffixOverrides=" OR id IN()">
            <foreach collection="list" item="item" index="index" open="(" close=")">
                <if test="index != 0">
                    <choose>
                        <when test="index % 1000 == 999">) OR id IN (</when>
                        <otherwise>,</otherwise>
                    </choose>
                </if>
                #{item}
            </foreach>
        </trim>

方案2:
 		DELETE
        FROM test
        WHERE
        org_id IN
        <foreach collection="list" item="item" index="index" open="(" close=")">
            <choose>
                <!--第一项-->
                <when test="index == 0">#{item}</when>
                <!--OR语句中的第一项-->
                <when test="index != 0 and index % 500 == 0">) OR org_id IN ( #{item}</when>
                <!--非第一项-->
                <otherwise>,#{item}</otherwise>
            </choose>
        </foreach>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值