久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

Mybatis三種批量插入數(shù)據(jù)的方式

瀏覽:58日期:2023-10-19 13:17:07
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 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(){//數(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 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(){//數(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. 批處理

測試代碼:

@SpringBootTestclass DemoApplicationTests {@Autowiredprivate SqlSessionFactory sqlSessionFactory;@Testpublic 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;}}三種方式的對比

MySQL服務(wù)器版本: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>

三種插入方式在不同數(shù)據(jù)量下的表現(xiàn),測試結(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)簽是通過拼接SQL語句的方式完成批量操作的。但是當(dāng)拼接的SQL過多,導(dǎo)致SQL大小超過了MySQL服務(wù)器中max_allowed_packet變量的值時(shí),會(huì)導(dǎo)致操作失敗,拋出PacketTooBigException異常。

最后是循環(huán)插入的方式,這種方式在數(shù)據(jù)量小的時(shí)候可以使用,在數(shù)據(jù)量大的情況下效率要低很多。

以上就是Mybatis的三種批量插入方式的詳細(xì)內(nèi)容,更多關(guān)于Mybatis 批量插入的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 色网站视频| 亚洲精品久久久狠狠狠爱 | 一区二区三区成人久久爱 | 久久国产综合 | 国产小视频在线 | 久久中文字幕视频 | 免费视频二区 | 伊人色综合久久天天五月婷 | 一区国产视频 | 9色porny自拍视频一区二区 | 亚洲一区二区中文字幕 | 国产区视频 | 国产精品久久久久久久久久免费 | 日韩欧美国产一区二区三区 | 欧美色视频在线观看 | 国产精品久久久久久久久久久新郎 | 午夜午夜精品一区二区三区文 | 欧美夜夜爽| 成人在线免费视频 | 中文天堂在线观看视频 | 亚洲国产精品一区 | 午夜私人影院在线观看 | 精品国产一区二区在线 | 激情久久av一区av二区av三区 | www.久久爱.cn| 中文字幕1区 | 欧美一区永久视频免费观看 | 国产在线网 | 中文字幕一区二区三区乱码图片 | 免费午夜电影 | 青青操av | 免费av一区二区三区 | 免费黄在线观看 | 成年人精品视频 | 在线久草| 欧美中文在线 | 亚洲精品一区久久久久久 | 狠狠操电影 | 91亚洲视频| 亚洲视频在线观看视频 | 国产精品久久免费观看spa |