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

时间:2015-07-28 19:21:31

标签: spring-boot datasource

我尝试在项目中使用两个数据库,所以我按照以下链接:

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

指定我的数据源??

1 个答案:

答案 0 :(得分:1)

You need to configure two Datasources, two EntityManagerFactories, and two TransactionManagers. Have a look at section 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. Good luck!