Mybatis 批量操作-删除、修改和查询
- SQL语句有长度限制,在进行数据合并在同一SQL中务必不能超过SQL长度限制,通过max_allowed_packet配置可以修改,默认是1M。
- 事务需要控制大小,事务太大可能会影响执行的效率。MySQL有innodb_log_buffer_size配置项,超过这个值会把InnoDB的数据刷到磁盘中,这时,效率会有所下降。所以比较好的做法是,在数据达到这个值前进行事务提交。
jdbc:mysql://127.0.0.1:3306/mybank?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
批量删除
|
<delete id= "batchDeleteByIds" parameterType= "list">
delete from instance where instance_id in
<foreach collection="list" item= "item" index ="index"
open= "(" close =")" separator=",">
#{item}
</foreach >
</delete >
|
|
<update id= "updateUpdateTimeByIds" parameterType= "map">
update instance
set update_time = #{ updateTime } where instance_id in
<foreach collection="idlist" item= "uid" index ="index"
open= "(" close =")" separator=",">
#{ uid}
</foreach >
</update >
|
|
<select id="selectByIds" resultType="list" parameterType="map">
SELECT infos, create_time, update_time FROM instance WHERE instance_id in
<foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
|
|
List<Instance> selectByIds (Map<String, Object> map);
void batchDeleteByIds (List<Long> list);
void updateUpdateTimeByIds(Map<String, Object> map);
|
|
Of course don't combine ALL of them, if the amount is HUGE. Say you have 1000 rows you need to insert, then don't do it one at a time. You shouldn't equally try to have all 1000 rows in a single query. Instead break it into smaller sizes.
|
|
Insert inside MyBatis foreach is not batch, this is a single (could become giant) SQL statement and that brings drawbacks:
Iteration over the collection must not be done in the mybatis XML. Just execute a simple Insertstatement in a Java Foreach loop. The most important thing is the session Executor type.
SqlSession session = sessionFactory.openSession(ExecutorType.BATCH); Unlike default ExecutorType.SIMPLE, the statement will be prepared once and executed for each record to insert.
|
...
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
SimpleTableMapper mapper = session.getMapper(SimpleTableMapper.class);
List<SimpleTableRecord> records = getRecordsToInsert(); // not shown BatchInsert<SimpleTableRecord> batchInsert = insert(records)
.into(simpleTable)
.map(id).toProperty("id")
.map(firstName).toProperty("firstName")
.map(lastName).toProperty("lastName")
.map(birthDate).toProperty("birthDate")
.map(employed).toProperty("employed")
.map(occupation).toProperty("occupation")
.build()
.render(RenderingStrategy.MYBATIS3); batchInsert.insertStatements().stream().forEach(mapper::insert); session.commit();
} finally {
session.close();
}
...
Mybatis 批量操作-删除、修改和查询的更多相关文章
- 转载Entity Framework 5.0(EF first)中的添加,删除,修改,查询,状态跟踪操作
转载原出处:http://www.cnblogs.com/kenshincui/p/3345586.html Entity Framework将概念模型中定义的实体和关系映射到数据源,利用实体框架可以 ...
- MyBatis中实现多表查询
如果查询的数据量大,推荐使用N+1次查询.数据量少使用联合查询... 一. 1.Mybatis是实现多表查询方式 1.1 业务装配:对两个表编写单表查询语句,在业务(Service)把查询的两表结果 ...
- Nodejs之MEAN栈开发(九)---- 用户评论的增加/删除/修改
由于工作中做实时通信的项目,需要用到Nodejs做通讯转接功能,刚开始接触,很多都不懂,于是我和同事就准备去学习nodejs,结合nodejs之MEAN栈实战书籍<Getting.MEAN.wi ...
- 9_13学习完整修改和查询&&实体类,数据访问类
完整修改和查询:中间变量运用. 1.先查 2.执行操作 ---------------------------------------------------- namespace ADO.NET_小 ...
- ADO.NET(完整修改和查询、实体类,数据访问类)
一.完整修改和查询 在编写c#语句时需考虑到用户体验,例如在编写修改语句时,需要考虑到输入的内容在数据库中是否能够找到. 中间变量运用. 1.先查 2.执行操作 完整修改语句: bool has = ...
- SQL语句添加删除修改字段及一些表与字段的基本操作
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200)2.删除字段 ALTER TABLE table_NA ...
- Mybatis oracle多表联合查询分页数据重复的问题
Mybatis oracle多表联合查询分页数据重复的问题 多表联合查询分页获取数据时出现一个诡异的现象:数据总条数正确,但有些记录多了,有些记录却又少了甚至没了.针对这个问题找了好久,最后发现是由于 ...
- 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...
- SQL语句添加删除修改字段[sql server 2000/2005]
用SQL语句添加删除修改字段1.增加字段 alter table docdsp add dspcodechar(200)2.删除字段 ALTER TABLE table_NAME ...
随机推荐
- hdu 2647 还是逆向拓扑
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- 【爬虫集合】Python爬虫
一.爬虫学习教程 1. https://www.jianshu.com/u/c32d557edfa3 2. WebMagic是一个简单灵活的Java爬虫框架.基于WebMagic,你可以快速开发出一个 ...
- C# Combox控件绑定自定义数据
DataTable dt = new DataTable(); dt.Columns.Add("name"); dt.Columns.A ...
- Unity 自定义"=="操作符 [翻译来源blogs.unity3d,2014/05]
主要内容来源 https://blogs.unity3d.com/cn/2014/05/16/custom-operator-should-we-keep-it/ 在我们代码里,如果有这样的代码: i ...
- 2.Redis 入门介绍
A)入门概述: 1.是什么: Redis:REmote Dlctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵循BSD协议,是一个高性能的(key/value) ...
- CentOS 7安装Hadoop集群
准备三台虚拟机,ip分别为192.168.220.10(master).192.168.220.11(slave1).192.168.220.12(slave2) 准备好jdk-6u45-linux- ...
- 【python】python configparser模块
ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值).使用的配置 ...
- nc 命令
目录 nc 命令 一.简介 二.案例 1.端口扫描 2.聊天 3.文件传输 4.目录传输 5.加密网络发送的数据 6.流视频 7.克隆一个设备 8.打开一个shell 9.反向shell 10.指定端 ...
- python读txt数据报编码错误
读数据代码: with open(path,'r') as f: for line in f: line = line.strip() 报错: UnicodeDecodeError: 'gbk' co ...
- YOLO---近段时间的练习目标
YOLO---近段时间的练习目标 yolo(darknet)官方主页:https://pjreddie.com/darknet/yolo/ 和在学校时用的不太一样了,有更新了- 还有一个常用版本: ...