erdCloud를 반대로 작성하여 다 바꾸었다. ㅎㅎ
잘못 작성한 버전

제대로 작성한 버전

export -> sql preview로 바로 코드 얻기
Chunk Size와 PageSize 초기값 1000으로 설정

참고 자료 : https://jojoldu.tistory.com/331
https://jonny-cho.github.io/spring/2025-07-27-spring-batch-chapter3-chunk-processing/
배치 관련 자료:
https://github.com/spring-projects/spring-batch/tree/main/spring-batch-samples
한국어 버전) https://github.com/jojoldu/spring-batch-in-action
job-step-read-process-write
@Slf4j
@Configuration
@RequiredArgsConstructor
public class BatchJobConfig {
private final JobRepository jobRepository;
private final PlatformTransactionManager platformTransactionManager;
private final EntityManagerFactory entityManagerFactory; // 읽기 (jpa)
private final DataSource dataSource; // 쓰기용 (jdbc)
// job
@Bean
public Job job() {
return new JobBuilder("job test",jobRepository)
.start(step())
.build();
}
// step
@Bean
public Step step() {
return new StepBuilder("step test",jobRepository)
.<BillingHistory, BillingHistory>chunk(1000, platformTransactionManager)
.reader(read())
.processor(process())
.writer(write())
.build();
}
// read
@Bean
public JpaPagingItemReader<BillingHistory> read(){
return new JpaPagingItemReaderBuilder<BillingHistory>()
.name("read test")
.entityManagerFactory(entityManagerFactory)
.pageSize(1000)
// .queryString("SELECT bh FROM BillingHistory bh WHERE bh.isProcessed = false") // 정산 안 된 거
.queryString("SELECT bh FROM BillingHistory bh")
.build();
}
// process
@Bean
public ItemProcessor<BillingHistory, BillingHistory> process(){
return billingHistory -> {
log.info("Processing billingHistory id: {}", billingHistory.getBillingId());
return billingHistory;
};
}
// write
@Bean
public JdbcBatchItemWriter<BillingHistory> write() {
String sql = "INSERT INTO Invoice (line_id, billing_id, total_amount, billing_month, created_at) " +
"VALUES (:lineId, :billingId, :amount, :billingMonth, NOW())";
return new JdbcBatchItemWriterBuilder<BillingHistory>()
.dataSource(dataSource)
// 실제로는 Usage 테이블이 아니라 Invoice 테이블에 INSERT 해야 합니다.
.sql(sql)
.beanMapped()
.build();
}
}