【问题标题】:Spring Boot unable to pick schema.sql file at the start of the Spring Boot runSpring Boot 无法在 Spring Boot 运行开始时选择 schema.sql 文件
【发布时间】:2019-07-10 07:04:01
【问题描述】:

我已经浏览了链接:https://walkingtechie.blogspot.com/2018/12/execute-schema-and-data-sql-on-startup-spring-boot.htmlSpring Boot - Loading Initial Data,但仍然难以理解一些事情。

我希望在 Spring Boot 应用程序 运行开始时执行 schema.sql,因为我使用以下配置。我希望在 Postgres DB 上执行所有操作。

我在 sql/schema.sql 文件夹中创建了 schema.sql 文件。我希望 Spring Boot 能够拾取该文件并运行它。如果我们能做到,有什么办法吗?我正在使用Spring Boot 版本2.1.6.RELEASEPostgres 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

【问题讨论】:

    标签: postgresql spring-boot initialization


    【解决方案1】:

    我可以通过使用下面的代码自己解决这个问题。

    用户.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
    

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 2018-10-03
      • 1970-01-01
      • 2015-03-11
      • 2015-12-30
      • 2021-10-06
      • 2015-09-30
      • 2014-11-28
      • 1970-01-01
      相关资源
      最近更新 更多