Mybatis的mapper.xml批量插入、修改sql

2023-09-13 17:52:09

今天要有个功能,要进行一批数据的插入和修改,为了不频繁调用数据库,所以想到了批量插入和修改,因为从毕业后,就没写过批量插入和批量修改,所以在这里记录一下,避免后续再遇到忘记怎么写了

批量插入(传入的参数是List<实体>list):

<insert id="insertList" keyColumn="id" keyProperty="id" useGeneratedKeys="true" parameterType="java.util.List">
    insert into xhs_collection_data (note_id,
         `status`,
         title,
         `desc`,
         `time`,
         user_id,
         nickname,
         liked_count,
         collected_count,
         comment_count,
         share_count,
         image_list,
         tag_list,
         batch_number,
         file_name) values
        <foreach collection="list" separator="," item="item">
            ( #{item.noteId,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER},#{item.title,jdbcType=VARCHAR},#{item.desc,jdbcType=VARCHAR},#{item.time,jdbcType=TIMESTAMP},
            #{item.userId,jdbcType=VARCHAR}, #{item.nickname,jdbcType=VARCHAR},#{item.likedCount,jdbcType=VARCHAR},#{item.collectedCount,jdbcType=VARCHAR},#{item.commentCount,jdbcType=TIMESTAMP},
            #{item.shareCount,jdbcType=VARCHAR},#{item.imageList,jdbcType=VARCHAR},#{item.tagList,jdbcType=VARCHAR},#{item.batchNumber,jdbcType=BIGINT},#{item.fileName,jdbcType=VARCHAR}
            )
        </foreach>
</insert>



批量修改(传入的参数是List<实体>list):

sql原理语句:update table set 要修改的表字段A = case when 表字段 = 实体数据字段 then 实体数据字段 when 表字段 = 实体数据字段 then 实体数据字段 when … then… end,
要修改的表字段B = case when 表字段 = 实体数据字段 then 实体数据字段 when 表字段 = 实体数据字段 then 实体数据字段 when … then… end where 条件

注意:这里踩过一个坑,因为当时不会写批量修改的语句,所以让文心一言帮忙生成了一个批量修改的sql,后续我忘记是不是手动给<foreach>标签手动加的<separator=“,”>这个属性,结果报错了,排查了半天这个sql哪里错了,最后还是放到数据库执行了一下看到了错误原因,当时我还让温馨一样帮忙检查了下我修改后的sql,结果还说没sql没问题,只是可能在拼接时报错

<update id="updateList" parameterType="java.util.List">
   update xhs_collection_data
   <trim prefix="set" suffixOverrides=",">
       <trim prefix="`status` = case" suffix="end,">
           <foreach collection="list" index="index" item="item" >
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.status,jdbcType=INTEGER}
           </foreach>
       </trim>
       <trim prefix="title = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.title,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="`desc` = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.desc,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="time = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.time,jdbcType=TIMESTAMP}
           </foreach>
       </trim>
       <trim prefix="user_id = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.userId,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="nickname = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.nickname,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="liked_count = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.likedCount,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="collected_count = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.collectedCount,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="comment_count = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.commentCount,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="share_count = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.shareCount,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="image_list = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.imageList,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="tag_list = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.tagList,jdbcType=VARCHAR}
           </foreach>
       </trim>
       <trim prefix="batch_number = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               <if test="item.batchNumber != null">
                   when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.batchNumber,jdbcType=BIGINT}
               </if>
           </foreach>
       </trim>
       <trim prefix="file_name = case" suffix="end,">
           <foreach collection="list" index="index" item="item">
               <if test="item.fileName != null">
                   when note_id = #{item.noteId,jdbcType=VARCHAR} then #{item.fileName,jdbcType=VARCHAR}
               </if>
           </foreach>
       </trim>
   </trim>
   where note_id in
   <foreach close=")" collection="list" item="item" open="(" separator=", ">
       #{item.noteId,jdbcType=VARCHAR}
   </foreach>
</update>
更多推荐

go语言操作数据库

