-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallExcelReadBatchConfig.java
More file actions
63 lines (55 loc) · 2.37 KB
/
SmallExcelReadBatchConfig.java
File metadata and controls
63 lines (55 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.io.example.config;
import com.io.example.dto.StudentDto;
import com.io.example.mapper.StudentMapper;
import com.io.example.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.extensions.excel.poi.PoiItemReader;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
@RequiredArgsConstructor
public class SmallExcelReadBatchConfig {
private final StudentService studentService;
@Bean
@StepScope
public PoiItemReader<StudentDto> smallExcelReader(
@Value("#{jobParameters['filePath']}") String filePath
) {
PoiItemReader<StudentDto> reader = new PoiItemReader<>();
reader.setResource(new ClassPathResource(filePath));
reader.setLinesToSkip(1);
reader.setRowMapper(new StudentMapper());
return reader;
}
@Bean
public ItemProcessor<StudentDto, StudentDto> smallExcelProcessor() {
return student -> student;
}
@Bean
public ItemWriter<StudentDto> smallExcelWriter() {
return items -> items.forEach(studentService::print);
}
@Bean
public Step smallExcelStep(JobRepository jobRepository,
PlatformTransactionManager transactionManager,
PoiItemReader<StudentDto> smallExcelReader,
ItemProcessor<StudentDto, StudentDto> smallExcelProcessor,
ItemWriter<StudentDto> smallExcelWriter,
@Value("${spring.batch.chunk-size}") int chunkSize) {
return new StepBuilder("smallExcelStep", jobRepository)
.<StudentDto, StudentDto>chunk(chunkSize, transactionManager)
.reader(smallExcelReader)
.processor(smallExcelProcessor)
.writer(smallExcelWriter)
.build();
}
}