mybatis批量插入和批量更新
批量插入数据使用的sql语句是:
insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo)
mybatis中mapper.xml的代码如下:

<!-- 批量插入数据 -->
<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>

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

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)

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

<!-- 批量更新数据 -->
<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+mysql批量插入和批量更新、存在及更新
mybatis+mysql批量插入和批量更新 一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo, ...
- spring data jpa开启批量插入、批量更新
spring data jpa开启批量插入.批量更新 原文链接:https://www.cnblogs.com/blog5277/p/10661096.html 原文作者:博客园--曲高终和寡 *** ...
- MongoDB与传统数据库的使用区别——批量插入与批量查询
我在百X知道上回答问题时经常遇到类似与这样的问题:MongoDB有没有像MySQL一样的ODBC驱动?MongoDB能不能像MySQL一样获取字段名称或类型. 我的回答是:不行,因为MongoDB不是 ...
- oracle 在xml中批量插入,批量修改及多组条件查询
最近公司用ibatis开发项目,本来可以用存储过程处理批量插入,批量修改及多组条件查询:但由于使用模块相对较小,暂时就在xml中配置,以前没有在xml做过类似处理,有必要记录一下:好了,代码如下: & ...
- mybatis+mysql批量插入和批量更新
一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo,oo,oo) mybatis中mapper.x ...
- Mybatis 批量插入、批量更新
合理的使用批量插入.更新对性能优化有很大的作用,速度明显快了N倍. 要注意数据库连接串后面要新增:&allowMultiQueries=true,表示一个sql ...
- Mybatis批量插入、批量更新
合理的使用批量插入.更新对优化有很大的作用,速度明显快了N倍. 数据库连接串后面要新增:&allowMultiQueries=true 批量插入的最大限制主要是看你整条sql占用的大小,所以可 ...
- mybatis Oracle 批量插入,批量更新
传入的参数只要是list类型的参数就行了..............1.批量插入<insert id="insertBatch" parameterType="ja ...
- mybatis的插入与批量插入的返回ID的原理
目录 背景 底层调用方法 单个对象插入 列表批量插入 完成 背景 最近正在整理之前基于mybatis的半ORM框架.原本的框架底层类ORM操作是通过StringBuilder的append拼接的,这次 ...
随机推荐
- 洛谷P2342-叠积木
Problem 洛谷P2342-叠积木 Accept: 373 Submit: 1.1k Time Limit: 1000 mSec Memory Limit : 128MB Problem ...
- 痞子衡嵌入式:如果你正在量产i.MX RT产品,不妨试试这款神器RT-Flash
-- RT产品落满地,客户工厂生产急; 痞子衡出新神器,从此量产不费力! 恩智浦半导体2017年10月正式发布了业内首款跨界处理器-i.MX RT系列,超强的性能.超高的性价比使得i.MX RT系列火 ...
- 用户输入序号选择商品,按q退出
输出商品列表,用户输入序号,显示用户选中的商品 li = ['手机','电脑','鼠标垫','游艇'] while 1: li = ['手机','电脑','鼠标垫','游艇'] for i in li ...
- c++ 启动exe(启动别的exe程序)
http://blog.csdn.net/sdcxyz/article/details/13631613 1例程 1.1面向过程例程如下: #include <iostream> #inc ...
- 使用cmd导出mysql数据到excel
windows环境 (有时候复制的不好使,最好可以手动输入一次试试) 1.windows + R 输入cmd弹出命令框 2.cd C:\Program Files\MySQL\MySQL Serve ...
- AliOS-Things linkkitapp解读
app-example-linkkitapp是AliOS-Things提供的设备联网并且和阿里云IOT平台数据交互的一个示例程序: 1:application_start()程序在app_entry. ...
- 【Codeforces Round 1114】Codeforces #538 (Div. 2)
Codeforces Round 1114 这场比赛做了\(A\).\(C\).\(D\).\(E\),排名\(134\). \(B\)题做了很长时间,好不容易最后一分钟\(Pretest\ Pass ...
- 创建http.Server实例,端口占用就换个端口
/** * Created by Sorrow.X on 2017/10/25. */ const http = require('http'); const url = require('url') ...
- Promise使用时应注意的问题
最近在使用axios库时遇到了个问题,后端接口报了500错误,但前端并未捕获到.1. 调用接口的业务代码如下: // 业务代码调用 axios({ url: url, method: 'post', ...
- decorator, async/await, generator
////////////decorator////////// function aopFunc (target, key, descriptor) { console.log('aopFunc') ...