mybatis批量插入和批量更新

时间:2022-11-09 02:50:04

批量插入数据使用的sql语句是:

insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo)

mybatis中mapper.xml的代码如下:

mybatis批量插入和批量更新
  <!-- 批量插入数据 -->
<insert id="insertBatch" parameterType="java.util.List"
useGeneratedKeys="true">
<selectKey resultType="long" keyProperty="id" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into wd_solr
(fayu_id, tablename,
name,logo,description,section_no,look_count,favorite_count,create_uid,create_time,update_time,timestamp)
values
<foreach collection="list" item="wdSolr" index="index"
separator=",">
(
#{wdSolr.fayuId},#{wdSolr.tablename},#{wdSolr.name},#{wdSolr.logo},
#{wdSolr.description},#{wdSolr.sectionNo},#{wdSolr.lookCount},#{wdSolr.favoriteCount},
#{wdSolr.createUid},#{wdSolr.createTime},#{wdSolr.updateTime},#{wdSolr.timestamp}
)
</foreach>
</insert>
mybatis批量插入和批量更新

批量更新数据使用的sql语句是:

mybatis批量插入和批量更新
UPDATE table
SET aa = CASE id
WHEN 1 THEN 'oo'
WHEN 2 THEN 'pp'
WHEN 3 THEN 'qq'
END
  ,SET bb = CASE id
WHEN 1 THEN 'xx'
WHEN 2 THEN 'yy'
WHEN 3 THEN 'zz'
END
WHERE id IN (1,2,3)
mybatis批量插入和批量更新

上面这一条mysql语句可以更新多条记录,mybatis中mapper.xml的代码如下:

mybatis批量插入和批量更新
  <!-- 批量更新数据 -->
<update id="updateBatch">
update wd_solr set
name =
<foreach collection="list" item="wdSolr" index="index"
separator=" " open="case id" close="end">
when #{wdSolr.id} then
#{wdSolr.name}
</foreach>
,logo =
<foreach collection="list" item="wdSolr" index="index"
separator=" " open="case id" close="end">
when #{wdSolr.id} then
#{wdSolr.logo}
</foreach>
,timestamp =
<foreach collection="list" item="wdSolr" index="index"
separator=" " open="case id" close="end">
when #{wdSolr.id} then #{wdSolr.timestamp}
</foreach>
where id in
<foreach collection="list" item="wdSolr" index="index"
separator="," open="(" close=")">
#{wdSolr.id}
</foreach>
</update>
mybatis批量插入和批量更新

mybatis批量插入和批量更新的更多相关文章

  1. mybatis&plus;mysql批量插入和批量更新、存在及更新

    mybatis+mysql批量插入和批量更新 一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo, ...

  2. spring data jpa开启批量插入、批量更新

    spring data jpa开启批量插入.批量更新 原文链接:https://www.cnblogs.com/blog5277/p/10661096.html 原文作者:博客园--曲高终和寡 *** ...

  3. MongoDB与传统数据库的使用区别——批量插入与批量查询

    我在百X知道上回答问题时经常遇到类似与这样的问题:MongoDB有没有像MySQL一样的ODBC驱动?MongoDB能不能像MySQL一样获取字段名称或类型. 我的回答是:不行,因为MongoDB不是 ...

  4. oracle 在xml中批量插入,批量修改及多组条件查询

    最近公司用ibatis开发项目,本来可以用存储过程处理批量插入,批量修改及多组条件查询:但由于使用模块相对较小,暂时就在xml中配置,以前没有在xml做过类似处理,有必要记录一下:好了,代码如下: & ...

  5. mybatis&plus;mysql批量插入和批量更新

    一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo,oo,oo) mybatis中mapper.x ...

  6. Mybatis 批量插入、批量更新

            合理的使用批量插入.更新对性能优化有很大的作用,速度明显快了N倍.         要注意数据库连接串后面要新增:&allowMultiQueries=true,表示一个sql ...

  7. Mybatis批量插入、批量更新

    合理的使用批量插入.更新对优化有很大的作用,速度明显快了N倍. 数据库连接串后面要新增:&allowMultiQueries=true 批量插入的最大限制主要是看你整条sql占用的大小,所以可 ...

  8. mybatis Oracle 批量插入,批量更新

    传入的参数只要是list类型的参数就行了..............1.批量插入<insert id="insertBatch" parameterType="ja ...

  9. mybatis的插入与批量插入的返回ID的原理

    目录 背景 底层调用方法 单个对象插入 列表批量插入 完成 背景 最近正在整理之前基于mybatis的半ORM框架.原本的框架底层类ORM操作是通过StringBuilder的append拼接的,这次 ...

随机推荐

  1. cf429B dp递推

    Description Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to loo ...

  2. 1&period;4 Service

    用于在后台完成用户指定的操作,为其他组件提供后台服务或监控其他组件的运行状态. 开发人员需要在应用程序配置文件中声明全部的service,使用<service></service&g ...

  3. C&plus;&plus;中的一些定义

    PS: 这篇博客用来记录一些一般的C++书中草草掠过的一些概念. 或者一些不太容易理解的概念的详细解释. 欢迎新手进入,欢迎高手指正! Orz . 引用: 为对象起了另外一个名字, 引用类型引用(re ...

  4. String&period;valueOf&lpar;null&rpar; 报空指针

    String.valueOf 默认的方法 argument 可以为null 的 boolean b = null; char c = null; char[] data = null; double ...

  5. 错误&quot&semi;Lc&period;exe 已退出,代码 -1 &quot&semi;

    今天做项目的时候突然出现编译不通过,错误为Lc.exe已退出,代码为-1.这让我郁闷了至少30分钟,后来上网查了一下,才知道原因所在,我们项目中使用了第三方组件(Infragistics)造成的,至于 ...

  6. Spring&plus;SpringMVC&plus;MyBatis深入学习及搭建&lpar;五&rpar;——动态sql

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——My ...

  7. C语言字符串的常见特殊操作&lpar;除了string&period;c实现的那些接口&rpar;

    字符串操作中,必须掌握的一些之前已经在文章有写过了,比如说字符串查找,字符串粘帖,字符串拷贝等等,这些在标准C库的string.c中已经有实现,故包含#include <string.h> ...

  8. Unity 协同程序

    定义协同程序: IEnumerator test() { Debug.log("test 1"); yeild return WaitForSecond(3.0f); Debug. ...

  9. 使用Flame Graph进行系统性能分析

    关键词:Flame Graph.perf.perl. FlameGraph是由BrendanGregg开发的一款开源可视化性能分析工具,形象的成为火焰图. 从底向上像火苗一样逐渐变小,也反映了相互之间 ...

  10. &period;NET Framework 4&period;0&sol;4&period;5离线版下载

    /******************************************************************************* * .NET Framework 4. ...