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