View Javadoc
1   /*
2    *    Copyright 2016-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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) // In Spring Batch 5, move this to the job builder constructor
128                 .incrementer(new RunIdIncrementer())
129                 .flow(step1)
130                 .end()
131                 .build();
132     }
133 }