从 Applications.properties 启动 Spring 的数据库 application.yml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33323837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:50:25  来源:igfitidea点击:

Database application.yml for Spring boot from applications.properties

springspring-bootdatabase-connection

提问by user3051261

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 the switch over to an application.yml file. However when I make the switch, my application errors out while attempting to connect to the db.

我有一个连接到 Postgres 数据库的有效 Spring Boot 应用程序。我已经使用 application.properties 文件设置了项目,但希望切换到 application.yml 文件。但是,当我进行切换时,我的应用程序在尝试连接到数据库时出错。

Original applications.properties file:

原始 applications.properties 文件:

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

And Here's what I've got so far in the application.yml file:

这是我目前在 application.yml 文件中的内容:

spring.jpa:
  database: POSTGRESQL
  hibernate.ddl-auto: create-drop
  show-sql: true

spring.datasource:
  platform: postgres
  driverClassName: org.postgresql.Driver
  url: jdbc:postgresql://localhost:5432/mydb
  username: foo
  password: bar

Am I missing something in the translation between file types?

我在文件类型之间的转换中遗漏了什么吗?

回答by Zoltan

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

您需要将.属性名称中的每个字符视为yaml文件中的级别:

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 driverClassNameproperty actually should be under spring.datasource. However, the purpose of this answer was to show how a propertiesfile is converted into yamlformat. So I have changed the driverClassNameproperty to be at the right path, that is not part of the transformation from propertiesto yaml.

编辑:已建议编辑,谢谢。该driverClassName属性实际上应该在spring.datasource. 但是,此答案的目的是展示如何将properties文件转换为yaml格式。所以我已将driverClassName属性更改为正确的路径,这不是从properties到的转换的一部分yaml

回答by granadaCoder

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

请为另一个答案点赞(Z0lt@n 的答案)

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

但是为了将来的读者粘贴在这里......一个sql server版本。

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

和 Maven 入口

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

现在,一些 spring、springboot、环境变量 voodoo 警报。

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"

有时,在指定环境变量时...对于某些命令行项目,我必须更改连字符并使其成为下划线。(又名,“SPRING_DATASOURCE_DRIVER-CLASS-NAME”与“SPRING_DATASOURCE_DRIVER_CLASS_NAME”

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

-e 下面的一般表示“通过命令行传递环境变量值”

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:

那些对日志记录 (logback.xml) 问题感兴趣的人,也许也想在这里找到我的答案:

Spring Boot Logback DB Appender Properties

Spring Boot Logback DB Appender 属性