mybatis 批量插入和批量更新

news/2024/6/2 10:50:22 标签: mybatis

一.批量插入

批量插入时,可以借助<foreach>标签构造多个数据执行插入

  1. 传入参数为List<Customer>
  2. 执行sql语句如下
<insert id="saveBatch" parameterType="java.util.List">
    insert into customer
     (name, password, age, weight) 
     values
     <foreach collection="list" separator="," item="item">
         (#{item.name}, #{item.password}, #{item.age}, #{item.weight})
     </foreach>
 </insert>

二. 批量更新

批量更新时,需要借助<foreach> <trim> 标签

  1. 方式一: 执行多条更新sql
  • 修改连接配置加上allowMultiQueries=true
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
  • 多条sql如下
<update id="updateCustomer">
       <foreach close=";" collection="list" item="item" open=" " separator=";">
           update customer
           set
           		name= #{item.name},
           		password = #{item.password},
           		age = #{item.age}
           where id =  #{item.id}
       </foreach>
</update>
  • 这种方式本质上是一次性执行多条更新sql语句
  1. 方式二:借助case when
  • 传入对象List
  • 原生更新执行sql语句如下
update customer
set 
	name= case id 
	    WHEN 1 THEN 'zhangsan'
	    WHEN 2 THEN 'lisi'
	    WHEN 3 THEN 'wangwu'
	end, 
	age = case id  
	    WHEN 1 THEN 123
	    WHEN 2 THEN 456
	    WHEN 3 THEN 789
	end
where id IN (1,2,3)

这段sql的含义是,根据id更新 name age 字段, 分别当id等于1 or 2 or 3 时,更新其值

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

http://www.niftyadmin.cn/n/4951909.html

相关文章

计算机竞赛 协同过滤电影推荐系统

文章目录 1 简介1 设计概要2 课题背景和目的3 协同过滤算法原理3.1 基于用户的协同过滤推荐算法实现原理3.1.1 步骤13.1.2 步骤23.1.3 步骤33.1.4 步骤4 4 系统实现4.1 开发环境4.2 系统功能描述4.3 系统数据流程4.3.1 用户端数据流程4.3.2 管理员端数据流程 4.4 系统功能设计 …

Unreal Engine 测试总结

Android 项目打包应选择哪种纹理格式&#xff1f;打包模式区别&#xff1f; 根据官网文档介绍&#xff0c;建议使用 ETC2&#xff1a;所有OpenGL 3.x 类型的设备都支持&#xff0c;并且支持alpha压缩 打包模式包括&#xff1a;内部测试阶段的开发模式&#xff0c;对外发布的发行…

Python爬虫淘宝商品代码指南

更新&#xff1a;2023-06-13 15:03 本篇文章将为您详细介绍使用Python爬取淘宝商品信息的代码步骤及方法 一、爬虫基础知识 在开始淘宝商品信息爬取之前&#xff0c;我们需要了解一些基础知识&#xff1a; 1、Python基础语法知识&#xff1a;包括基本数据类型、字符串、列…

idea2023 springboot2.7.5+mybatisplus3.5.2+jsp 初学单表增删改查

创建项目 修改pom.xml 为2.7.5 引入mybatisplus 2.1 修改pom.xml <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency><!--mysq…

TypeError: a bytes-like object is required, not ‘str‘

raceback (most recent call last): File "D:\pycharmcode\client.py", line 12, in <module> tcp_socket.send(send_data) TypeError: a bytes-like object is required, not str 使用socket进行ubuntu与windows通信时&#xff0c;发送数据时报了以上错…

第五章,向量空间,3-内积、长度、夹角和距离

第五章&#xff0c;向量空间&#xff0c;3-内积、长度、夹角和距离 内积定义内积的运算性质内积空间 长度性质单位向量及单位化 夹角距离 玩转线性代数(28)长度、夹角和距离的笔记&#xff0c;相关证明以及例子见原文 内积 定义 设有n维向量 x ( x 1 x 2 ⋮ x n ) , y ( y…

【C++】C++入门基础:引用详解

本篇继续分享关于C入门的相关知识&#xff0c;有关命名空间、缺省参数和函数重载的部分欢迎阅读我的上一篇文章【C】C入门基础详解&#xff08;1&#xff09;_王笃笃的博客-CSDN博客 继续我们的学习 引用 在C语言中我们接触过指针&#xff0c;很多人都或多或少为他感到头痛过…

Python 潮流周刊#16:优雅重要么?如何写出 Pythonic 的代码?

你好&#xff0c;我是猫哥。这里每周分享优质的 Python、AI 及通用技术内容&#xff0c;大部分为英文。标题取自其中两则分享&#xff0c;不代表全部内容都是该主题&#xff0c;特此声明。 本周刊由 Python猫 出品&#xff0c;精心筛选国内外的 250 信息源&#xff0c;为你挑选…