Spring Boot JPA数据库选择

Lui*_*sco 11 postgresql jpa spring-boot

如何启动一个独立的Spring Boot JPA应用程序 - 而不是通过cli - 选择数据库来获取数据,例如localhost:5432/my_db; 或192.168.1.100:5432/our_db,或example.com:5432/their_db?

我当前使用application.properties文件中的一个文件,该文件包含:

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/my_db
spring.datasource.username=postgres
spring.datasource.password=postgres

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
Run Code Online (Sandbox Code Playgroud)

提前致谢

gyo*_*der 4

由于您可能还需要配置用户名和密码,因此我建议application-mydatasource.properties为每个数据源配置创建单独的文件。然后,您将根据设置活动配置文件激活要使用的数据源。您可以在application.properties( spring.profiles.active) 中或通过命令行参数设置活动配置文件:

$ java -jar -Dspring.profiles.active=mydatasource demo-0.0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

然后将application-mydatasource.properties覆盖您的application.properties. 我相信您还需要设置spring.profiles=可用的配置文件列表。

请参阅配置文件特定属性