这篇文章主要介绍了Mybatis怎么批量导入数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1. 循环插入

mapper.xml:

<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.buhe.demo.mapper.StudentMapper"><insertid="insert"parameterType="Student">INSERTINTOtb_student(name,age,phone,address,class_id)VALUES(#{name},#{age},#{phone},#{address},#{classId})</insert></mapper>

mapper接口:

publicinterfaceStudentMapper{intinsert(Studentstudent);}

测试代码:

@SpringBootTestclassDemoApplicationTests{@ResourceprivateStudentMapperstudentMapper;@TestpublicvoidtestInsert(){//数据生成List<Student>studentList=createData(100);//循环插入longstart=System.currentTimeMillis();studentList.stream().forEach(student->studentMapper.insert(student));System.out.println(System.currentTimeMillis()-start);}privateList<Student>createData(intsize){List<Student>studentList=newArrayList<>();Studentstudent;for(inti=0;i<size;i++){student=newStudent();student.setName("小王"+i);student.setAge(18);student.setClassId(1);student.setPhone("1585xxxx669");student.setAddress("未知");studentList.add(student);}returnstudentList;}}2. foreach标签

mapper.xml:

<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.buhe.demo.mapper.StudentMapper"><insertid="insert"parameterType="Student">INSERTINTOtb_student(name,age,phone,address,class_id)VALUES(#{name},#{age},#{phone},#{address},#{classId})</insert><insertid="insertBatch">INSERTINTOtb_student(name,age,phone,address,class_id)VALUES<foreachcollection="list"separator=","item="item">(#{item.name},#{item.age},#{item.phone},#{item.address},#{item.classId})</foreach></insert></mapper>

mapper接口:

publicinterfaceStudentMapper{intinsert(Studentstudent);intinsertBatch(List<Student>studentList);}

测试代码:

@SpringBootTestclassDemoApplicationTests{@ResourceprivateStudentMapperstudentMapper;@TestpublicvoidtestInsertByForeachTag(){//数据生成List<Student>studentList=createData(100);//使用foreach标签,拼接SQL插入longstart=System.currentTimeMillis();studentMapper.insertBatch(studentList);System.out.println(System.currentTimeMillis()-start);}privateList<Student>createData(intsize){List<Student>studentList=newArrayList<>();Studentstudent;for(inti=0;i<size;i++){student=newStudent();student.setName("小王"+i);student.setAge(18);student.setClassId(1);student.setPhone("1585xxxx669");student.setAddress("未知");studentList.add(student);}returnstudentList;}}3. 批处理

测试代码:

@SpringBootTestclassDemoApplicationTests{@AutowiredprivateSqlSessionFactorysqlSessionFactory;@TestpublicvoidtestInsertBatch(){//数据生成List<Student>studentList=createData(100);//使用批处理longstart=System.currentTimeMillis();SqlSessionsqlSession=sqlSessionFactory.openSession(ExecutorType.BATCH,false);StudentMapperstudentMapperNew=sqlSession.getMapper(StudentMapper.class);studentList.stream().forEach(student->studentMapperNew.insert(student));sqlSession.commit();sqlSession.clearCache();System.out.println(System.currentTimeMillis()-start);}privateList<Student>createData(intsize){List<Student>studentList=newArrayList<>();Studentstudent;for(inti=0;i<size;i++){student=newStudent();student.setName("小王"+i);student.setAge(18);student.setClassId(1);student.setPhone("1585xxxx669");student.setAddress("未知");studentList.add(student);}returnstudentList;}}三种方式的对比

MySQL服务器版本:5.6.4

其他依赖版本如下:

<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.4</version><relativePath/><!--lookupparentfromrepository--></parent><groupId>com.buhe</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>DemoprojectforSpringBoot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.41</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources></build></project>

三种插入方式在不同数据量下的表现,测试结果:

插入方式10条100条500条1000条循环插入496ms3330ms15584ms33755msforeach标签268ms366ms392ms684ms批处理222ms244ms364ms426ms

三种方式中,批处理的方式效率是最高的,尤其是在数据量大的情况下尤为明显。

其次是foreach标签,foreach标签是通过拼接SQL语句的方式完成批量操作的。但是当拼接的SQL过多,导致SQL大小超过了MySQL服务器中max_allowed_packet变量的值时,会导致操作失败,抛出PacketTooBigException异常。

最后是循环插入的方式,这种方式在数据量小的时候可以使用,在数据量大的情况下效率要低很多。

感谢你能够认真阅读完这篇文章,希望小编分享的“Mybatis怎么批量导入数据”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!