我目前有一個數據庫連接,它正在工作。我想連接另一個(甚至最多2個)數據庫。我該怎麼做?應該只有使用註釋和屬性文件的解決方案。Spring Boot JPA:如何連接多個數據庫?
我讀這 Profile Specific Properties ,它有點幫助,但我仍然不知道如何開關從一個配置文件到其他的代碼運行時。我假設我需要一次連接到一個配置文件,然後才能嘗試從不同的數據庫中檢索/保留內容。
我也讀過這個問題,How to use 2 or more databases with spring?,但我不知道它是如何工作太好/如果它會適用。我沒有使用控制器類,我不知道那是什麼。我也不確定他們在答案中提到的配置類是如何連接到特定的DO的。
這是我的application.properties文件:(標示出用戶名和密碼,但它的存在在我的文件)
hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.default_schema=dbo
hibernate.packagesToScan=src.repositories.LMClientRepository.java
spring.jpa.generate-ddl=true
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.url=jdbc:sqlserver://schqvsqlaod:1433;database=dbMOBClientTemp;integratedSecurity=false;
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.database=dbMOBClientTemp
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
這是我的應用程序文件:
package testApplication;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import fileRetrieval.InputFileParse;
import lmDataObjects.LMClientDO;
import lmDataObjects.LoadMethodDO;
import repositories.LMClientRepository;
import repositories.LoadMethodRepository;
@SpringBootApplication
@EnableJpaRepositories(basePackageClasses = LoadMethodRepository.class)
@EntityScan(basePackageClasses = LoadMethodDO.class)
@EnableCaching
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner demo(LoadMethodRepository lm_repo, LMClientRepository lmc_repo) {
return (args) -> {
List<LMClientDO> lmlist = InputFileParse.getMultiGroupfile();
List<String> uniqueMediaIds = new ArrayList(InputFileParse.getUniqueMediaIds());
for (int i = 0; i < InputFileParse.getUniqueMediaIds().size(); i ++){
lm_repo.save(new LoadMethodDO(uniqueMediaIds.get(i)));
}
for (int i = 0; i < lmlist.size(); i++){
lmc_repo.save(new LMClientDO(lmlist.get(i).getClientId(), lmlist.get(i).getMediaId()));
}
//Here is where I would like to do stuff with data from the other database that I have not connected yet
};
}
}
我也做了一個新的屬性文件叫做application-MTS.properties,並且我把新數據庫的數據放在那裏。仍然不確定如何處理它。
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.url=jdbc:sqlserver://SCHQVSQLCON2\VSPD:1433;database=dbMTS;integratedSecurity=false;
還有一個[示例項目在這裏](https://github.com/snicoll-demos/demo-multi-entity-managers) –