-
2022-05-12 12:26:17
之前的项目都是导入各种插件,各种框架,就比如mybatis那个逆向工程,你只需要链接数据库,然后就帮你把每张表的实体类,以及对应的mapper通通构建出来,即便只有单表操作的sql语句,但也极大简化了开发,但是对于初学者还是不建议使用的。今天自己手动实现了一下,在测试批量更新的功能时,你向list传入一天记录更新没有问题,但多条记录就报错。在这里困了好久,最后才知道,数据库默认将批量更新设置为false,这里需要我们手动打开,只需要在配置文件的url后面加上allowMultiQueries=true就可以了。这个东西还是得自己多动手敲才能发现问题,很多东西就是你以为你会了,实际上一动手才发现根本做不出来。
<!--批量修改--> <update id="updateBatch" parameterType="java.util.List"> <foreach collection="list" item="item" separator=";" index="index"> update manage <set> <trim suffixOverrides=","> <if test="item.userName != '' and item.userName != null"> userName=#{item.userName}, </if> <if test="item.password != '' and item.password != null"> password=#{item.password}, </if> <if test="item.realName != '' and item.realName != null"> realName=#{item.realName}, </if> </trim> </set> where 1=1 <if test="item.id != '' and item.id !=null"> and id=#{item.id} </if> </foreach> </update>
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/fruit_shop?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&allowMultiQueries=true jdbc.username=root jdbc.password=123456
更多相关内容 -
Mybatis批量更新报错问题
2020-08-31 12:59:26主要介绍了Mybatis批量更新报错的问题及解决办法,包括mybatis批量更新的两种方式,需要的的朋友参考下 -
Mybatis批量更新三种方式的实现
2020-08-26 08:45:52主要介绍了Mybatis批量更新三种方式的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 -
MyBatis批量更新
2022-03-03 15:00:01UPDATE tableName ...这句sql 的意思是,更新orderId 字段,如果id=1 则orderId 的值为3,如果id=2 则orderId 的值为4…… where部分不影响代码的执行,但是会提高sql执行的效率。确保sql语句仅执行需要修改的行UPDATE tableName SET orderId = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHERE id IN (1,2,3)
这句sql 的意思是,更新orderId 字段,如果id=1 则orderId 的值为3,如果id=2 则orderId 的值为4……
where部分不影响代码的执行,但是会提高sql执行的效率。确保sql语句仅执行需要修改的行数,这里只有3条数据进行更新,而where子句确保只有3行数据执行。如果更新多个值的话,只需要稍加修改:
多字段: UPDATE categories SET orderId = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END, title = CASE id WHEN 1 THEN 'New Title 1' WHEN 2 THEN 'New Title 2' WHEN 3 THEN 'New Title 3' END WHERE id IN (1,2,3)
单个字段修改:
<update id="BatchUpdate" parameterType="map"> update activity_gift_status <set> <trim prefix="ref_activity_type=case" suffix="end,"> <foreach collection="list" item="item" index="index"> <if test="item.refActivityType !=null"> when ref_activity_id=#{item.refActivityId} then #{item.refActivityType} </if> </foreach> </trim> </set> where activity_gift_status_id in <foreach collection="list" item="item" index="index" separator="," open="(" close=")"> #{item.activityGiftStatusId} </foreach> </update>
多条件修改:
<update id="updateByBatch" parameterType="list"> update message_notice_task <trim prefix="set" suffixOverrides=","> <trim prefix=" call_back_status = case" suffix="end,"> <foreach collection="list" item="item" index="index"> <if test="item.callBackStatus !=null"> when task_id = #{item.taskId} then #{item.callBackStatus} </if> </foreach> </trim> <trim prefix="reason = case" suffix="end,"> <foreach collection="list" item="item" index="index"> <if test="item.reason !=null"> when task_id = #{item.taskId} then #{item.reason} </if> </foreach> </trim> <trim prefix="reason_text = case" suffix="end,"> <foreach collection="list" item="item" index="index"> <if test="item.reasonText !=null"> when task_id = #{item.taskId} then #{item.reasonText} </if> </foreach> </trim> </trim> <where> AND task_id in <foreach collection="list" item="item" index="index" separator="," open="(" close=")"> #{item.taskId} </foreach> </where> </update>
-
mybatis批量更新
2021-01-05 18:32:18mybatis批量更新 1.常见的批量更新(生成多条执行语句) <update id="updateBatch" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=...mybatis批量更新
1.常见的批量更新(生成多条执行语句)
<update id="updateBatch" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update course <set> name=${item.name} </set> where id = ${item.id} </foreach> </update>
这种写法需要给数据库添加配置allowMultiQueries=true
2.一条语句执行批量更新操作
<update id="updateBatch" parameterType="java.util.List"> update mydata_table set status= <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end"> when #{item.id} then #{item.status} </foreach> where id in <foreach collection="list" index="index" item="item" separator="," open="(" close=")"> #{item.id,jdbcType=BIGINT} </foreach> </update>
生成的sql如下:
update mydata_table set status = --此处应该是<foreach>展开值 case id when id = #{item.id} then #{item.status} ... end where id in (...);
第一种方法是用传统方式生成的多条SQL语句,执行效率相对较低。第二种方法是巧妙使用case when的语法完成的一条语句实现的批量更新的操作,执行效率相对较快。使用起来也比较方便。
参考文献
- https://blog.csdn.net/xyjawq1/article/details/74129316
- https://blog.csdn.net/qq_40558766/article/details/89966773
-
Mybatis批量更新数据
2022-02-14 17:39:24Mybatis批量更新实现 <update id="updateDeviceStatusBatch"> update s_camera_device <trim prefix="set" suffixOverrides=","> <trim prefix="status =case" suffix="end,"> <foreach ...Mybatis批量更新实现
<update id="updateDeviceStatusBatch"> update s_camera_device <trim prefix="set" suffixOverrides=","> <trim prefix="status =case" suffix="end,"> <foreach collection="devices" item="item" index="index"> <if test="item != null"> when device_no = #{item.deviceNo} then #{item.status} </if> </foreach> </trim> </trim> where device_no in <foreach collection="devices" index="index" item="item" separator="," open="(" close=")"> #{item.deviceNo} </foreach> </update>
根据设备号更新设备当前状态
该语法使用 case when 函数来解决批量更新数据
执行SQL
update s_camera_device set status = case when device_no = 1001 then 1 when device_no = 1002 then 1 when device_no = 1003 then 0 end where device_no in (1001,1002,1003)
-
mybatis批量更新操作
2022-05-10 17:00:36java项目中使用case when语句进行批量更新操作。 -
Java mybatis批量更新
2022-06-15 16:03:55Java mybatis批量更新 -
Mybatis批量更新报错
2021-06-29 19:30:46Mybatis批量更新报错1. 问题描述2. 问题分析3. 解决方案 1. 问题描述 使用批量更新,一直报语法错误,多次检查Mapper文件发现语法没啥问题,就是每次执行都是语法错误,花了好一会时间,特此记录一下。 错误信息如下... -
mybatis批量更新sql
2022-05-10 17:05:29最近有个业务是根据用户批量更新用户的所有属性,因为是个老项目,当我打开已有的方法时是类似这样的画风. for (Object o : list) { //操作数据库 根据用户id更新某项属性 } 我当时就心里mmp,之前公司批量更新的... -
mybatis批量更新数据三种方法
2022-04-24 09:57:42mybatis批量更新数据三种方法效率对比_PreciousLife的博客-CSDN博客_mybatis 批量更新 此处说明下,若是使用for循环遍历方式,来生成N条sql,那么就需要注意两个地方: 第一个地方: separator=";... -
mybatis 批量更新update详解文档
2022-07-13 09:16:16Mybatis批量更新Update详解文档 -
mybatis 批量更新数据 mysql批量更新数据
2021-12-08 00:28:00通常如果需要一次更新多条数据有两个方式,(1)在业务代码中循环遍历逐条更新。...1 批量更新不同的值 不同的条件 MySQL没有提供直接的方法来实现批量更新,但可以使用case when语法来实现这个功能。 -
【MyBatis】关于MyBatis批量更新的几种方式
2021-08-05 10:58:13这种方式就是将需要更新的数据,循环调用update方法去更新数据,实现代码如下: public void test() { // 需要更新的集合 List updateMap = new ArrayList(); HashMap param = new HashMap(3); param.p... -
mybatis批量更新及其效率问题
2020-11-10 14:10:57mybatis批量更新及其效率问题 一:背景 注意: 第一种: 第二种: 二:批量更新的方式总结: 第一种: 第二种: 三:总结: 最近,负责公司一些旧数据的批量整理和清洗... -
Mybatis批量更新详解
2021-03-05 14:05:26转:...但是批量更新却没有详细的解决方案。实现目标这里主要讲的是1张table中。根据不同的id值,来update不同的property。数据表:1张。Tblsupertitleresult... -
Mybatis批量更新Oracle和Mysql
2022-06-15 22:33:02Mybatis批量更新Oracle -
mybatis批量更新返回值
2021-04-15 10:05:46int updateAmount = saleContractDetailMapper.updateContractDetailAmountFromLading(contractDetailDTOList);...批量更新,返回值1和0,1更新成功(全部更新成功),0更新失败(一条失败或者全部失败) -
mybatis批量更新map集合
2022-04-15 11:49:58mybatis批量更新 需要在数据库链接url后面带一个参数 &allowMultiQueries=true spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/test?useUnicode=true&... -
mybatis 批量更新update优化
2022-02-18 17:40:56mybatis 批量更新update优化 -
Mybatis 批量更新及参数讲解
2021-10-06 07:49:35前言 Mybatis 对于大家来说真是再熟悉不过了,ORM...批量更新 #dao 层 void batchUpdateTargetData(@Param(value = "encryptListData") List<UserInfo> encryptListData,@Param(value = "yearNum") Integer ye -
mybatis执行批量更新batch update 的方法(oracle,mysql两种)
2020-08-31 17:30:58主要介绍了mybatis执行批量更新batch update 的方法,提供oracle和mysql两种方法,非常不错,需要的朋友参考下 -
关于mybatis批量更新(updateBatch)的问题
2022-04-18 11:07:58结尾,多条update语句拼接的方式进行批量更新的。 但是Mybatis默认是不支持以" ; " 结尾的sql语句的,也就是不支持多条sql语句的批量执行。 需要在连接mysql的url加上 allowMultiQueries 这个参数。才可以执行。 &... -
springboot + mybatis 批量更新问题
2022-03-20 20:28:30springboot + mybatis 批量更新问题 在开发过程中会或多或少的遇到些sql批量操作的问题,会因为sql或者配置的原因下造成批量操作无法实现。以下是一些问题总结: 1、mapper层需要添加注解,@Param 例如在UserMapper... -
mybatis批量更新数据三种方法效率对比
2020-09-28 11:43:17探讨批量更新数据三种写法的效率问题。 实现方式有三种, 1> 用for循环通过循环传过来的参数集合,循环出N条sql, 2> 用mysql的case when 条件判断变相的进行批量更新(推荐使用) 3> 用ON DUPLICATE KEY... -
mybatis批量更新list对象
2021-04-26 13:19:41mybatis要想批量更新,首先我们数据库需要支持批量更新操作 需要在连接数据库时,添加配置 url: jdbc:mysql://192.168.6.11:3306/equipment_im_dev?serverTimezone=Asia/Shanghai&characterEncoding=utf8&... -
Mybatis 批量更新数据(多字段批量)
2022-04-06 13:17:19-- 批量更新指标分数和级别--> <update id="updateAll" parameterType="com.futuredata.web.assess.pojo.WordVO"> update assess_factor_copy set fxjb=case id <foreach collection="list" item=...