给我一个spring batch双数据源的例子,H2与DB2,并且在application.yml中 指定默认数据源为h2
时间: 2024-03-06 11:49:07 浏览: 161
好的,下面是一个简单的 Spring Batch 双数据源配置的例子:
首先,我们需要在 pom.xml 文件中添加 H2 和 DB2 的依赖:
```xml
<dependencies>
<!-- H2 Database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<!-- DB2 Database -->
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc4</artifactId>
<version>11.5.0.0</version>
</dependency>
<!-- Spring Batch -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
```
接着,在 application.yml 配置文件中,我们可以指定默认数据源为 H2 数据库。
```yml
spring:
datasource:
url: jdbc:h2:mem:testdb
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
database-platform: org.hibernate.dialect.H2Dialect
# Second Datasource
myapp:
datasource:
url: jdbc:db2://localhost:50000/mydb
username: db2admin
password: password
driver-class-name: com.ibm.db2.jcc.DB2Driver
```
接下来,我们需要配置两个数据源的 Bean,这里使用 `@Primary` 和 `@Qualifier` 注解来指定默认数据源和第二个数据源。
```java
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Primary
@Bean(name = "h2DataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource h2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db2DataSource")
@ConfigurationProperties(prefix = "myapp.datasource")
public DataSource db2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jobRepository")
public JobRepository jobRepository(@Qualifier("h2DataSource") DataSource dataSource,
PlatformTransactionManager transactionManager) throws Exception {
JobRepositoryFactoryBean jobRepositoryFactoryBean = new JobRepositoryFactoryBean();
jobRepositoryFactoryBean.setDataSource(dataSource);
jobRepositoryFactoryBean.setTransactionManager(transactionManager);
jobRepositoryFactoryBean.afterPropertiesSet();
return jobRepositoryFactoryBean.getObject();
}
@Bean(name = "jobLauncher")
public JobLauncher jobLauncher(@Qualifier("h2DataSource") DataSource dataSource,
PlatformTransactionManager transactionManager) throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository(dataSource, transactionManager));
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("h2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
```
现在,我们已经配置好了双数据源的 Spring Batch,可以在 Job 中使用 `@Qualifier` 注解来选择使用哪个数据源。
```java
@Configuration
public class MyJobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource db2DataSource;
@Autowired
private DataSource h2DataSource;
@Bean
public Job myJob() {
return jobBuilderFactory.get("myJob")
.incrementer(new RunIdIncrementer())
.start(step1())
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Person, Person>chunk(10)
.reader(personReader(db2DataSource))
.processor(personProcessor())
.writer(personWriter(h2DataSource))
.build();
}
@Bean
@StepScope
public JdbcCursorItemReader<Person> personReader(@Qualifier("db2DataSource") DataSource dataSource) {
return new JdbcCursorItemReaderBuilder<Person>()
.name("personReader")
.dataSource(dataSource)
.sql("SELECT * FROM PERSON")
.rowMapper(new PersonRowMapper())
.build();
}
@Bean
public ItemProcessor<Person, Person> personProcessor() {
return item -> {
item.setAge(item.getAge() + 1);
return item;
};
}
@Bean
public JdbcBatchItemWriter<Person> personWriter(@Qualifier("h2DataSource") DataSource dataSource) {
return new JdbcBatchItemWriterBuilder<Person>()
.dataSource(dataSource)
.sql("INSERT INTO PERSON (ID, NAME, AGE) VALUES (:id, :name, :age)")
.beanMapped()
.build();
}
}
```
在这个例子中,我们使用了 `@Qualifier` 注解来选择数据源,`personReader()` 方法使用了 DB2 数据源,`personWriter()` 方法使用了 H2 数据源。
希望这个例子能帮助你理解 Spring Batch 双数据源的配置和使用。
阅读全文