Mybatis三種批量插入數(shù)據(jù)的方式
1. 循環(huán)插入
mapper.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <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); }
測(cè)試代碼:
@SpringBootTest class DemoApplicationTests { @Resource private StudentMapper studentMapper; @Test public void testInsert(){ //數(shù)據(jù)生成 List<Student> studentList = createData(100); //循環(huán)插入 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標(biāo)簽
mapper.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <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); }
測(cè)試代碼:
@SpringBootTest class DemoApplicationTests { @Resource private StudentMapper studentMapper; @Test public void testInsertByForeachTag(){ //數(shù)據(jù)生成 List<Student> studentList = createData(100); //使用foreach標(biāo)簽,拼接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. 批處理
測(cè)試代碼:
@SpringBootTest class DemoApplicationTests { @Autowired private SqlSessionFactory sqlSessionFactory; @Test public void testInsertBatch(){ //數(shù)據(jù)生成 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; } }
三種方式的對(duì)比
MySQL服務(wù)器版本:5.6.4
其他依賴(lài)版本如下:
<?xml version="1.0" encoding="UTF-8"?> <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>
三種插入方式在不同數(shù)據(jù)量下的表現(xiàn),測(cè)試結(jié)果:
插入方式 | 10條 | 100條 | 500條 | 1000條 |
---|---|---|---|---|
循環(huán)插入 | 496ms | 3330ms | 15584ms | 33755ms |
foreach標(biāo)簽 | 268ms | 366ms | 392ms | 684ms |
批處理 | 222ms | 244ms | 364ms | 426ms |
三種方式中,批處理的方式效率是最高的,尤其是在數(shù)據(jù)量大的情況下尤為明顯。
其次是foreach標(biāo)簽,foreach標(biāo)簽是通過(guò)拼接SQL語(yǔ)句的方式完成批量操作的。但是當(dāng)拼接的SQL過(guò)多,導(dǎo)致SQL大小超過(guò)了MySQL服務(wù)器中max_allowed_packet變量的值時(shí),會(huì)導(dǎo)致操作失敗,拋出PacketTooBigException異常。
最后是循環(huán)插入的方式,這種方式在數(shù)據(jù)量小的時(shí)候可以使用,在數(shù)據(jù)量大的情況下效率要低很多。
以上就是Mybatis的三種批量插入方式的詳細(xì)內(nèi)容,更多關(guān)于Mybatis 批量插入的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- mybatis中批量插入的兩種方式(高效插入)
- MyBatis批量插入(insert)數(shù)據(jù)操作
- MyBatis-Plus 批量插入數(shù)據(jù)的操作方法
- MyBatis批量插入大量數(shù)據(jù)(1w以上)
- MyBatis批量插入數(shù)據(jù)的三種方法實(shí)例
- 給你的MyBatis-Plus裝上批量插入的翅膀(推薦)
- MyBatis批量插入數(shù)據(jù)到Oracle數(shù)據(jù)庫(kù)中的兩種方式(實(shí)例代碼)
- Mybatis-plus 批量插入太慢的問(wèn)題解決(提升插入性能)
- mybatis插入與批量插入返回ID的原理詳解
- Mybatis批量插入的三種實(shí)現(xiàn)方法
相關(guān)文章
Java基于直方圖應(yīng)用的相似圖片識(shí)別實(shí)例
這篇文章主要介紹了Java基于直方圖應(yīng)用的相似圖片識(shí)別實(shí)例,是非常實(shí)用的技巧,多見(jiàn)于圖形里游戲中,需要的朋友可以參考下2014-09-09springBoot下實(shí)現(xiàn)java自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)表
這篇文章主要介紹了springBoot下實(shí)現(xiàn)java自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)表的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java并發(fā)編程之ConcurrentLinkedQueue隊(duì)列詳情
這篇文章主要介紹了Java并發(fā)編程之ConcurrentLinkedQueue隊(duì)列詳情,ConcurrentLinkedQueue?內(nèi)部的隊(duì)列使用單向鏈表方式實(shí)現(xiàn),下文更多相關(guān)內(nèi)容敘述需要的小伙伴可以參考一下2022-04-04Java實(shí)現(xiàn)的百度語(yǔ)音識(shí)別功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的百度語(yǔ)音識(shí)別功能,較為簡(jiǎn)明扼要的分析了Java調(diào)用百度語(yǔ)音接口相關(guān)操作步驟,并給出了具體的語(yǔ)音識(shí)別用法代碼示例,需要的朋友可以參考下2018-08-08MyBatis 添加元數(shù)據(jù)自定義元素標(biāo)簽的實(shí)現(xiàn)代碼
這篇文章主要介紹了MyBatis 添加元數(shù)據(jù)自定義元素標(biāo)簽的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07macOS下Spring Boot開(kāi)發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了macOS下Spring Boot開(kāi)發(fā)環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Java代理模式實(shí)例詳解【靜態(tài)代理與動(dòng)態(tài)代理】
這篇文章主要介紹了Java代理模式,結(jié)合實(shí)例形式詳細(xì)分析了java靜態(tài)代理與動(dòng)態(tài)代理模式相關(guān)概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-09-09