Mybatis 自定义批量更新

分享知识 传递快乐

因为项目需要,需要根据账号对数据批量更新,而传统的批量更新是根据id更新,一条一条更新又比较耗时,下面记录一下对其它字段批量更新的操作。

由于环境使用的Mybatis,在此以mybatis批量更新做示。

1、通过拼接SQL语句进行批量更新

接口定义

int batchUpdate1(List<StudentEntity> list);

XML文件

<!-- 通过接收传进来的参数list进行循环着组装sql -->

<update id="batchUpdate1" parameterType="list">

    <!-- 接收list参数,循环着组装sql语句;注意for循环的写法 separator=";" 代表着每次循环完,在sql后面放一个分号 item="cus" 循环List的每条的结果集 collection="list" list 即为 map传过来的参数key -->

    <foreach collection="list" separator=";" item="stu">
        update student set
        age = #{stu.age},
        sex = #{stu.sex}
        where name = #{stu.name}
    </foreach>

</update>

在数据库连接配置中必须添加 &allowMultiQueries=true

如:

jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai

 &allowMultiQueries=true 意为 允许批量更新,否则在执行SQL时会报以下异常:

Error updating database.  Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'update student set
            age = 2,
            sex = '2021-07-24 08:36:48'

报错原因:

是因为spring boot配置文件中,关于数据库的连接配置,并没有允许进行批量更新的操作。

这种方式就是通过SQL拼接,单条单条的进行更新,如果行数多的情况下给不建议使用。

2、通过case when语句进行批量更新

接口定义

int batchUpdate2(List<BigDataSearchEntity> list);

XML文件

<update id="batchUpdate2" parameterType="list">
    update student
    <trim prefix="set" suffixOverrides=",">
        <trim prefix="age=case" suffix="end,">
            <foreach collection="list" item="stu" index="index">
                <if test="stu.age != null">
                    when name = #{stu.name} then #{stu.age}
                </if>
            </foreach>
        </trim>
        <trim prefix="name=case" suffix="end,">
            <foreach collection="list" item="stu" index="index">
                <if test="stu.sex != null">
                    when id=#{stu.name} then #{stu.sex}
                </if>
            </foreach>
        </trim>
    </trim>
    where
    <foreach collection="list" separator="or" item="stu" index="index">
        name = #{stu.name}
    </foreach>
</update>

通过case when语句进行批量更新,只要一条SQL语句:

update big_data_search set age=case when name='1428236138892185666' then 52 when name='1428236138892185665' then 42 end 
WHERE name = '1428236138892185666' or name = '1428236138892185665';

上面的方式是针对多个字段的情况;如果只是更新单个字段,可以这么写:

<!-- 批量更新第二种方法,针对单个字段进行批量更新 -->
<update id="batchUpdate2" parameterType="list">
    UPDATE big_data_search
    SET name = CASE
    <foreach collection="list" item="bd" index="index">
        when name=#{bd.name} then #{bd.age}
    </foreach>
    END
    WHERE name IN
    <foreach collection="list" index="index" item="bd" open="(" separator="," close=")">
        #{bd.name}
    </foreach>
</update>

—————————
如有不足请留言指正
相互学习,共同进步

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旷野历程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值