且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在应用程序启动时运行SQL脚本并获取数据?

更新时间:2023-01-30 08:13:22

默认情况下,Spring-Boot加载data.sql和/或data-${platform}.sql.

By default, Spring-Boot loads data.sql and/or data-${platform}.sql.

但是,请记住,该脚本将在每次启动时加载,因此,我认为(至少对于生产而言)有意义的是,只保留数据库中已经存在的值,而不是每次都重新插入开始. 我个人仅在使用内存数据库时将数据库初始化用于测试/开发目的.不过,这是Spring-Boot提供的功能.

However, keep in mind that the script would be loaded at every start, so I would think it makes more sense (at least for production), to just have the values already present in the database, not re-inserted at every start. I've personally only used database initialization for test/dev purposes when using a memory database. Still, this is the feature provided by Spring-Boot.

来源: spring-boot- howto-数据库初始化:

Spring JDBC具有数据源初始化器功能. Spring Boot启用 默认情况下,它会从标准位置schema.sql和 data.sql(在类路径的根目录中).此外,Spring Boot将 加载架构$$ {platform} .sql和数据$$ {platform} .sql文件(如果 礼物).

Spring JDBC has a DataSource initializer feature. Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql (in the root of the classpath). In addition Spring Boot will load the schema-${platform}.sql and data-${platform}.sql files (if present).

src/main/resources/ data-oracle.sql:

src/main/resources/data-oracle.sql:

insert into...
insert into...