最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年04月20日 09:49:14   作者:布禾  
這篇文章主要介紹了Mybatis的三種批量插入方式,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下

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

測試代碼:

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

測試代碼:

@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. 批處理

測試代碼:

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

三種方式的對比

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變量的值時,會導(dǎo)致操作失敗,拋出PacketTooBigException異常。

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

以上就是Mybatis的三種批量插入方式的詳細內(nèi)容,更多關(guān)于Mybatis 批量插入的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用java代碼代替xml實現(xiàn)SSM教程

    使用java代碼代替xml實現(xiàn)SSM教程

    這篇文章主要介紹了使用java代碼代替xml實現(xiàn)SSM教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • mybatis中使用oracle關(guān)鍵字出錯的解決方法

    mybatis中使用oracle關(guān)鍵字出錯的解決方法

    這篇文章主要給大家介紹了關(guān)于mybatis中使用oracle關(guān)鍵字出錯的解決方法,文中通過示例代碼將解決的方法介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-08-08
  • spring如何使用命名空間p簡化bean的配置

    spring如何使用命名空間p簡化bean的配置

    這篇文章主要介紹了spring如何使用命名空間p簡化bean的配置,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • Spring與bean有關(guān)的生命周期示例詳解

    Spring與bean有關(guān)的生命周期示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring與bean有關(guān)的生命周期的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • spring @profile注解的使用方法

    spring @profile注解的使用方法

    本篇文章主要介紹了spring @profile注解的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Java實現(xiàn)post請求詳細代碼(帶有參數(shù))

    Java實現(xiàn)post請求詳細代碼(帶有參數(shù))

    這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)帶有參數(shù)post請求的相關(guān)資料,文中通過代碼示例介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-08-08
  • SpringBoot指定激活配置文件的方法

    SpringBoot指定激活配置文件的方法

    Spring Boot 對多環(huán)境整合已經(jīng)有了很好的支持,能夠在運行間、打包時自由切換環(huán)境,這篇文章主要介紹了SpringBoot指定激活配置文件,需要的朋友可以參考下
    2023-11-11
  • java判斷請求是來自PC端還是手機端小技巧

    java判斷請求是來自PC端還是手機端小技巧

    這篇文章主要為大家介紹了java判斷請求是來自PC端還是手機端小技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • SpringBoot+MDC實現(xiàn)鏈路調(diào)用日志的方法

    SpringBoot+MDC實現(xiàn)鏈路調(diào)用日志的方法

    MDC是 log4j 、logback及l(fā)og4j2 提供的一種方便在多線程條件下記錄日志的功能,這篇文章主要介紹了SpringBoot+MDC實現(xiàn)鏈路調(diào)用日志,需要的朋友可以參考下
    2022-12-12
  • JAVAEE model1模型實現(xiàn)商品瀏覽記錄(去除重復(fù)的瀏覽記錄)(一)

    JAVAEE model1模型實現(xiàn)商品瀏覽記錄(去除重復(fù)的瀏覽記錄)(一)

    這篇文章主要為大家詳細介紹了JAVAEE model1模型實現(xiàn)商品瀏覽記錄,去除重復(fù)的瀏覽記錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11

最新評論

方山县| 大关县| 买车| 北京市| 江油市| 大厂| 思茅市| 沁阳市| 通江县| 石河子市| 融水| 沙田区| 隆子县| 都江堰市| 普安县| 万年县| 长海县| 九江县| 曲松县| 福海县| 黑山县| 湖州市| 武清区| 东阿县| 伽师县| 平阳县| 海盐县| 康平县| 行唐县| 那坡县| 房山区| 天水市| 泰兴市| 吴桥县| 章丘市| 左云县| 东莞市| 周至县| 德庆县| 西吉县| 高雄县|