我需要快速指导在 Spring Boot Batch 项目中创建两个关系数据源。一个是 Oracle 作为源数据库,另一个是 Postgres 目标数据库。Spring Boot V2.2.5.RELEADE
Spring Boot 版本 2.2.5.RELEASE
在这里,我想自定义两个数据源以使用此处提到的所有属性(http://shekup.blogspot.com/2018/05/multiple-data-sources-in-spring-batch.html#:~:text=Multiple%20Data %20sources%20in%20Spring%20batch,such%20as%20ETL%20batch%20job. ) 两个数据源
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres?currentSchema=XXXX?useSSL=false
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver
# max no. of connections in the pool
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=20
spring.datasource.hikari.connection-test-query=SELECT 1
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.default_schema=YYYY
spring.batch.initialize-schema=always
spring.batch.table-prefix=YYYY.BATCH_
### Source oracle DS ###
oracle.datasource.url=jdbc:oracle:thin:@//XXXX:1527/XXX
oracle.datasource.username=XXX
oracle.datasource.password=XXX
oracle.datasource.driverClassName=oracle.jdbc.Oracloracleiver
# max no. of connections in the pool
oracle.spring.datasource.hikari.maximum-pool-size=30
oracle.spring.datasource.hikari.minimum-idle=20
oracle.spring.datasource.hikari.connection-test-query=SELECT 1
数据库配置
@Configuration
public class DataSourceConfig {
@Autowired
private Environment env;
@Primary
@Bean(name = "postgresDataSource")
@ConfigurationProperties("spring.datasource")
public DataSource batchDataSource() {
return DataSourceBuilder.create().url(env.getProperty("spring.datasource.url"))
.driverClassName(env.getProperty("spring.datasource.driver-class-name"))
.username(env.getProperty("spring.datasource.username"))
.password(env.getProperty("spring.datasource.password")).build();
}
@Bean(name = "oracleDataSource")
@ConfigurationProperties("oracle.datasource")
public DataSource mysqlBatchDataSource() {
return DataSourceBuilder.create().url(env.getProperty("oracle.datasource.url"))
.driverClassName(env.getProperty("oracle.datasource.driver-class-name"))
.username(env.getProperty("oracle.datasource.username"))
.password(env.getProperty("oracle.datasource.password")).build();
}
}