【问题标题】:how can i use two databases with spring-boot我如何在 spring-boot 中使用两个数据库
【发布时间】:2015-07-28 19:21:30
【问题描述】:

我试图在我的项目中使用两个数据库,所以我点击这个链接:

this 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();
} 

指定我的数据源??

【问题讨论】:

标签: spring-boot datasource


【解决方案1】:

您需要配置两个Datasources、两个EntityManagerFactories 和两个TransactionManagers。查看 Spring Boot 文档中的 67.7 Use Two EntityManagers 部分和 Spring Data JPA 文档中的 4.1.2. Annotation based configuration 部分。您还需要在 Application 类中禁用数据源自动配置。可以在here 找到一个展示如何将所有这些东西放在一起的示例。祝你好运!

【讨论】:

    猜你喜欢
    • 2017-06-11
    • 2018-08-07
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2015-12-25
    • 2023-01-15
    • 1970-01-01
    相关资源
    最近更新 更多