我正在尝试替换一些项目以使用 Spring Data JDBC 而不是使用JdbcTemplate
.
现在,我想多用它DataSource
,如何在Spring Data JDBC中配置它?
我正在尝试替换一些项目以使用 Spring Data JDBC 而不是使用JdbcTemplate
.
现在,我想多用它DataSource
,如何在Spring Data JDBC中配置它?
截至 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 要点是:
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
@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 知道需要哪个数据源。
目前不支持使用两个或多个DataSource
s. 您必须手动重做该JdbcRepositoryFactoryBean
操作。