给我一个spring batch双数据源的例子,系统表写到H2,业务表写到DB2
时间: 2024-03-06 22:48:31 浏览: 229
好的,下面是一个简单的Spring Batch双数据源的例子,其中系统表写到H2,业务表写到DB2:
首先,需要在Spring Boot应用程序中配置两个数据源的连接信息。可以在application.yml(或application.properties)文件中添加以下配置:
```
spring:
datasource:
system:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password: password
business:
url: jdbc:db2://localhost:50000/mydb
driver-class-name: com.ibm.db2.jcc.DB2Driver
username: user
password: password
```
然后,定义两个数据源的bean:
```java
@Configuration
public class DataSourceConfig {
@Bean(name = "systemDataSource")
@ConfigurationProperties(prefix = "spring.datasource.system")
public DataSource systemDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "businessDataSource")
@ConfigurationProperties(prefix = "spring.datasource.business")
public DataSource businessDataSource() {
return DataSourceBuilder.create().build();
}
}
```
接下来,需要配置两个事务管理器和两个JdbcTemplate来访问两个数据源:
```java
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
@Qualifier("systemDataSource")
private DataSource systemDataSource;
@Autowired
@Qualifier("businessDataSource")
private DataSource businessDataSource;
@Bean(name = "systemTransactionManager")
public PlatformTransactionManager systemTransactionManager() {
return new DataSourceTransactionManager(systemDataSource);
}
@Bean(name = "businessTransactionManager")
public PlatformTransactionManager businessTransactionManager() {
return new DataSourceTransactionManager(businessDataSource);
}
@Bean(name = "systemJdbcTemplate")
public JdbcTemplate systemJdbcTemplate() {
return new JdbcTemplate(systemDataSource);
}
@Bean(name = "businessJdbcTemplate")
public JdbcTemplate businessJdbcTemplate() {
return new JdbcTemplate(businessDataSource);
}
}
```
最后,在Job中使用两个JdbcTemplate访问两个数据源:
```java
@Configuration
public class MyJobConfig {
@Autowired
@Qualifier("systemJdbcTemplate")
private JdbcTemplate systemJdbcTemplate;
@Autowired
@Qualifier("businessJdbcTemplate")
private JdbcTemplate businessJdbcTemplate;
@Autowired
@Qualifier("systemTransactionManager")
private PlatformTransactionManager systemTransactionManager;
@Autowired
@Qualifier("businessTransactionManager")
private PlatformTransactionManager businessTransactionManager;
@Bean
public Job myJob() {
return jobBuilderFactory.get("myJob")
.start(step1())
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<MyEntity, MyEntity>chunk(10)
.reader(systemItemReader())
.processor(myProcessor())
.writer(businessItemWriter())
.transactionManager(businessTransactionManager)
.build();
}
@Bean
public ItemReader<MyEntity> systemItemReader() {
JdbcCursorItemReader<MyEntity> reader = new JdbcCursorItemReader<>();
reader.setDataSource(systemJdbcTemplate.getDataSource());
reader.setSql("SELECT * FROM SYSTEM_TABLE");
reader.setRowMapper(new MyEntityMapper());
return reader;
}
@Bean
public ItemProcessor<MyEntity, MyEntity> myProcessor() {
return new MyEntityProcessor();
}
@Bean
public ItemWriter<MyEntity> businessItemWriter() {
JdbcBatchItemWriter<MyEntity> writer = new JdbcBatchItemWriter<>();
writer.setDataSource(businessJdbcTemplate.getDataSource());
writer.setSql("INSERT INTO BUSINESS_TABLE (ID, NAME) VALUES (:id, :name)");
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
return writer;
}
}
```
这个例子中,Job从系统表读取数据,经过处理后,将结果写入业务表。系统表使用systemDataSource连接,业务表使用businessDataSource连接,每个数据源都有自己的事务管理器和JdbcTemplate。
阅读全文