1.10GO连接MySQL因为Go语言没有提供任何官方数据库驱动,所以需要安装第三方函数库。由于在github上安装,所以需要安装git软件,安装过程一直点击下一步即可。安装完成后需要配置环境变量1.10.1安装gitgit软件安装完毕后,配置git的环境变量,这样可以使用get的指令1.10.2安装数据库驱动设置GO

简单几个配置 Go 实现敏感数据脱敏,可以自定义数据脱敏规则(附完整实现源码)

简单几个配置Go实现敏感数据脱敏,可以自定义数据脱敏规则(附完整实现源码)。介绍为了保障企业的数据安全和隐私安全,godlp提供了一系列针对敏感数据的识别和处置方案,其中包含敏感数据识别算法,数据脱敏处理方式,业务自定义的配置选项和海量数据处理能力。godlp能够应用多种隐私合规标准,对原始数据进行分级打标、判断敏感级

pytorch环境配置

pytorch安装与基础pytorch相关配置一:安装cuda1:找到nvidiacontrolpanel2:打开控制面板,找到系统信息中的,组件即可查看到cuda版本,这里我的cuda版本为12.0.1503:下载对应版本的cudatoolkit(我下载的12.0.1)4:安装流程5.验证是否安装成功二:配置cuDN

5-2 Pytorch中的模型层layers

深度学习模型一般由各种模型层组合而成。torch.nn中内置了非常丰富的各种模型层。它们都属于nn.Module的子类,具备参数管理功能。例如:nn.Linear,nn.Flatten,nn.Dropout,nn.BatchNorm2d,nn.Embeddingnn.Conv2d,nn.AvgPool2d,nn.Con

Go并发可视化解释 – select语句

上周,我发布了一篇关于如何直观解释Golang中通道(Channel)的文章。如果你对通道仍然感到困惑,请先查看那篇文章。Go并发可视化解释—Channel作为一个快速复习:Partier、Candier和Stringer经营着一家咖啡店。Partier负责接受顾客的订单,然后将这些订单传递给厨房,Candier和St

go学习-基本知识点

string转[]byte通用的转换会发生内存拷贝,但是如下利用unsafe.Pointer实现的强转则不需要拷贝funcTestDemo(t*testing.T){a:="aaa"b:="bbbbbbb"//ssh是a的内存地址//通过将字符串的底层地址强制转换成StringHead结构来获取字符串的底层指针和长度/

kafka消费者模式

一、单线程消费者模式packagenj.zb.kb23.kafka;importorg.apache.kafka.clients.consumer.ConsumerConfig;importorg.apache.kafka.clients.consumer.ConsumerRecord;importorg.apache

Python与数据分析--Matplotlib-2

目录1.写在文前2.实例1--武汉新冠趋势图3.简单散点图实例4.灵活散点图实例5.饼图操作实例1.写在文前"""Matplotlib库我们大致已经学完了,但是缺乏一定的实战经验。今天我们就来进行一下matplotlib的实战操作。"""#导入对应的库函数importnumpyasnpimportmatplotlib.

day40 设计模式、jdk8新特性

一、代理模式为其他对象提供一种代理控制此对象的访问若一个对象不适合直接引用另一个对象,代理对象在客户端和目标对象之间起到中介作用组成:抽象角色:通过接口抽象类真实角色实现了哪些方法代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法可以附加自己的操作真实角色:实现抽象角色,定义业务逻辑,供

【Redis 多机服务的简单认识】

目录主从同步哨兵模式集群服务随着业务的不断发展,单机Redis的性能已经不能满⾜我们的需求了,此时我们需要将单机Redis扩展为多机服务,Redis多机服务主要包含以下3个内容:Redis主从同步Redis哨兵模式Redis集群服务(Redis3.0新增功能)主从同步主从同步(主从复制)是Redis⾼可⽤服务的基⽯,也

OpenCV(三十八):二维码检测

1.二维码识别原理功能图形:位置探测图形:通常,二维码中有三个位置探测图形,呈现L型或大角度十字架形状,分布在二维码的三个角上,用于帮助扫描设备定位二维码的位置和方向。位置探测图形分隔符:帮助扫描设备区分位置探测图形和二维码的数据区域。计算模式:通常是一个小的正方形图案,用于校准扫描设备以捕捉和解码二维码的图像。对齐标

热文推荐