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