简体   繁体   English

带有旧表的Spring Boot

[英]Spring Boot with Legacy tables

I want to use Spring Boot with Legacy database and use hibernate for the same. 我想将Spring Boot与Legacy数据库一起使用,并将hibernate用于同一数据库。 I have to access both DB2 and Teradata as data sources.I would like to know if this is possible ? 我必须同时访问DB2和Teradata作为数据源。我想知道是否可行? I'm a newbie to Spring , SpringBoot and have no idea how to configure data sources , should i still need multiple xml configs etc. The demos in spring io sites are good but they dont deal with any of my requirement. 我是Spring,SpringBoot的新手,不知道如何配置数据源,我是否仍需要多个xml配置等。Spring io站点中的演示很好,但它们不能满足我的任何要求。 Kindly point me in the right direction. 请指出正确的方向。

In Spring Boot it is very easy to configure multiple datasources. 在Spring Boot中,配置多个数据源非常容易。 This part of the documentation describes the procedure in detail. 文档的部分详细描述了该过程。

In essence, all you need to do is have the connection properties in a place where Spring boot can locate them (the easiest place is application.properties ) and then configure the beans. 本质上,您需要做的就是将连接属性放在Spring Boot可以找到它们的位置(最简单的位置是application.properties ),然后配置Bean。 The code would look like: 代码如下所示:

application.properties application.properties

datasource.primary.url=jdbc:mysql://localhost/test
datasource.primary.username=dbuser
datasource.primary.password=dbpass

datasource.secondary.url=jdbc:mysql://localhost/test2
datasource.secondary.username=dbuser2
datasource.secondary.password=dbpass2

Spring configuration file Spring配置文件

@Bean
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

You will notice that no XML configuration is used which the preferred style these days. 您会注意到,目前没有使用首选样式的XML配置。 Actually using Spring Boot means that you don't have to write almost any configuration :) 实际上使用Spring Boot意味着您几乎不必编写任何配置:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM