[英]In Spring-Boot, How can we connect to two databases (Mysql database and MongoDB) in the same project?
[英]how can i use two databases with spring-boot
我試圖在我的項目中使用兩個數據庫,所以我遵循以下鏈接:
但是就像示例一樣,我創建一個接口並擴展CrudRepository
public interface UsuarioRepository extends CrudRepository<TbUsuario, Long>{}
所以我在哪里使用?
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
指定我的數據源?
您需要配置兩個數據源,兩個EntityManagerFactories和兩個TransactionManager。 看看Spring Boot文檔和4.1.2中的67.7使用兩個EntityManager 。 Spring Data JPA文檔中基於注釋的配置 。 您還需要在Application類中禁用數據源自動配置。 在這里可以找到顯示如何將所有這些東西放在一起的示例。 祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.