Mybatis 批量插入、批量更新
来源:互联网 发布:js设置属性值 编辑:程序博客网 时间:2024/04/28 04:30
合理的使用批量插入、更新对性能优化有很大的作用,速度明显快了N倍。
要注意数据库连接串后面要新增:&allowMultiQueries=true,表示一个sql可以通过分号分割为多个独立sql。
批量插入的最大限制主要是看你整条sql的长度大小,所以可以根据自身sql长度的大小来配置这个要分批的每批的个数。
批量插入
Dao
1
2
3
public
Integer saveStudents(List<Student> students) {
return
super
.getSqlSession().insert(
"StudentMapper.insertStudents"
, students);
}
Mapper
1
2
3
4
5
6
7
8
9
10
11
<
insert
id
=
"insertStudents"
parameterType
=
"java.util.List"
>
insert into t_student(
city_id
)
values
<
foreach
collection
=
"list"
item
=
"itm"
index
=
"index"
separator
=
","
>
(
#{itm.city_id}
)
</
foreach
>
</
insert
>
批量更新
Dao
1
2
3
public
Integer updateStudents(List<Student> students) {
return
super
.getSqlSession().update(
"StudentMapper.updateStudents"
, students);
}
Mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
<
update
id
=
"updateStudents"
parameterType
=
"java.util.List"
>
<
foreach
collection
=
"list"
item
=
"item"
index
=
"index"
open
=
""
close
=
""
separator
=
";"
>
UPDATE t_studnet
<
set
>
<
if
test
=
"item.name != null"
>
name= #{item.name}
</
if
>
</
set
>
WHERE id = #{item.id}
AND age = #{item.age}
</
foreach
>
</
update
>
List分批的工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 分批list
*
* @param sourceList
* 要分批的list
* @param batchCount
* 每批list的个数
* @return List<List<Object>>
*/
public
static
List<List<?>> batchList(List<?> sourceList,
int
batchCount) {
List<List<?>> returnList =
new
ArrayList<>();
int
startIndex =
0
;
// 从第0个下标开始
while
(startIndex < sourceList.size()) {
int
endIndex =
0
;
if
(sourceList.size() - batchCount < startIndex) {
endIndex = sourceList.size();
}
else
{
endIndex = startIndex + batchCount;
}
returnList.add(sourceList.subList(startIndex, endIndex));
startIndex = startIndex + batchCount;
// 下一批
}
return
returnList;
}
0 0
- Mybatis 批量插入、批量更新
- Mybatis批量插入、更新
- mybatis批量插入与批量更新。
- MyBatis中批量插入和批量更新
- mybatis+oracle批量插入,批量更新
- Mybatis批量更新和插入
- mybatis批量更新和插入
- mybatis 批量插入 ,更新总结
- Mybatis批量插入更新数据
- spring springmvc mybatis 整合批量操作,批量更新,批量插入
- mybatis(mysql)批量插入 更新 删除
- Mybatis批量更新和插入数据
- mybatis的批量插入与更新
- MyBatis中批量插入、删除、更新
- 基于MyBatis的批量插入更新实现
- mybatis 批量查询插入、更新操作
- Mybatis批处理(批量查询,更新,插入)
- Oracle数据库 Mybatis批量插入,更新
- 更改ORACLE归档路径及归档模式
- slidingmenu学习总结
- Ubuntu下运行Faster-Rcnn
- 循环队列
- 不同人员对性能的关注点与web前端性能测试概念
- Mybatis 批量插入、批量更新
- source control : create git repository on my mac
- 习题三1008
- 按钮触摸状态
- 使用C++复制一个文本文件内容
- 屏蔽控制台应用程序的窗口#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
- jQuery获取 radio 选中后的文字 ,获取
- JVM(Java虚拟机)优化大全和案例实战
- Android之路的起点!