但是大容量插入不起作用。
型号:
@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数据库中测试批量插入?或者这是可能的?
发布于 2021-02-28 20:39:27
这里不是spring boot负责批量插入,它是hiberate (或者您正在使用的jpa提供程序),它只是在spring应用程序中运行。
Spring boot为您提供了配置hibernate的方法和配置键,但本质上您正在配置的是hibernate。
使hibernate批处理成为插件的原因是:
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=truehttps://stackoverflow.com/questions/66409050
复制相似问题