1

我正在尝试替换一些项目以使用 Spring Data JDBC 而不是使用JdbcTemplate.

现在,我想多用它DataSource,如何在Spring Data JDBC中配置它?

4

2 回答 2

1

截至 2022 年 1 月,尚未添加对 Spring Data JDBC 中的多个数据源的支持 - https://github.com/spring-projects/spring-data-jdbc/issues/544

甚至 Spring Boot 也不支持开箱即用,您可以通过自己配置来实现。您可以在 Internet 上找到示例,例如https://dba-presents.com/index.php/jvm/java/242-spring-data-jdbc-with-two-datasources 要点是:

  1. 将实体和存储库移动到单独的包 - 每个数据源。
  2. 配置数据库连接。
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/employees
spring.datasource.username=root
spring.datasource.password=password
 
spring.users-datasource.jdbc-url=jdbc:mysql://localhost:3306/users
spring.users-datasource.username=root
spring.users-datasource.password=password
  1. 分别配置每个 JDBC 存储库。配置将包与适当的数据源链接起来。
@Configuration
@EnableJdbcRepositories(
  basePackages = "com.dbapresents.twodatasources.dao.employees",
  transactionManagerRef = "employeesTransactionManager",
  jdbcOperationsRef = "employeesJdbcOperations"
)
@EnableAutoConfiguration(exclude = {
  DataSourceAutoConfiguration.class,
  JdbcRepositoriesAutoConfiguration.class
})
public class PersistenceEmployeesConfiguration {
 
  @Bean
  @ConfigurationProperties(prefix="spring.datasource")
  DataSource employeesDataSource() {
    return DataSourceBuilder.create().build();
  }
 
  @Bean(name = "employeesTransactionManager")
  PlatformTransactionManager employeesTransactionManager(@Qualifier("employeesDataSource") DataSource employeesDataSource) {
    return new JdbcTransactionManager(employeesDataSource);
  }
 
  @Bean
  NamedParameterJdbcOperations employeesJdbcOperations(@Qualifier("employeesDataSource") DataSource dataSource) {
      return new NamedParameterJdbcTemplate(dataSource);
  }
 
}
@Configuration
@EnableJdbcRepositories(
  basePackages = "com.dbapresents.twodatasources.dao.users",
  transactionManagerRef = "usersTransactionManager",
  jdbcOperationsRef = "usersJdbcOperations"
)
@EnableAutoConfiguration(exclude = {
  DataSourceAutoConfiguration.class,
  JdbcRepositoriesAutoConfiguration.class
})
public class PersistenceUsersConfiguration {
 
  @Bean
  @ConfigurationProperties(prefix="spring.users-datasource")
  DataSource userDataSource() {
    return DataSourceBuilder.create().build();
  }
 
  @Bean(name = "usersTransactionManager")
  PlatformTransactionManager usersTransactionManager(@Qualifier("userDataSource") DataSource userDataSource) {
    return new JdbcTransactionManager(userDataSource);
  }
 
  @Bean
  NamedParameterJdbcOperations usersJdbcOperations(@Qualifier("userDataSource") DataSource dataSource) {
    return new NamedParameterJdbcTemplate(dataSource);
  }
 
  @Bean
  Dialect jdbcDialect() {
    return MySqlDialect.INSTANCE;
  }
 
  @Bean
  JdbcCustomConversions customConversions() {
    return new JdbcCustomConversions();
  }
 
  @Bean
  JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy, JdbcCustomConversions customConversions) {
    JdbcMappingContext mappingContext = new JdbcMappingContext((NamingStrategy)namingStrategy.orElse(NamingStrategy.INSTANCE));
    mappingContext.setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
    return mappingContext;
  }
 
  @Bean
  JdbcConverter jdbcConverter(JdbcMappingContext mappingContext,
    @Qualifier("usersJdbcOperations") NamedParameterJdbcOperations jdbcOperationsDataBase1,
    @Lazy RelationResolver relationResolver,
    JdbcCustomConversions conversions,
    Dialect dialect) {
 
    DefaultJdbcTypeFactory jdbcTypeFactory = new DefaultJdbcTypeFactory(jdbcOperationsDataBase1.getJdbcOperations());
    return new BasicJdbcConverter(mappingContext, relationResolver, conversions, jdbcTypeFactory,
      dialect.getIdentifierProcessing());
  }
 
}

当您在特定存储库上执行方法时,由于配置,Spring 知道需要哪个数据源。

于 2022-01-25T20:14:01.690 回答
0

目前不支持使用两个或多个DataSources. 您必须手动重做该JdbcRepositoryFactoryBean操作。

于 2018-05-06T07:13:11.097 回答