Skip to content

Commit 5adc812

Browse files
committed
Add utility classes for Spring batch
1 parent 7969f39 commit 5adc812

File tree

15 files changed

+615
-7
lines changed

15 files changed

+615
-7
lines changed

pom.xml

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@
1515
limitations under the License.
1616
1717
-->
18-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<!-- Copyright 2016-2019 the original author or authors. Licensed under the
19+
Apache License, Version 2.0 (the "License"); you may not use this file except
20+
in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
21+
Unless required by applicable law or agreed to in writing, software distributed
22+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
23+
OR CONDITIONS OF ANY KIND, either express or implied. See the License for
24+
the specific language governing permissions and limitations under the License. -->
25+
<project xmlns="http://maven.apache.org/POM/4.0.0"
26+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1928
<modelVersion>4.0.0</modelVersion>
2029
<parent>
2130
<groupId>org.mybatis</groupId>
@@ -38,7 +47,7 @@
3847
<clirr.comparisonVersion>1.1.0</clirr.comparisonVersion>
3948
<module.name>org.mybatis.dynamic.sql</module.name>
4049
</properties>
41-
50+
4251
<build>
4352
<pluginManagement>
4453
<plugins>
@@ -52,7 +61,7 @@
5261
</plugins>
5362
</pluginManagement>
5463
</build>
55-
64+
5665
<reporting>
5766
<plugins>
5867
<plugin>
@@ -64,7 +73,7 @@
6473
</plugin>
6574
</plugins>
6675
</reporting>
67-
76+
6877
<dependencies>
6978
<dependency>
7079
<groupId>org.junit.jupiter</groupId>
@@ -84,7 +93,7 @@
8493
<version>${junit.platform.version}</version>
8594
<scope>test</scope>
8695
</dependency>
87-
96+
8897
<dependency>
8998
<groupId>org.assertj</groupId>
9099
<artifactId>assertj-core</artifactId>
@@ -97,6 +106,12 @@
97106
<version>3.5.0</version>
98107
<scope>test</scope>
99108
</dependency>
109+
<dependency>
110+
<groupId>org.mybatis</groupId>
111+
<artifactId>mybatis-spring</artifactId>
112+
<version>2.0.0</version>
113+
<scope>test</scope>
114+
</dependency>
100115
<dependency>
101116
<groupId>org.hsqldb</groupId>
102117
<artifactId>hsqldb</artifactId>
@@ -109,6 +124,24 @@
109124
<version>5.1.4.RELEASE</version>
110125
<scope>test</scope>
111126
</dependency>
127+
<dependency>
128+
<groupId>org.springframework.batch</groupId>
129+
<artifactId>spring-batch-core</artifactId>
130+
<version>4.1.1.RELEASE</version>
131+
<scope>test</scope>
132+
</dependency>
133+
<dependency>
134+
<groupId>org.springframework.batch</groupId>
135+
<artifactId>spring-batch-test</artifactId>
136+
<version>4.1.1.RELEASE</version>
137+
<scope>test</scope>
138+
<exclusions>
139+
<exclusion>
140+
<groupId>junit</groupId>
141+
<artifactId>junit</artifactId>
142+
</exclusion>
143+
</exclusions>
144+
</dependency>
112145
<dependency>
113146
<groupId>ch.qos.logback</groupId>
114147
<artifactId>logback-classic</artifactId>
@@ -138,7 +171,7 @@
138171
<url>git:ssh://[email protected]/mybatis/mybatis-dynamic-sql.git?gh-pages#</url>
139172
</site>
140173
</distributionManagement>
141-
174+
142175
<profiles>
143176
<profile>
144177
<id>javadocVersion</id>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2016-2019 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+
* http://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 org.mybatis.dynamic.sql.util.springbatch;
17+
18+
import java.util.Map;
19+
20+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
21+
22+
public class SpringBatchProviderAdapter {
23+
24+
public String select(Map<String, Object> parameterValues) {
25+
SelectStatementProvider selectStatement =
26+
(SelectStatementProvider) parameterValues.get(SpringBatchUtility.PARAMETER_KEY);
27+
return selectStatement.getSelectStatement();
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2016-2019 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+
* http://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 org.mybatis.dynamic.sql.util.springbatch;
17+
18+
import org.mybatis.dynamic.sql.BindableColumn;
19+
import org.mybatis.dynamic.sql.render.MyBatis3RenderingStrategy;
20+
21+
/**
22+
* This rendering strategy should be used for MyBatis3 statements using one of the
23+
* Spring batch readers supplied by mybatis-spring integration (http://www.mybatis.org/spring/).
24+
* Those readers are MyBatisPagingItemReader and MyBatisCursorItemReader.
25+
*
26+
*/
27+
public class SpringBatchReaderRenderingStrategy extends MyBatis3RenderingStrategy {
28+
29+
@Override
30+
public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName) {
31+
String newPrefix = SpringBatchUtility.PARAMETER_KEY + "." + prefix; //$NON-NLS-1$
32+
return super.getFormattedJdbcPlaceholder(column, newPrefix, parameterName);
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2016-2019 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+
* http://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 org.mybatis.dynamic.sql.util.springbatch;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import org.mybatis.dynamic.sql.render.RenderingStrategy;
22+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
23+
24+
public class SpringBatchUtility {
25+
private SpringBatchUtility() {}
26+
27+
public static final String PARAMETER_KEY = "mybatis3_dsql_query"; //$NON-NLS-1$
28+
29+
public static final RenderingStrategy SPRING_BATCH_READER_RENDERING_STRATEGY =
30+
new SpringBatchReaderRenderingStrategy();
31+
32+
public static Map<String, Object> toParameterValues(SelectStatementProvider selectStatement) {
33+
Map<String, Object> parameterValues = new HashMap<>();
34+
parameterValues.put(PARAMETER_KEY, selectStatement);
35+
return parameterValues;
36+
}
37+
}

src/site/site.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2016-2018 the original author or authors.
4+
Copyright 2016-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* Copyright 2016-2019 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+
* http://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;
17+
18+
import static examples.springbatch.PersonDynamicSqlSupport.*;
19+
import static org.mybatis.dynamic.sql.SqlBuilder.*;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.apache.ibatis.session.SqlSessionFactory;
24+
import org.mybatis.dynamic.sql.select.SelectDSL;
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.MyBatisCursorItemReader;
32+
import org.springframework.batch.core.Job;
33+
import org.springframework.batch.core.Step;
34+
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
35+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
36+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
37+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
38+
import org.springframework.batch.core.launch.support.RunIdIncrementer;
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.batch.test.JobLauncherTestUtils;
43+
import org.springframework.beans.factory.annotation.Autowired;
44+
import org.springframework.context.annotation.Bean;
45+
import org.springframework.context.annotation.Configuration;
46+
import org.springframework.core.convert.converter.Converter;
47+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
48+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
49+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
50+
51+
@EnableBatchProcessing
52+
@Configuration
53+
@MapperScan("examples.springbatch")
54+
public class BatchConfiguration {
55+
56+
@Autowired
57+
private JobBuilderFactory jobBuilderFactory;
58+
59+
@Autowired
60+
private StepBuilderFactory stepBuilderFactory;
61+
62+
@Bean
63+
public DataSource dataSource() {
64+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
65+
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL)
66+
.addScript("classpath:/examples/springbatch/schema.sql")
67+
.addScript("classpath:/examples/springbatch/data.sql")
68+
.build();
69+
return db;
70+
}
71+
72+
@Bean
73+
public BatchConfigurer batchConfigurer() {
74+
return new NoPersistenceBatchConfigurer();
75+
}
76+
77+
@Bean
78+
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
79+
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
80+
bean.setDataSource(dataSource);
81+
return bean;
82+
}
83+
84+
@Bean
85+
public JobLauncherTestUtils jobLauncherTestUtils(Job job) {
86+
return new JobLauncherTestUtils();
87+
}
88+
89+
@Bean
90+
public MyBatisCursorItemReader<Person> reader(SqlSessionFactory sqlSessionFactory) {
91+
SelectStatementProvider selectStatement = SelectDSL.select(person.allColumns())
92+
.from(person)
93+
.where(lastName, isEqualTo("flintstone"))
94+
.build()
95+
.render(SpringBatchUtility.SPRING_BATCH_READER_RENDERING_STRATEGY);
96+
97+
MyBatisCursorItemReader<Person> reader = new MyBatisCursorItemReader<>();
98+
reader.setQueryId(PersonMapper.class.getName() + ".selectMany");
99+
reader.setSqlSessionFactory(sqlSessionFactory);
100+
reader.setParameterValues(SpringBatchUtility.toParameterValues(selectStatement));
101+
return reader;
102+
}
103+
104+
@Bean
105+
public PersonProcessor processor() {
106+
return new PersonProcessor();
107+
}
108+
109+
@Bean
110+
public MyBatisBatchItemWriter<Person> writer(SqlSessionFactory sqlSessionFactory,
111+
Converter<Person, UpdateStatementProvider> convertor) {
112+
MyBatisBatchItemWriter<Person> 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 Converter<Person, UpdateStatementProvider> convertor() {
121+
return new UpdateStatementConvertor();
122+
}
123+
124+
@Bean
125+
public Step step1(ItemReader<Person> reader, ItemProcessor<Person, Person> processor, ItemWriter<Person> writer) {
126+
return stepBuilderFactory.get("step1")
127+
.<Person, Person>chunk(10)
128+
.reader(reader)
129+
.processor(processor)
130+
.writer(writer)
131+
.build();
132+
}
133+
134+
@Bean
135+
public Job importUserJob(Step step1) {
136+
return jobBuilderFactory.get("importUserJob")
137+
.incrementer(new RunIdIncrementer())
138+
.flow(step1)
139+
.end()
140+
.build();
141+
}
142+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2016-2019 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+
* http://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;
17+
18+
import javax.sql.DataSource;
19+
20+
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
21+
import org.springframework.stereotype.Component;
22+
23+
@Component
24+
public class NoPersistenceBatchConfigurer extends DefaultBatchConfigurer {
25+
@Override
26+
public void setDataSource(DataSource dataSource) {
27+
}
28+
}

0 commit comments

Comments
 (0)