万mybatis批量插入10条数据的示例分析

  介绍

这篇文章给大家分享的是有关mybatis批量插入万10条数据的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

数据库在使用mybatis插入大量数据的时候,为了提高效率,放弃循环插入,改为批量插入,映射器如下:

package  com.lcy.service.mapper;      import  com.lcy.service.pojo.TestVO;   import  org.apache.ibatis.annotations.Insert;      import 并不知道;      public  interface  TestMapper  {      ,,,@Insert (“”)   ,,,Integer  testBatchInsert (List 列表);   }

实体类:

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;      }

测试类如下:

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 并不知道;      @SpringBootTest (classes =, TestApplication.class)   @RunWith (SpringRunner.class)   public  class  TestDemo  {      ,,@ autowired   ,,,private  TestMapper  testMapper;      ,,@Test   ,,,public  void 插入(),{   ,,,,,,,List  List =, new  ArrayList<在();   ,,,,,,,for  (int 小姐:=,0;,小姐:& lt;, 200000;,我+ +),{   ,,,,,,,,,,,list.add (new  TestVO(小姐:+,,,,,+,我,,小姐:+,,,,,+,我,,小姐:+,,,,,+,我,,小姐:+,,,,,+,我,,小姐:+,,,,,+,i));   ,,,,,,,}   ,,,,,,,System.out.println (testMapper.testBatchInsert(列表);   ,,,}      }

为了复现错误,我限制了JVM内存:

 mybatis批量插入万10条数据的示例分析

执行测试类报错如下:

. lang。OutOfMemoryError: Java堆空间

,在java.base/java.util.Arrays.copyOf (Arrays.java: 3746)

可以看的到,数组在申请内存的时候,导致栈内存溢出

改进方法,分批新增:

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 并不知道;   import  java.util.stream.Collectors;      @SpringBootTest (classes =, TestApplication.class)   @RunWith (SpringRunner.class)   public  class  TestDemo  {      ,,@ autowired   ,,,private  TestMapper  testMapper;      ,,@Test   ,,,public  void 插入(),{   ,,,,,,,List  List =, new  ArrayList<在();   ,,,,,,,for  (int 小姐:=,0;,小姐:& lt;, 200000;,我+ +),{   ,,,,,,,,,,,list.add (new  TestVO(小姐:+,,,,,+,我,,小姐:+,,,,,+,我,,小姐:+,,,,,+,我,,小姐:+,,,,,+,我,,小姐:+,,,,,+,i));   ,,,,,,,}   ,,,,,,,int  index =, list.size (),/, 10000;   ,,,,,,,for  (int  i=0; i<,指数;我+ +){   ,,,,,,,,,,,//流流表达式,跳过表示跳过前我* 10000条记录,限制表示读取当前流的前10000条记录   ,,,,,,,,,,,testMapper.testBatchInsert (list.stream () .skip(我* 10000).limit (10000) .collect (Collectors.toList ()));   ,,,,,,,}   ,,,}   null

万mybatis批量插入10条数据的示例分析