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