Spring 启动无法在 Spring 启动时选择 schema.sql 文件 运行
Spring Boot unable to pick schema.sql file at the start of the Spring Boot run
我已经完成了 link:https://walkingtechie.blogspot.com/2018/12/execute-schema-and-data-sql-on-startup-spring-boot.html and Spring Boot - Loading Initial Data,但仍然难以理解一些事情。
我希望在 Spring 引导应用程序开始时执行 schema.sql
运行,为此我使用以下配置。我希望一切都在 Postgres DB 上执行。
我在 sql/schema.sql 文件夹中创建了 schema.sql 文件位置。我希望 spring Boot 能够拾取该文件并 运行 它。如果我们能做到,有什么办法吗?我正在使用 Spring Boot
版本 2.1.6.RELEASE
和 Postgres 11.0
.
spring:
datasource:
url: jdbc:postgresql://localhost:5432/test?currentSchema=test
username: postgres
password: postgres
platform: postgres
schema:
- classpath:sql/schema.sql
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
default_schema: test
format_sql: true
jdbc:
lob:
non_contextual_creation: true
show-sql: true
hibernate:
ddl-auto: none
我能够使用以下代码自行解决此问题。
User.java
@Data
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
private String name;
}
DataSqlApplication.java
@SpringBootApplication
public class DataSqlApplication {
public static void main(String[] args) {
SpringApplication.run(DataSqlApplication.class, args);
}
}
application.yml
---
spring:
datasource:
url: jdbc:postgresql://localhost:5432/test?currentSchema=test
username: postgres
password: admin
platform: postgres
initialization-mode: always
schema:
- classpath:sql/schema.sql
data:
- classpath:sql/data.sql
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
properties:
hibernate:
default_schema: test
format_sql: true
jdbc:
lob:
non_contextual_creation: true
show-sql: true
hibernate:
ddl-auto: none
generate-ddl: false
profiles:
active:
- local
# Logging
logging:
level:
org.springframework.data: debug
我已经完成了 link:https://walkingtechie.blogspot.com/2018/12/execute-schema-and-data-sql-on-startup-spring-boot.html and Spring Boot - Loading Initial Data,但仍然难以理解一些事情。
我希望在 Spring 引导应用程序开始时执行 schema.sql
运行,为此我使用以下配置。我希望一切都在 Postgres DB 上执行。
我在 sql/schema.sql 文件夹中创建了 schema.sql 文件位置。我希望 spring Boot 能够拾取该文件并 运行 它。如果我们能做到,有什么办法吗?我正在使用 Spring Boot
版本 2.1.6.RELEASE
和 Postgres 11.0
.
spring:
datasource:
url: jdbc:postgresql://localhost:5432/test?currentSchema=test
username: postgres
password: postgres
platform: postgres
schema:
- classpath:sql/schema.sql
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
default_schema: test
format_sql: true
jdbc:
lob:
non_contextual_creation: true
show-sql: true
hibernate:
ddl-auto: none
我能够使用以下代码自行解决此问题。
User.java
@Data
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
private String name;
}
DataSqlApplication.java
@SpringBootApplication
public class DataSqlApplication {
public static void main(String[] args) {
SpringApplication.run(DataSqlApplication.class, args);
}
}
application.yml
---
spring:
datasource:
url: jdbc:postgresql://localhost:5432/test?currentSchema=test
username: postgres
password: admin
platform: postgres
initialization-mode: always
schema:
- classpath:sql/schema.sql
data:
- classpath:sql/data.sql
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
properties:
hibernate:
default_schema: test
format_sql: true
jdbc:
lob:
non_contextual_creation: true
show-sql: true
hibernate:
ddl-auto: none
generate-ddl: false
profiles:
active:
- local
# Logging
logging:
level:
org.springframework.data: debug