1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.springbatch.paging;
17
18 import static examples.springbatch.mapper.PersonDynamicSqlSupport.*;
19 import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
20 import static org.mybatis.dynamic.sql.SqlBuilder.select;
21
22 import javax.sql.DataSource;
23
24 import org.apache.ibatis.session.SqlSessionFactory;
25 import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
26 import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
27 import org.mybatis.dynamic.sql.util.springbatch.SpringBatchUtility;
28 import org.mybatis.spring.SqlSessionFactoryBean;
29 import org.mybatis.spring.annotation.MapperScan;
30 import org.mybatis.spring.batch.MyBatisBatchItemWriter;
31 import org.mybatis.spring.batch.MyBatisPagingItemReader;
32 import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
33 import org.springframework.batch.core.job.Job;
34 import org.springframework.batch.core.job.builder.JobBuilder;
35 import org.springframework.batch.core.job.parameters.RunIdIncrementer;
36 import org.springframework.batch.core.repository.JobRepository;
37 import org.springframework.batch.core.step.Step;
38 import org.springframework.batch.core.step.builder.StepBuilder;
39 import org.springframework.batch.infrastructure.item.ItemProcessor;
40 import org.springframework.batch.infrastructure.item.ItemReader;
41 import org.springframework.batch.infrastructure.item.ItemWriter;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.context.annotation.ComponentScan;
45 import org.springframework.context.annotation.Configuration;
46 import org.springframework.core.convert.converter.Converter;
47 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
48 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
49 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
50 import org.springframework.transaction.PlatformTransactionManager;
51
52 import java.util.Objects;
53
54 import examples.springbatch.common.PersonRecord;
55 import examples.springbatch.mapper.PersonMapper;
56
57 @EnableBatchProcessing
58 @Configuration
59 @ComponentScan("examples.springbatch.common")
60 @MapperScan("examples.springbatch.mapper")
61 public class PagingReaderBatchConfiguration {
62
63 @Autowired
64 private JobRepository jobRepository;
65
66 @Bean
67 public DataSource dataSource() {
68 return new EmbeddedDatabaseBuilder()
69 .setType(EmbeddedDatabaseType.HSQL)
70 .addScript("classpath:/org/springframework/batch/core/schema-drop-hsqldb.sql")
71 .addScript("classpath:/org/springframework/batch/core/schema-hsqldb.sql")
72 .addScript("classpath:/examples/springbatch/schema.sql")
73 .addScript("classpath:/examples/springbatch/data.sql")
74 .build();
75 }
76
77 @Bean
78 public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
79 SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
80 sessionFactory.setDataSource(dataSource);
81 return Objects.requireNonNull(sessionFactory.getObject());
82 }
83
84 @Bean
85 public PlatformTransactionManager transactionManager(DataSource dataSource) {
86 return new DataSourceTransactionManager(dataSource);
87 }
88
89 @Bean
90 public MyBatisPagingItemReader<PersonRecord> reader(SqlSessionFactory sqlSessionFactory) {
91 SelectStatementProvider selectStatement = select(person.allColumns())
92 .from(person)
93 .where(forPagingTest, isEqualTo(true))
94 .orderBy(id)
95 .limit(SpringBatchUtility.MYBATIS_SPRING_BATCH_PAGESIZE)
96 .offset(SpringBatchUtility.MYBATIS_SPRING_BATCH_SKIPROWS)
97 .build()
98 .render(SpringBatchUtility.SPRING_BATCH_PAGING_ITEM_READER_RENDERING_STRATEGY);
99
100 MyBatisPagingItemReader<PersonRecord> reader = new MyBatisPagingItemReader<>();
101 reader.setQueryId(PersonMapper.class.getName() + ".selectMany");
102 reader.setSqlSessionFactory(sqlSessionFactory);
103 reader.setParameterValues(SpringBatchUtility.toParameterValues(selectStatement));
104 reader.setPageSize(7);
105 return reader;
106 }
107
108 @Bean
109 public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionFactory,
110 Converter<PersonRecord, UpdateStatementProvider> convertor) {
111 MyBatisBatchItemWriter<PersonRecord> writer = new MyBatisBatchItemWriter<>();
112 writer.setSqlSessionFactory(sqlSessionFactory);
113 writer.setItemToParameterConverter(convertor);
114 writer.setStatementId(PersonMapper.class.getName() + ".update");
115 return writer;
116 }
117
118 @Bean
119 public Step step1(ItemReader<PersonRecord> reader, ItemProcessor<PersonRecord, PersonRecord> processor, ItemWriter<PersonRecord> writer) {
120 return new StepBuilder("step1", jobRepository)
121 .<PersonRecord, PersonRecord>chunk(7)
122 .reader(reader)
123 .processor(processor)
124 .writer(writer)
125 .build();
126 }
127
128 @Bean
129 public Job upperCaseLastName(Step step1) {
130 return new JobBuilder("upperCaseLastName", jobRepository)
131 .incrementer(new RunIdIncrementer())
132 .flow(step1)
133 .end()
134 .build();
135 }
136 }