我正在学习有关Springboot的知识,并试图连接到DB2数据库。我的工作正常。
以下是我的工作DB2属性:
spring.datasource.url=jdbc:db2://server:port/database:currentSchema=schema-name;
spring.datasource.username=user1
spring.datasource.password=password1
但是我将它们重命名为“ DB2”,而是“ spring”,就像:
db2.datasource.url=jdbc:db2://server:port/database:currentSchema=schema-name;
db2.datasource.username=user1
db2.datasource.password=password1
我的应用程序仍然运行,当我这样做时,我的控制器不再像重命名之前那样返回结果。
我问这样的原因是,如果我将来添加第二个数据源,如果我这样命名,我可以通过其数据源很容易区分属性。
更新:
感谢@kosta tenasis在下面的答案和本文( https://www.javadevjournal.com/spring-boot/multiple-data-sources-with-sring-spring-boot/ ) 。
对数据源进行配置,就可以修改应用程序
db2.datasource.url=...
。
spring.datasource.url=...
然后回到我的特定问题,一旦您 更改为使用hikari和hikari没有 url
属性,而是使用 jdbc-url
,因此只需
db2.datasource.jdbc-url=...
在数据源中更改以上更改为: note2:您必须在项目中添加多个数据源时必须创建,您将有注释 @ConfigurationProperties
。此注释需要指向您更新的应用程序。数据源( db2.datasource.url
)。
I am learning about springboot and trying to connect to a DB2 database. I got that working just fine.
Below are my working DB2 properties:
spring.datasource.url=jdbc:db2://server:port/database:currentSchema=schema-name;
spring.datasource.username=user1
spring.datasource.password=password1
But I renamed them to start with "db2" instead "spring" like:
db2.datasource.url=jdbc:db2://server:port/database:currentSchema=schema-name;
db2.datasource.username=user1
db2.datasource.password=password1
My app still runs, hHowever, when I do that, my controllers no longer return results as they did before the rename.
The reason I ask this is that if I add 2nd data source in the future, I could distinguish easily properties by their data sources if I name them like this.
UPDATE:
Thanks to @Kosta Tenasis answer below and this article (https://www.javadevjournal.com/spring-boot/multiple-data-sources-with-spring-boot/), I was able to resolve and figure this out.
Then going back to my specific question, once you have the configuration for data source in place, you can then modify application.properties to have:
db2.datasource.url=...
instead of having:
spring.datasource.url=...
NOTE1: if you are using Springboot 2.0, they changed to use Hikari and Hikari does not have url
property but instead uses jdbc-url
, so just change above to:
db2.datasource.jdbc-url=...
NOTE2: In your datasource that you had to create when adding multiple datasources to your project, you will have annotation @ConfigurationProperties
. This annotation needs to point to your updated application.properties for datasource (the db2.datasource.url
).
发布评论
评论(1)
默认情况下,Spring寻找
Spring.datasource。
因此,您可能会获得错误的结果,因为您没有连接到数据库。如果要配置不同的数据源,则可以从默认值中,您可以这样做的属性
,假设一天来了,您想要第二个
datasource
,您可以将上一类修改为类似的类别:之后,在每个
class
中,您想要dataSource
您可以指定您喜欢的bean:因此,默认情况下,Spring会寻找
spring.datasource.url(或spring.datasource.jdbc-url)
spring.datasource.username
spring.datasource.passource.password
如果您指定另一个
dataSource
of DataSource 您自己的,不需要这些值。因此,在上面的示例中,我们指定的是
db3.datasource
spring将寻找db3.datasource.url
db3.datasource.username
db3.datasource.password
重要的是,
spring
不是推断 完整的路径确实是:<代码> db3.datasource.url不是
spring.db3.datasource.url
最终要包裹起来,您确实可以灵活地以
spring
开始,如果需要。只要您指定,春季就会接收任一路。注意:此答案仅写在此处提供的文本框中,并且未在IDE中测试编译错误。逻辑仍然存在
By default Spring looks for
spring.datasource.**
for the properties of theDataSource
to connect to.So you might be getting wrong results because you are not connecting to the database. If you want to configure a DataSource with different,from default, properties you can do like so
And let's say a day comes along and you want a second
DataSource
you can modify the previous class to something like:and after that in each
Class
that you want aDataSource
you can specify which of the beans you like:So by default spring will look for
spring.datasource.url (or spring.datasource.jdbc-url)
spring.datasource.username
spring.datasource.password
If you specify another
DataSource
of your own, those values are not needed.So in the above example where we specified let's say
db3.datasource
spring will look fordb3.datasource.url
db3.datasource.username
db3.datasource.password
Important thing here is that the
spring
IS NOT inferred meaning the complete path is indeed:db3.datasource.url
and NOT
spring.db3.datasource.url
Finally to wrap this up you do have the flexibility to make it start with
spring
if you want so by declaring a prefix likespring.any.path.ilike.datasouce
and of course under that the related values. Spring will pick up either path as long as you specify it.NOTE: This answer is written solely in the text box provided here and was not tested in an IDE for compilation errors. The logic still holds though