Skip to content

Commit fdbbbe0

Browse files
authored
Merge pull request mybatis#76 from jeffgbutler/spring-batch
Add Utilities to Support MyBatis Spring batch
2 parents 7969f39 + c91cbac commit fdbbbe0

21 files changed

+1177
-14
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://git@github.com/mybatis/mybatis-dynamic-sql.git?gh-pages#</url>
139172
</site>
140173
</distributionManagement>
141-
174+
142175
<profiles>
143176
<profile>
144177
<id>javadocVersion</id>

src/main/java/org/mybatis/dynamic/sql/util/ValueMapping.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,8 +23,9 @@ public class ValueMapping<T> extends AbstractColumnMapping implements UpdateMapp
2323

2424
private Supplier<T> valueSupplier;
2525

26-
private ValueMapping(SqlColumn<T> column) {
26+
private ValueMapping(SqlColumn<T> column, Supplier<T> valueSupplier) {
2727
super(column);
28+
this.valueSupplier = valueSupplier;
2829
}
2930

3031
public T value() {
@@ -37,8 +38,6 @@ public <R> R accept(UpdateMappingVisitor<R> visitor) {
3738
}
3839

3940
public static <T> ValueMapping<T> of(SqlColumn<T> column, Supplier<T> valueSupplier) {
40-
ValueMapping<T> mapping = new ValueMapping<>(column);
41-
mapping.valueSupplier = valueSupplier;
42-
return mapping;
41+
return new ValueMapping<>(column, valueSupplier);
4342
}
4443
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.select.SelectModel;
19+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
20+
21+
public class SpringBatchCursorReaderSelectModel {
22+
23+
private SelectModel selectModel;
24+
25+
public SpringBatchCursorReaderSelectModel(SelectModel selectModel) {
26+
this.selectModel = selectModel;
27+
}
28+
29+
public SelectStatementProvider render() {
30+
return selectModel.render(SpringBatchUtility.SPRING_BATCH_READER_RENDERING_STRATEGY);
31+
}
32+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.select.SelectModel;
22+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
23+
24+
public class SpringBatchPagingReaderSelectModel {
25+
26+
private SelectModel selectModel;
27+
28+
public SpringBatchPagingReaderSelectModel(SelectModel selectModel) {
29+
this.selectModel = selectModel;
30+
}
31+
32+
public SelectStatementProvider render() {
33+
SelectStatementProvider selectStatement =
34+
selectModel.render(SpringBatchUtility.SPRING_BATCH_READER_RENDERING_STRATEGY);
35+
return new LimitAndOffsetDecorator(selectStatement);
36+
}
37+
38+
public class LimitAndOffsetDecorator implements SelectStatementProvider {
39+
private Map<String, Object> parameters = new HashMap<>();
40+
private String selectStatement;
41+
42+
public LimitAndOffsetDecorator(SelectStatementProvider delegate) {
43+
parameters.putAll(delegate.getParameters());
44+
45+
selectStatement = delegate.getSelectStatement()
46+
+ " LIMIT #{_pagesize} OFFSET #{_skiprows}"; //$NON-NLS-1$
47+
}
48+
49+
@Override
50+
public Map<String, Object> getParameters() {
51+
return parameters;
52+
}
53+
54+
@Override
55+
public String getSelectStatement() {
56+
return selectStatement;
57+
}
58+
}
59+
}
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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.BasicColumn;
22+
import org.mybatis.dynamic.sql.render.RenderingStrategy;
23+
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
24+
import org.mybatis.dynamic.sql.select.SelectDSL;
25+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
26+
27+
public class SpringBatchUtility {
28+
private SpringBatchUtility() {}
29+
30+
public static final String PARAMETER_KEY = "mybatis3_dsql_query"; //$NON-NLS-1$
31+
32+
public static final RenderingStrategy SPRING_BATCH_READER_RENDERING_STRATEGY =
33+
new SpringBatchReaderRenderingStrategy();
34+
35+
public static Map<String, Object> toParameterValues(SelectStatementProvider selectStatement) {
36+
Map<String, Object> parameterValues = new HashMap<>();
37+
parameterValues.put(PARAMETER_KEY, selectStatement);
38+
return parameterValues;
39+
}
40+
41+
/**
42+
* Select builder that renders in a manner appropriate for the MyBatisPagingItemReader.
43+
*
44+
* <b>Important</b> rendered SQL will contain LIMIT and OFFSET clauses in the SELECT statement. If your database
45+
* (Oracle) does not support LIMIT and OFFSET, the queries will fail.
46+
*
47+
* @param selectList a column list for the SELECT statement
48+
* @return FromGatherer used to continue a SELECT statement
49+
*/
50+
public static QueryExpressionDSL.FromGatherer<SpringBatchPagingReaderSelectModel> selectForPaging(
51+
BasicColumn...selectList) {
52+
return SelectDSL.select(SpringBatchPagingReaderSelectModel::new, selectList);
53+
}
54+
55+
/**
56+
* Select builder that renders in a manner appropriate for the MyBatisCursorItemReader.
57+
*
58+
* @param selectList a column list for the SELECT statement
59+
* @return FromGatherer used to continue a SELECT statement
60+
*/
61+
public static QueryExpressionDSL.FromGatherer<SpringBatchCursorReaderSelectModel> selectForCursor(
62+
BasicColumn...selectList) {
63+
return SelectDSL.select(SpringBatchCursorReaderSelectModel::new, selectList);
64+
}
65+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy