itsource

applications.properties에서 스프링 부팅을 위한 데이터베이스 application.yml

mycopycode 2023. 2. 26. 09:44
반응형

applications.properties에서 스프링 부팅을 위한 데이터베이스 application.yml

Postgres 데이터베이스에 접속하는 Spring Boot 어플리케이션이 동작하고 있다.application.properties 파일로 프로젝트를 셋업했는데 application.yml 파일로 전환하고 싶습니다.단, 전환 시 응용 프로그램이 DB에 접속하려고 할 때 오류가 발생합니다.

원래 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

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

파일 형식 간 변환에서 누락된 부분이 있습니까?

한 사람 한 사람 치료해야 한다.속성 이름의 문자는 의 레벨로 지정됩니다.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

편집: 편집이 제안되었습니다. 감사합니다.driverClassName부동산은 실제로 아래에 있어야 한다.spring.datasource하지만, 이 답변의 목적은 이 답변이 얼마나 중요한지 보여주는 것이었다.properties파일이 로 변환되다yaml포맷합니다.그래서 제가 변경했습니다.driverClassName올바른 경로에 있어야 하는 속성, 즉 변환의 일부가 아닙니다.properties로.yaml.

다른 답변(Z0lt@n의 답변)을 상향 투표하십시오.

하지만 미래의 독자들을 위해 여기에 붙이는 것은...sql 서버 버전

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

및 maven 엔트리

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

추가

이것은 driver Class Name의 "표준" 이름인 것 같습니다.

SPRING_DATASOURCE_DRIVER-CLASS-NAME

물론 이 예에서는 다음과 같은 가치를 사용합니다.

com.microsoft.sqlserver.jdbc.SQLServerDriver

봄, 봄부츠, 환경변수 부두경보입니다

환경변수를 지정할 때...일부 명령줄 항목의 경우 하이픈을 변경하고 밑줄을 그어야 합니다.('Spring_DATASOURCE_DRIVER-CLASS-NAME'과 'Spring_DATASOURCE_DRIVER_CLASS_NAME'이라고도 함)

아래 -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"

부두교 선물이야

로깅(logback.xml) 문제에 관심이 있는 분은 여기에서 답변을 찾을 수도 있습니다.

스프링 부트 로그백 DB Appender 속성

postgresql용 application.yml 파일

스프링 데이터 소스(Data Source Auto Configuration 및 Data Spring Data Source

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

서버:
포트: 1111

스프링:
응용 프로그램:
이름: client-1

데이터 소스:
비밀번호: postgres
url: jdbc:postgresql://localhost:5432/postgres?currentSchema=education
사용자 이름: postgres
jpa:
휴지 상태:
ddl-auto: 갱신
★★★★
지지: :
org.intermate.인터메이트dLDlex
show-sql: true

언급URL : https://stackoverflow.com/questions/33323837/database-application-yml-for-spring-boot-from-applications-properties

반응형