Mybatis plus 批量操作

本文介绍了一种使用MyBatis批量更新数据库记录的方法。通过定义Mapper接口及XML文件中的动态SQL,实现了针对多个记录的特定字段进行批量更新的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【批量修改】

xxxMapper.java:

/**
 * 批量修改指定记录的属性字段
 *
 * @param updateVo 待修改属性字段
 * @param idList id集合
 * @return
 */
Integer batchUpdateFields(@Param("vo")UpdateVo updateVo, @Param("list")List<String> idList);

xxxMapper.xml:

<!-- 批量修改指定记录的属性字段 -->
    <update id="batchUpdateFields">
        update t_xxx
        <trim prefix="set" suffixOverrides=",">
            <if test="vo.status!= null">
                <trim prefix="status= case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id = #{item} then #{vo.status}
                    </foreach>
                </trim>
            </if>
            <if test="vo.updateTime != null">
                <trim prefix="update_time = case" suffix="end,">
                    <foreach collection="list" item="item">
                        when id = #{item} then #{vo.updateTime}
                    </foreach>
                </trim>
            </if>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

上述批量修改的sql实际执行:

update t_xxx
set 
status = case 
when id = ? then ? 
when id = ? then ? 
when id = ? then ? 
when id = ? then ? 
end, 
update_time = case 
when id = ? then ? 
when id = ? then ? 
when id = ? then ? 
when id = ? then ? 
end 
where id in ( ? , ? , ? , ? )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值