-
Mybatis的三种批量插入方式
1. 循环插入
mapper.xml:
<mapper namespace="com.buhe.demo.mapper.StudentMapper"> <insert id="insert" parameterType="Student"> INSERT INTO tb_student (name, age, phone, address, class_id) VALUES (#{name},#{age},#{phone},#{address},#{classId}) </insert> </mapper>
mapper接口:
public interface StudentMapper { int insert(Student student); }
测试代码:
class DemoApplicationTests { private StudentMapper studentMapper; public void testInsert(){ //数据生成 List<Student> studentList = createData(100); //循环插入 long start = System.currentTimeMillis(); studentList.stream().forEach(student -> studentMapper.insert(student)); System.out.println(System.currentTimeMillis() - start); } private List<Student> createData(int size){ List<Student> studentList = new ArrayList<>(); Student student; for(int i = 0; i < size; i++){ student = new Student(); student.setName("小王" + i); student.setAge(18); student.setClassId(1); student.setPhone("1585xxxx669"); student.setAddress("未知"); studentList.add(student); } return studentList; } }
2. foreach标签
mapper.xml:
<mapper namespace="com.buhe.demo.mapper.StudentMapper"> <insert id="insert" parameterType="Student"> INSERT INTO tb_student (name, age, phone, address, class_id) VALUES (#{name},#{age},#{phone},#{address},#{classId}) </insert> <insert id="insertBatch"> INSERT INTO tb_student (name, age, phone, address, class_id) VALUES <foreach collection="list" separator="," item="item"> (#{item.name},#{item.age},#{item.phone},#{item.address},#{item.classId}) </foreach> </insert> </mapper>
mapper接口:
public interface StudentMapper { int insert(Student student); int insertBatch(List<Student> studentList); }
测试代码:
class DemoApplicationTests { private StudentMapper studentMapper; public void testInsertByForeachTag(){ //数据生成 List<Student> studentList = createData(100); //使用foreach标签,拼接SQL插入 long start = System.currentTimeMillis(); studentMapper.insertBatch(studentList); System.out.println(System.currentTimeMillis() - start); } private List<Student> createData(int size){ List<Student> studentList = new ArrayList<>(); Student student; for(int i = 0; i < size; i++){ student = new Student(); student.setName("小王" + i); student.setAge(18); student.setClassId(1); student.setPhone("1585xxxx669"); student.setAddress("未知"); studentList.add(student); } return studentList; } }
3. 批处理
测试代码:
class DemoApplicationTests { private SqlSessionFactory sqlSessionFactory; public void testInsertBatch(){ //数据生成 List<Student> studentList = createData(100); //使用批处理 long start = System.currentTimeMillis(); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false); StudentMapper studentMapperNew = sqlSession.getMapper(StudentMapper.class); studentList.stream().forEach(student -> studentMapperNew.insert(student)); sqlSession.commit(); sqlSession.clearCache(); System.out.println(System.currentTimeMillis() - start); } private List<Student> createData(int size){ List<Student> studentList = new ArrayList<>(); Student student; for(int i = 0; i < size; i++){ student = new Student(); student.setName("小王" + i); student.setAge(18); student.setClassId(1); student.setPhone("1585xxxx669"); student.setAddress("未知"); studentList.add(student); } return studentList; } }
三种方式的对比
MySQL服务器版本:5.6.4
其他依赖版本如下:
<project xmlns="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.0 https://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/> <!-- lookup parent from repository --> </parent> <groupId>com.buhe</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</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条 |
---|---|---|---|---|
循环插入 | 496ms | 3330ms | 15584ms | 33755ms |
foreach标签 | 268ms | 366ms | 392ms | 684ms |
批处理 | 222ms | 244ms | 364ms | 426ms |
三种方式中,批处理的方式效率是最高的,尤其是在数据量大的情况下尤为明显。
其次是foreach标签,foreach标签是通过拼接SQL语句的方式完成批量操作的。但是当拼接的SQL过多,导致SQL大小超过了MySQL服务器中max_allowed_packet变量的值时,会导致操作失败,抛出PacketTooBigException异常。
最后是循环插入的方式,这种方式在数据量小的时候可以使用,在数据量大的情况下效率要低很多。
原文:https://www.cnblogs.com/seve/p/14654104.html
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
JavaScript判断两个数组相等的四类方法
js如何操作video标签
React实战--利用甘特图和看板,强化Paas平
【记录】正则替换的偏方
前端下载 Blob 类型整理
抽象语法树AST必知必会
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程