postgresql Spring Boot JPA 数据库选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25530917/
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
Spring Boot JPA Database Choice
提问by Luisa Francisco
How can I start a stand-alone Spring Boot JPA application -- not via cli -- with a choice of databases to get data, e.g., localhost:5432/my_db; or 192.168.1.100:5432/our_db, or example.com:5432/their_db?
我如何启动一个独立的 Spring Boot JPA 应用程序——而不是通过 cli——选择数据库来获取数据,例如,localhost:5432/my_db;或 192.168.1.100:5432/our_db,或 example.com:5432/their_db?
Mine currently uses the one in the application.properties file that contains:
我的当前使用 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
Thanks in advance
提前致谢
采纳答案by gyoder
Since you probably need to configure username and password as well, I recommend creating separate application-mydatasource.properties
files for each data source configuration. You will then activate the datasource you want to use based on setting the active profile. You can set the active profile either in application.properties
(spring.profiles.active
) or via a command line argument:
由于您可能还需要配置用户名和密码,我建议application-mydatasource.properties
为每个数据源配置创建单独的文件。然后,您将根据设置的活动配置文件激活要使用的数据源。您可以在application.properties
( spring.profiles.active
) 中或通过命令行参数设置活动配置文件:
$ java -jar -Dspring.profiles.active=mydatasource demo-0.0.1-SNAPSHOT.jar
The application-mydatasource.properties
will then override any properties in your application.properties
. I believe you will also need to set spring.profiles=
to the list of profiles available.
然后application-mydatasource.properties
将覆盖您的application.properties
. 我相信您还需要设置spring.profiles=
可用的配置文件列表。
See Profile specific properties.
请参阅配置文件特定属性。
回答by a.zaragoza.ib
Another options besides the @Profile label, that you will have to declare in every enviroment that you will deploy the application, you could use in Spring Boot the label:
除了@Profile 标签之外的另一个选项,您必须在将部署应用程序的每个环境中声明,您可以在 Spring Boot 中使用标签:
@ConditionalOnProperty(name="propertyName", havingValue="propertyValue")
@ConditionalOnProperty(name="propertyName", havingValue="propertyValue")
And declare a property to decide wich database you want to load in each case!
并声明一个属性来决定在每种情况下要加载的数据库!
Hope being helping!!
希望有所帮助!!