我如何通过 spring-boot 使用两个数据库

how can i use two databases with spring-boot

我试图在我的项目中使用两个数据库,所以我遵循这个 link:

但就像示例一样,我创建了一个接口并扩展了 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。查看 67.7 Use Two EntityManagers in the Spring Boot docs and 4.1.2. Annotation based configuration in the Spring Data JPA docs. You also need to disable datasource auto configuration in the Application class. An example showing how to put all this stuff together can be found here 部分。祝你好运!