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.bulkinsert;
17  
18  import static examples.springbatch.mapper.PersonDynamicSqlSupport.firstName;
19  import static examples.springbatch.mapper.PersonDynamicSqlSupport.forPagingTest;
20  import static examples.springbatch.mapper.PersonDynamicSqlSupport.lastName;
21  
22  import javax.sql.DataSource;
23  
24  import org.apache.ibatis.session.SqlSessionFactory;
25  import org.mybatis.dynamic.sql.insert.InsertDSL;
26  import org.mybatis.dynamic.sql.render.RenderingStrategies;
27  import org.mybatis.spring.SqlSessionFactoryBean;
28  import org.mybatis.spring.annotation.MapperScan;
29  import org.mybatis.spring.batch.MyBatisBatchItemWriter;
30  import org.springframework.batch.core.Job;
31  import org.springframework.batch.core.Step;
32  import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
33  import org.springframework.batch.core.job.builder.JobBuilder;
34  import org.springframework.batch.core.launch.support.RunIdIncrementer;
35  import org.springframework.batch.core.repository.JobRepository;
36  import org.springframework.batch.core.step.builder.StepBuilder;
37  import org.springframework.batch.item.ItemProcessor;
38  import org.springframework.batch.item.ItemWriter;
39  import org.springframework.beans.factory.annotation.Autowired;
40  import org.springframework.context.annotation.Bean;
41  import org.springframework.context.annotation.ComponentScan;
42  import org.springframework.context.annotation.Configuration;
43  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
44  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
45  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
46  import org.springframework.transaction.PlatformTransactionManager;
47  
48  import examples.springbatch.common.PersonRecord;
49  import examples.springbatch.mapper.PersonDynamicSqlSupport;
50  import examples.springbatch.mapper.PersonMapper;
51  
52  @EnableBatchProcessing
53  @Configuration
54  @ComponentScan("examples.springbatch.bulkinsert")
55  @ComponentScan("examples.springbatch.common")
56  @MapperScan("examples.springbatch.mapper")
57  public class BulkInsertConfiguration {
58  
59      @Autowired
60      private JobRepository jobRepository;
61  
62      @Autowired
63      private PlatformTransactionManager transactionManager;
64  
65      @Bean
66      public DataSource dataSource() {
67          return new EmbeddedDatabaseBuilder()
68                  .setType(EmbeddedDatabaseType.HSQL)
69                  .addScript("classpath:/org/springframework/batch/core/schema-drop-hsqldb.sql")
70                  .addScript("classpath:/org/springframework/batch/core/schema-hsqldb.sql")
71                  .addScript("classpath:/examples/springbatch/schema.sql")
72                  .build();
73      }
74  
75      @Bean
76      public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
77          SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
78          sessionFactory.setDataSource(dataSource);
79          return sessionFactory.getObject();
80      }
81  
82      @Bean
83      public PlatformTransactionManager transactionManager(DataSource dataSource) {
84          return new DataSourceTransactionManager(dataSource);
85      }
86  
87      @Bean
88      public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionFactory) {
89          MyBatisBatchItemWriter<PersonRecord> writer = new MyBatisBatchItemWriter<>();
90          writer.setSqlSessionFactory(sqlSessionFactory);
91  
92          writer.setItemToParameterConverter(row -> InsertDSL.insert(row)
93                      .into(PersonDynamicSqlSupport.person)
94                      .map(firstName).toProperty("firstName")
95                      .map(lastName).toProperty("lastName")
96                      .map(forPagingTest).toStringConstant("false")
97                      .build()
98                      .render(RenderingStrategies.MYBATIS3));
99  
100         writer.setStatementId(PersonMapper.class.getName() + ".insert");
101         return writer;
102     }
103 
104     @Bean
105     public Step step1(ItemProcessor<PersonRecord, PersonRecord> processor, ItemWriter<PersonRecord> writer) {
106         return new StepBuilder("step1", jobRepository)
107                 .<PersonRecord, PersonRecord>chunk(10, transactionManager)
108                 .reader(new TestRecordGenerator())
109                 .processor(processor)
110                 .writer(writer)
111                 .build();
112     }
113 
114     @Bean
115     public Job insertRecords(Step step1) {
116         return new JobBuilder("insertRecords", jobRepository)
117                 .incrementer(new RunIdIncrementer())
118                 .flow(step1)
119                 .end()
120                 .build();
121     }
122 }