Bulk Insert with H2 Database on Spring Boot



我遵循了这个示例(如何使用jparerepository进行批量(多行)插入?)并创建了一个h2-数据库示例案例。但是大容量插入不能工作

模型:

@Table(name = "user")
@Entity
public class User
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
}

存储库:

public interface IUserRepository extends JpaRepository<User, Long>
{
}
测试:

@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
@Bean
ApplicationRunner init(IUserRepository userRepository)
{
return args -> {
User user = new User();
user.setName("Test-1");
User user1 = new User();
user1.setName("Test-2");
userRepository.saveAll(Arrays.asList(user, user1));
};
}
}

属性:

spring.datasource.url=jdbc:h2:~/test;TRACE_LEVEL_FIle=4
spring.jpa.hibernate.ddl-auto=create
spring.datasource.username=sa
spring.jpa.properties.hibernate.jdbc.batch_size=5

输出:

/*SQL #:1*/call next value for hibernate_sequence;
2021-02-28 15:13:35.110  INFO 43465 --- [           main] h2database                               
/*SQL l:58 #:1*/SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=? {1: 'QUERY_TIMEOUT'};
2021-02-28 15:13:35.119  INFO 43465 --- [           main] h2database                               
/*SQL #:1*/call next value for hibernate_sequence;
2021-02-28 15:13:35.136  INFO 43465 --- [           main] h2database                               
/*SQL l:41 #:1*/insert into user (name, id) values (?, ?) {1: 'Test-1', 2: 1};
2021-02-28 15:13:35.137  INFO 43465 --- [           main] h2database                                
/*SQL l:41 #:1*/insert into user (name, id) values (?, ?) {1: 'Test-2', 2: 2};
2021-02-28 15:13:35.139  INFO 43465 --- [           main] h2database                               
/*SQL */COMMIT;
2021-02-28 15:13:35.139  INFO 43465 --- [           main] h2database                               
/*SQL */COMMIT;

如何使用Spring-Boot在h2-database中测试大容量插入?或者这是可能的吗?

这里不是spring boot负责批量插入,它是hibernate(或您正在使用的jpa提供程序),它只是发生,以便它在spring but应用程序中运行。

Spring boot为您提供了配置hibernate的方法和配置键,但本质上,您正在配置的是hibernate。

如何使hibernate批处理insert:

spring.jpa.properties.hibernate.jdbc.batch_size=5
spring.jpa.properties.hibernate.order_inserts=true

更新:

spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.batch_versioned_data=true

最新更新