Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 901 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Database application.yml for Spring boot from applications.properties

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

Original applications.properties file:

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:

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?
Reply

#2
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`.
Reply

#3
**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
```
Reply

#4
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 driverClassName.

SPRING_DATASOURCE_DRIVER-CLASS-NAME

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

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:

[To see links please register here]

Reply

#5
~~~
server:
port: 1111
spring:
application:
name: client-one
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/postgres?currentSchema=education
username: postgres
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true

~~~
Reply

#6
File `application.yml`


```yml
server:
port: 1234
spring:
application:
name: foo
datasource:
url: jdbc:postgresql://localhost:5432/foo
password: postgres
username: postgres
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true
```

Test with PostgreSQL 15.3 , JDK 20, Spring Boot 3.1.0 .
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through