View Javadoc
1   /*
2    *    Copyright 2016-2025 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.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.Job;
33  import org.springframework.batch.core.Step;
34  import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
35  import org.springframework.batch.core.job.builder.JobBuilder;
36  import org.springframework.batch.core.launch.support.RunIdIncrementer;
37  import org.springframework.batch.core.repository.JobRepository;
38  import org.springframework.batch.core.step.builder.StepBuilder;
39  import org.springframework.batch.item.ItemProcessor;
40  import org.springframework.batch.item.ItemReader;
41  import org.springframework.batch.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 examples.springbatch.common.PersonRecord;
53  import examples.springbatch.mapper.PersonMapper;
54  
55  @EnableBatchProcessing
56  @Configuration
57  @ComponentScan("examples.springbatch.common")
58  @MapperScan("examples.springbatch.mapper")
59  public class PagingReaderBatchConfiguration {
60  
61      @Autowired
62      private JobRepository jobRepository;
63  
64      @Autowired
65      private PlatformTransactionManager transactionManager;
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 sessionFactory.getObject();
83      }
84  
85      @Bean
86      public PlatformTransactionManager transactionManager(DataSource dataSource) {
87          return new DataSourceTransactionManager(dataSource);
88      }
89  
90      @Bean
91      public MyBatisPagingItemReader<PersonRecord> reader(SqlSessionFactory sqlSessionFactory) {
92          SelectStatementProvider selectStatement =  select(person.allColumns())
93                  .from(person)
94                  .where(forPagingTest, isEqualTo(true))
95                  .orderBy(id)
96                  .limit(SpringBatchUtility.MYBATIS_SPRING_BATCH_PAGESIZE)
97                  .offset(SpringBatchUtility.MYBATIS_SPRING_BATCH_SKIPROWS)
98                  .build()
99                  .render(SpringBatchUtility.SPRING_BATCH_PAGING_ITEM_READER_RENDERING_STRATEGY);
100 
101         MyBatisPagingItemReader<PersonRecord> reader = new MyBatisPagingItemReader<>();
102         reader.setQueryId(PersonMapper.class.getName() + ".selectMany");
103         reader.setSqlSessionFactory(sqlSessionFactory);
104         reader.setParameterValues(SpringBatchUtility.toParameterValues(selectStatement));
105         reader.setPageSize(7);
106         return reader;
107     }
108 
109     @Bean
110     public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionFactory,
111             Converter<PersonRecord, UpdateStatementProvider> convertor) {
112         MyBatisBatchItemWriter<PersonRecord> writer = new MyBatisBatchItemWriter<>();
113         writer.setSqlSessionFactory(sqlSessionFactory);
114         writer.setItemToParameterConverter(convertor);
115         writer.setStatementId(PersonMapper.class.getName() + ".update");
116         return writer;
117     }
118 
119     @Bean
120     public Step step1(ItemReader<PersonRecord> reader, ItemProcessor<PersonRecord, PersonRecord> processor, ItemWriter<PersonRecord> writer) {
121         return new StepBuilder("step1", jobRepository)
122                 .<PersonRecord, PersonRecord>chunk(7, transactionManager)
123                 .reader(reader)
124                 .processor(processor)
125                 .writer(writer)
126                 .build();
127     }
128 
129     @Bean
130     public Job upperCaseLastName(Step step1) {
131         return new JobBuilder("upperCaseLastName", jobRepository)
132                 .incrementer(new RunIdIncrementer())
133                 .flow(step1)
134                 .end()
135                 .build();
136     }
137 }