数据库 在使用mybatis插入大量数据的时候,为了提高效率,放弃循环插入,改为批量插入,mapper如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.lcy.service.mapper;
import com.lcy.service.pojo.TestVO;
import org.apache.ibatis.annotations.Insert;
import java.util.List;
public interface TestMapper {
@Insert ( "" )
Integer testBatchInsert(List list);
}
|
实体类:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.lcy.service.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestVO {
private String t1;
private String t2;
private String t3;
private String t4;
private String t5;
}
|
测试类如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import com.lcy.service.TestApplication;
import com.lcy.service.mapper.TestMapper;
import com.lcy.service.pojo.TestVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest (classes = TestApplication. class )
@RunWith (SpringRunner. class )
public class TestDemo {
@Autowired
private TestMapper testMapper;
@Test
public void insert() {
List list = new ArrayList<>();
for ( int i = 0 ; i < 200000 ; i++) {
list.add( new TestVO(i + "," + i, i + "," + i, i + "," + i, i + "," + i, i + "," + i));
}
System.out.println(testMapper.testBatchInsert(list));
}
}
|
为了复现bug,我限制了JVM内存:
执行测试类报错如下:
java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.Arrays.copyOf(Arrays.java:3746)
可以看到,Arrays在申请内存的时候,导致栈内存溢出
改进方法,分批新增:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import com.lcy.service.TestApplication;
import com.lcy.service.mapper.TestMapper;
import com.lcy.service.pojo.TestVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@SpringBootTest (classes = TestApplication. class )
@RunWith (SpringRunner. class )
public class TestDemo {
@Autowired
private TestMapper testMapper;
@Test
public void insert() {
List list = new ArrayList<>();
for ( int i = 0 ; i < 200000 ; i++) {
list.add( new TestVO(i + "," + i, i + "," + i, i + "," + i, i + "," + i, i + "," + i));
}
int index = list.size() / 10000 ;
for ( int i= 0 ;i< index;i++){
//stream流表达式,skip表示跳过前i*10000条记录,limit表示读取当前流的前10000条记录
testMapper.testBatchInsert(list.stream().skip(i* 10000 ).limit( 10000 ).collect(Collectors.toList()));
}
}
}
|
还有一种方法是调高JVM内存,不过不建议使用,不仅吃内存,而且数据量过大会导致sql过长报错
到此这篇关于详解mybatis批量插入10万条数据的优化过程的文章就介绍到这了,更多相关mybatis批量插入10万数据内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://blog.csdn.net/weixin_39841589/article/details/93740276
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 50
-
2025-05-29 58
-
2025-06-04 89
-
2025-06-04 103
-
2025-06-04 78
热门评论