입사하자마자 Spring Batch로 이용하여 멀티데이터베이스를 제어하는 코드를 짰다.
바로 본론으로 들어가보자.
1.resources/application.yml 생성
datasource:
DB 종류 이름1:
first:
jdbc-url: 첫번째 DB 주소
username: 아이디
password: 비밀번호
driver-class-name: 첫번째 클래스 이름
DB 종류 이름2:
jdbc-url: 두번째 DB 주소
password: 비밀번호
username: 아이디
driver-class-name: 두번째 클래스 이름
-datasource 부분에서 DB 종류이름으로 나누어서 인식한다.
-추후 first,second,third… 로 확장 가능하다.
2.각각 해당하는 DB Connect 담당 디텍토리 및 클래스 생성
@Configuration
@MapperScan(value = "com.infinigru.springbatchtest.dao.해당DB dao 클래스",sqlSessionFactoryRef = "DB이름SqlSessionFactory")
@EnableTransactionManagement
public class DB이름DataSourceConfig {
@Bean(name = "DB이름DataSource")
@Primary//메인DB에만 선언
@ConfigurationProperties(prefix = "datasource.DB종류1.first")//application.yml에 선언한 순서대로 작성!
public DataSource mariadbDatasource() {
return DB이름SourceBuilder.create().build();
}
@Bean(name = "DB이름SqlSessionFactory")
@Primary//메인DB에만 선언
public SqlSessionFactory DB이름SqlSessionFactory(@Qualifier("DB이름DataSource") DataSource DB이름Datasource, ApplicationContext applicationContext) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(DB이름Datasource);
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:해당 Mapper파일 위치"));
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "DB이름SqlSessionTemplate")
@Primary//메인DB에만 선언
public SqlSessionTemplate masterSqlSessionTemplate(SqlSessionFactory DB이름SqlSessionFactory) {
return new SqlSessionTemplate(DB이름SqlSessionFactory);
}
}
3.Job을 설정하는 구간에서 선언한 DB를 Step에 연결시켜 준다.
MariadbDataSourceConfig mariadbDataSourceConfig;
GoldilocksDataBaseConfig goldilocksDataBaseConfig;
public JobClass(MariadbDataSourceConfig mariadbDataSourceConfig, GoldilocksDataBaseConfig goldilocksDataBaseConfig) {
this.mariadbDataSourceConfig = mariadbDataSourceConfig;
this.goldilocksDataBaseConfig = goldilocksDataBaseConfig;
}
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(DB이름1DataSourceConfig.DB이름1Datasource());
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:해당 Mapper파일 위치"));
sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:Mybatis 설정 파일 위치"));
return sqlSessionFactoryBean.getObject();
}
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(DB이름2DataBaseConfig.DB이름2DataSource());
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:해당 Mapper파일 위치"));
sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:Mybatis 설정 파일 위치"));
return sqlSessionFactoryBean.getObject();
}
4.위에서 선언한 sqlSessionFactory를 해당 Reader,Writer에 주입해준다.
public MyBatisPagingItemReader<T> CustomMybatisPagingItemReader(int chunkSize, SqlSessionFactory sqlSessionFactory1, String queryId) throws Exception {
MyBatisPagingItemReader<T> itemReader = new MyBatisPagingItemReaderBuilder<T>()
.pageSize(chunkSize)
.sqlSessionFactory(sqlSessionFactory1)//여기에 주입!
.queryId(queryId)//Mybatis의 같은 경우에는 Mapper.xml에서 선언한 DAO를 일치시켜서 해당 서비스를 하는 메서드를 주입해준다.
.build();
itemReader.afterPropertiesSet();
return itemReader;
}
설정에 관한 부분은 여기까지이다.
슬슬 Batch에 관한 글을 계속 올릴 예정….
내용이 많이 부족하지만 질문 해주시면 감사하겠습니다.