mybatis 批量更新数据 mysql

硅谷探秘者 4083 0 1

mybatis 批量更新数据 mysql 

方式1

简单粗暴,写一个更新的方法,循环调用就是了,但是效率就比较低了。性能较差。


方式2

批处理,类似于:

UPDATE stu  SET name='jia' ,score=213 WHERE id =1;
UPDATE stu  SET name='jia2' ,score=456 WHERE id =2;
UPDATE stu  SET name='jia3',score=222 WHERE id =3

xml文件这样写

<update id="test">
        <foreach separator=";" collection="list" item="s" index="index">
            update stu
            <set>
                name=#{s.name},score=#{s.score}
            </set>
            where id =#{s.id}
        </foreach>
    </update>

打印的sql像这样

==>  Preparing: update stu SET name=?,score=? where id =? ; update stu SET name=?,score=? where id =? ; update stu SET name=?,score=? where id =? 
==> Parameters: jias1(String), 123(Integer), 1(Integer), jias2(String), 123(Integer), 2(Integer), jias3(String), 123(Integer), 3(Integer)
<==    Updates: 1

        但Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行。所以需要在连接mysql的url上加 &allowMultiQueries=true 这个才可以执行。


方式3

mysql没有提供批量更新的方式,但是可以通过一些技巧实现,例如

UPDATE stu SET
    name = CASE id
        WHEN 1 THEN 'jiajia'
        WHEN 2 THEN 'wanghusai'
        WHEN 3 THEN 'wangha'
    END,
    score = CASE id
        WHEN 1 THEN 12.3
        WHEN 2 THEN 52.2
        WHEN 3 THEN 33.2
    END
WHERE id IN (1,2,3)

xml配置文件可以这样实现

     <update id="test2">
         update stu
         set name=
         <foreach collection="list" item="item" index="index" separator=" " open="case id" close="end">
		       when #{item.id} then #{item.name}
		 </foreach>
		 ,score=
         <foreach collection="list" item="item" index="index" separator=" " open="case id" close="end">
		       when #{item.id} then #{item.score}
		 </foreach>
         where id in 
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
	        #{item.id}
	    </foreach>
    </update>

打印的sql语句就是这样:

==>  Preparing: update stu set name= case id when ? then ? when ? then ? when ? then ? end ,score= case id when ? then ? when ? then ? when ? then ? end where id in ( ? , ? , ? ) 
==> Parameters: 1(Integer), jias1(String), 2(Integer), jias2(String), 3(Integer), jias3(String), 1(Integer), 123(Integer), 2(Integer), 123(Integer), 3(Integer), 123(Integer), 1(Integer), 2(Integer), 3(Integer)
<==    Updates: 3


评论区
请写下您的评论...
暂无评论...
猜你喜欢
数据库 2450 mysql生成不同的uuidsql语句:UPDATEusersetuuid=UUID();去掉中间的-UPDATEusersetuuid=REPLACE(uuid,"-","");注意这两个
weblog 855 mybatis模糊查询(mysql)接口方法:intcount(@Param("name")Stringname);配置文件: selectid="count"resultType="int
框架 1031 mybatis遍历int组foreachcollection="ids"index="index"item="item" open="("separator=","close=")" #{item}/foreach
框架 1445 询到的可能是一级缓存的不同的sqlSession使用同一个mapper时查询,查询到的可能是另一个sqlSession做相同操作留下的缓存如果你配置了二级缓存,那么查询的顺序应该为:二级
java项目 1394 springboot+mybatis配置多源并利用aop实现自动切换(demo)
框架 1662 springboot+mybatis配置多源并利用aop实现自动切换1.项目大致结构2.pom依赖dependencygroupIdorg.springframework.boot
数据库 1199 mysql库的变化,二进制日志包含所有或者潜在(如没有匹配到任何行的delete语句),语句以时间的形式保存,描述了改。二进制日志还包含执行每个库语句的时间信息
框架 4898 解决mybatis返回Map当字段为空时没有属性1.修改mybatis配置文件mybatis:configuration:call-setters-on-nulls:true2.库中:3.没有修
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。