Database application.yml for Spring boot from applications.properties

前端 未结 3 1358
萌比男神i
萌比男神i 2020-12-06 16:19

I\'ve got a working Spring Boot Application that connects to a Postgres database. I\'ve got the project set up with an application.properties file, but would like to make th

相关标签:
3条回答
  • 2020-12-06 17:01

    application.yml file for postgresql

    Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)

    spring:
      datasource:
        driver-class-name: org.postgresql.Driver
        username: postgres
        password: root
        url: jdbc:postgresql://localhost/postgres
        platform: postgres
        initialization-mode: always
        continue-on-error: true
      jpa:
        show-sql: true
        generate-ddl: true
        hibernate:
          ddl-auto: create
        database: postgresql
    
    0 讨论(0)
  • 2020-12-06 17:08

    Please upvote the other answer (Z0lt@n's answer)

    But pasting here for future readers... a sql server version.

    spring:
      jpa:
        show-sql: true
        hibernate:
          ddl-auto: update
        properties:
          hibernate.jdbc.batch_size: 20
          hibernate.cache.use_query_cache: false
          hibernate.cache.use_second_level_cache: false
          hibernate.cache.use_structured_entries: false
          hibernate.cache.use_minimal_puts: false
      datasource:
        #SPRING_DATASOURCE_URL environment variable will be something like -> jdbc:sqlserver://MySqlServer\\MyInstance:1433;DatabaseName=MyDbName;
        url: ${SPRING_DATASOURCE_URL}
        username: ${SPRING_DATASOURCE_USERNAME}
        password: ${SPRING_DATASOURCE_PASSWORD}
        driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
    

    and maven entry

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.0.0.jre8</version>
        </dependency>
    

    APPEND

    This seems to be the "standard" name for the class-name.

    SPRING_DATASOURCE_DRIVER-CLASS-NAME
    

    And of course, in my example, you would use:

    com.microsoft.sqlserver.jdbc.SQLServerDriver
    

    NOW, some spring, springboot, environment variable voodoo alert.

    Sometimes, when specifying the environment variable...for some command lines items, I would have to change the hyphens and make them underscores. (aka, "SPRING_DATASOURCE_DRIVER-CLASS-NAME" vs "SPRING_DATASOURCE_DRIVER_CLASS_NAME"

    below the -e generically represents "passing environment variable values via the command line"

    MyCommandLineProgram.exe -e SPRING_DATASOURCE_URL="jdbc:sqlserver://myServerName:1433;DatabaseName=MyDB;" -e SPRING_DATASOURCE_USERNAME="myUserName" -e SPRING_DATASOURCE_PASSWORD="myPassword" -e SPRING_DATASOURCE_DRIVER_CLASS_NAME="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    

    There's some voodoo for you.

    Those interested in the logging (logback.xml) issue, maybe want to find my answer here as well:

    Spring Boot Logback DB Appender Properties

    0 讨论(0)
  • 2020-12-06 17:16

    You need to treat each . character in property names as levels in the yaml file:

    spring:
      jpa:
        database: POSTGRESQL
        show-sql: true
        hibernate:
          ddl-auto: create-drop
      datasource:
        platform: postgres
        url: jdbc:postgresql://localhost:5432/mydb
        username: foo
        password: bar
        driverClassName: org.postgresql.Driver
    

    EDIT: edits have been suggested, thanks for that. The driverClassName property actually should be under spring.datasource. However, the purpose of this answer was to show how a properties file is converted into yaml format. So I have changed the driverClassName property to be at the right path, that is not part of the transformation from properties to yaml.

    0 讨论(0)
提交回复
热议问题