国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Mybatis三種批量插入數據的方式

瀏覽:47日期:2023-10-19 13:17:07
1. 循環插入

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

測試代碼:

@SpringBootTestclass DemoApplicationTests {@Resourceprivate StudentMapper studentMapper;@Testpublic 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:

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

測試代碼:

@SpringBootTestclass DemoApplicationTests {@Resourceprivate StudentMapper studentMapper;@Testpublic 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. 批處理

測試代碼:

@SpringBootTestclass DemoApplicationTests {@Autowiredprivate SqlSessionFactory sqlSessionFactory;@Testpublic 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

其他依賴版本如下:

<?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>

三種插入方式在不同數據量下的表現,測試結果:

插入方式 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異常。

最后是循環插入的方式,這種方式在數據量小的時候可以使用,在數據量大的情況下效率要低很多。

以上就是Mybatis的三種批量插入方式的詳細內容,更多關于Mybatis 批量插入的資料請關注好吧啦網其它相關文章!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 99国产成人高清在线视频 | 国产福利久久 | 在线观看视频一区 | 国产精品久久久久久一级毛片 | 极品丝袜高跟91白沙发在线 | 91国语精品自产拍在线观看一 | 亚洲成年男人的天堂网 | 91国偷自产一区二区三区 | 欧美日本一区二区三区道 | 欧美日本视频一区 | 夜鲁夜鲁夜鲁在线观看福利 | 欧美日韩综合高清一区二区 | 日本特级淫片免费 | 日本一区二区高清不卡 | 成年人在线免费网站 | a一级毛片视频免费看 | 在线日韩视频 | 国产成人精品一区二三区 | 欧美色久| 精品 日韩 国产 欧美在线观看 | 亚州精品一区二区三区 | 狠狠色丁香婷婷综合小时婷婷 | 国产91香蕉| 国产成a人亚洲精v品久久网 | 免费三级网址 | 日韩一级黄色毛片 | 九九久久精品这里久久网 | a级毛片在线免费看 | 欧美不卡视频在线观看 | 欧美在线观看成人高清视频 | 欧美在线日韩在线 | 99re免费99re在线视频手机版 | 女同日韩互慰互摸在线观看 | 亚洲精品欧美精品国产精品 | 好妞操| 在线观看中文字幕国产 | 国产欧美一区二区久久 | 日韩精品一区二区三区在线观看 | 亚洲在线免费 | 国产成人综合怡春院精品 | 日韩专区亚洲综合久久 |