Spring Boot with Multiple SQL Import Files – 带有多个SQL导入文件的Spring Boot

最后修改: 2018年 7月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring Boot allows us to import sample data into our database – mainly to prepare data for integration tests. Out of the box, there are two possibilities. We can use import.sql (Hibernate support) or data.sql (Spring JDBC support) files to load data.

Spring Boot允许我们将样本数据导入数据库–主要是为集成测试准备数据。开箱即用,有两种可能性。我们可以使用import.sql(支持Hibernate)或data.sql(支持Spring JDBC)文件来加载数据

However, sometimes we want to split one big SQL file into a few smaller ones, e.g., for better readability or to share some files with an init data between modules.

然而,有时我们想把一个大的SQL文件分割成几个小的文件,例如,为了提高可读性,或者在模块之间共享一些带有初始数据的文件。

In this tutorial, we’ll show how to do it with both – Hibernate and Spring JDBC.

在本教程中,我们将展示如何同时使用Hibernate和Spring JDBC。

2. Hibernate Support

2.休眠支持

We can define files which contain sample data to load with a property spring.jpa.properties.hibernate.hbm2ddl.import_files. It can be set in the application.properties file inside the test resources folder.

我们可以用一个属性spring.jpa.properties.hibernate.hbm2ddl.import_files来定义包含样本数据的文件来加载。它可以在测试资源文件夹内的application.properties文件中设置。

This is in a case we want to load sample data just for JUnit tests. The value has to be a comma-separated list of files to import:

这是在我们想为JUnit测试加载样本数据的情况下。该值必须是一个以逗号分隔的要导入的文件的列表。

spring.jpa.properties.hibernate.hbm2ddl.import_files=import_active_users.sql,import_inactive_users.sql

This configuration will load sample data from two files: import_active_users.sql and import_inactive_users.sql. Important to mention here is that we have to use prefix spring.jpa.properties to pass values (JPA configuration) to the EntityManagerFactory.

这个配置将从两个文件中加载样本数据。import_active_users.sqlimport_inactive_users.sql。这里需要提到的是,我们必须使用前缀spring.jpa.properties来传递值(JPA配置) 到EntityManagerFactory

Next, we’ll show how we can do it with the Spring JDBC support.

接下来,我们将展示如何通过Spring JDBC支持来实现。

3. Spring JDBC Support

3.Spring JDBC支持

The configuration for initial data and Spring JDBC support is very similar to Hibernate. We have to use the spring.sql.init.data-locations property:

初始数据和Spring JDBC支持的配置与Hibernate非常相似。我们必须使用spring.sql.init.data-locations属性。

spring.sql.init.data-locations=import_active_users.sql,import_inactive_users.sql

Setting the value as above gives the same results as in the Hibernate support. However, a significant advantage of this solution is the possibility to define value using an Ant-style pattern:

按上述方法设置值,得到的结果与Hibernate支持中的结果相同。然而,这个解决方案的一个显著优势是可以使用Ant风格的模式来定义值

spring.sql.init.data-locations=import_*_users.sql

The above value tells the Spring to search for all files with a name that matches import_*_users.sql pattern and import data which is inside.

上述值告诉Spring搜索所有名称与import_*_users.sql模式匹配的文件,并导入其中的数据。

This property was introduced in Spring Boot 2.5.0; in earlier versions of Spring Boot, we need to use the spring.datasource.data property.

这个属性是在Spring Boot 2.5.0中引入的;在Spring Boot的早期版本中,我们需要使用spring.datasource.data property。

4. Conclusion

4.结论

In this short article, we showed how to configure a Spring Boot application to load initial data from custom SQL files.

在这篇短文中,我们展示了如何配置Spring Boot应用程序以从自定义SQL文件中加载初始数据。

Finally, we showed two possibilities – Hibernate and Spring JDBC. They both work pretty well, and it’s up to the developer which one to chose.

最后,我们展示了两种可能性–Hibernate和Spring JDBC。它们都很好用,至于选择哪一种,则取决于开发者。

As always, the complete code examples used in this article are available over on Github.

像往常一样,本文中使用的完整代码示例可在Github上获得