Skip to content

Add Utilities to Support MyBatis Spring batch #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
limitations under the License.

-->
<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">
<!-- Copyright 2016-2019 the original author or authors. Licensed under the
Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied. See the License for
the specific language governing permissions and limitations under the License. -->
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mybatis</groupId>
Expand All @@ -38,7 +47,7 @@
<clirr.comparisonVersion>1.1.0</clirr.comparisonVersion>
<module.name>org.mybatis.dynamic.sql</module.name>
</properties>

<build>
<pluginManagement>
<plugins>
Expand All @@ -52,7 +61,7 @@
</plugins>
</pluginManagement>
</build>

<reporting>
<plugins>
<plugin>
Expand All @@ -64,7 +73,7 @@
</plugin>
</plugins>
</reporting>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -84,7 +93,7 @@
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand All @@ -97,6 +106,12 @@
<version>3.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
Expand All @@ -109,6 +124,24 @@
<version>5.1.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>4.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>4.1.1.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down Expand Up @@ -138,7 +171,7 @@
<url>git:ssh://[email protected]/mybatis/mybatis-dynamic-sql.git?gh-pages#</url>
</site>
</distributionManagement>

<profiles>
<profile>
<id>javadocVersion</id>
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/mybatis/dynamic/sql/util/ValueMapping.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,8 +23,9 @@ public class ValueMapping<T> extends AbstractColumnMapping implements UpdateMapp

private Supplier<T> valueSupplier;

private ValueMapping(SqlColumn<T> column) {
private ValueMapping(SqlColumn<T> column, Supplier<T> valueSupplier) {
super(column);
this.valueSupplier = valueSupplier;
}

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

public static <T> ValueMapping<T> of(SqlColumn<T> column, Supplier<T> valueSupplier) {
ValueMapping<T> mapping = new ValueMapping<>(column);
mapping.valueSupplier = valueSupplier;
return mapping;
return new ValueMapping<>(column, valueSupplier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.springbatch;

import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;

public class SpringBatchCursorReaderSelectModel {

private SelectModel selectModel;

public SpringBatchCursorReaderSelectModel(SelectModel selectModel) {
this.selectModel = selectModel;
}

public SelectStatementProvider render() {
return selectModel.render(SpringBatchUtility.SPRING_BATCH_READER_RENDERING_STRATEGY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.springbatch;

import java.util.HashMap;
import java.util.Map;

import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;

public class SpringBatchPagingReaderSelectModel {

private SelectModel selectModel;

public SpringBatchPagingReaderSelectModel(SelectModel selectModel) {
this.selectModel = selectModel;
}

public SelectStatementProvider render() {
SelectStatementProvider selectStatement =
selectModel.render(SpringBatchUtility.SPRING_BATCH_READER_RENDERING_STRATEGY);
return new LimitAndOffsetDecorator(selectStatement);
}

public class LimitAndOffsetDecorator implements SelectStatementProvider {
private Map<String, Object> parameters = new HashMap<>();
private String selectStatement;

public LimitAndOffsetDecorator(SelectStatementProvider delegate) {
parameters.putAll(delegate.getParameters());

selectStatement = delegate.getSelectStatement()
+ " LIMIT #{_pagesize} OFFSET #{_skiprows}"; //$NON-NLS-1$
}

@Override
public Map<String, Object> getParameters() {
return parameters;
}

@Override
public String getSelectStatement() {
return selectStatement;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.springbatch;

import java.util.Map;

import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;

public class SpringBatchProviderAdapter {

public String select(Map<String, Object> parameterValues) {
SelectStatementProvider selectStatement =
(SelectStatementProvider) parameterValues.get(SpringBatchUtility.PARAMETER_KEY);
return selectStatement.getSelectStatement();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.springbatch;

import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.render.MyBatis3RenderingStrategy;

/**
* This rendering strategy should be used for MyBatis3 statements using one of the
* Spring batch readers supplied by mybatis-spring integration (http://www.mybatis.org/spring/).
* Those readers are MyBatisPagingItemReader and MyBatisCursorItemReader.
*
*/
public class SpringBatchReaderRenderingStrategy extends MyBatis3RenderingStrategy {

@Override
public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName) {
String newPrefix = SpringBatchUtility.PARAMETER_KEY + "." + prefix; //$NON-NLS-1$
return super.getFormattedJdbcPlaceholder(column, newPrefix, parameterName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.springbatch;

import java.util.HashMap;
import java.util.Map;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectDSL;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;

public class SpringBatchUtility {
private SpringBatchUtility() {}

public static final String PARAMETER_KEY = "mybatis3_dsql_query"; //$NON-NLS-1$

public static final RenderingStrategy SPRING_BATCH_READER_RENDERING_STRATEGY =
new SpringBatchReaderRenderingStrategy();

public static Map<String, Object> toParameterValues(SelectStatementProvider selectStatement) {
Map<String, Object> parameterValues = new HashMap<>();
parameterValues.put(PARAMETER_KEY, selectStatement);
return parameterValues;
}

/**
* Select builder that renders in a manner appropriate for the MyBatisPagingItemReader.
*
* <b>Important</b> rendered SQL will contain LIMIT and OFFSET clauses in the SELECT statement. If your database
* (Oracle) does not support LIMIT and OFFSET, the queries will fail.
*
* @param selectList a column list for the SELECT statement
* @return FromGatherer used to continue a SELECT statement
*/
public static QueryExpressionDSL.FromGatherer<SpringBatchPagingReaderSelectModel> selectForPaging(
BasicColumn...selectList) {
return SelectDSL.select(SpringBatchPagingReaderSelectModel::new, selectList);
}

/**
* Select builder that renders in a manner appropriate for the MyBatisCursorItemReader.
*
* @param selectList a column list for the SELECT statement
* @return FromGatherer used to continue a SELECT statement
*/
public static QueryExpressionDSL.FromGatherer<SpringBatchCursorReaderSelectModel> selectForCursor(
BasicColumn...selectList) {
return SelectDSL.select(SpringBatchCursorReaderSelectModel::new, selectList);
}
}
Loading