Added builder for JdbcCursorItemReader

This provides a builder for the JdbcCursorItemReader to simplify java
configuration.

Resolves BATCH-2555
This commit is contained in:
Michael Minella
2016-11-18 09:18:26 -06:00
parent a6f7ad3373
commit a75b067ebc
6 changed files with 808 additions and 2 deletions

View File

@@ -1,66 +0,0 @@
/*
* Copyright 2006-2013 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.springframework.batch.core.resource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.core.StatementCreatorUtils;
import org.springframework.util.Assert;
/**
* Implementation of the {@link PreparedStatementSetter} interface that accepts
* a list of values to be set on a PreparedStatement. This is usually used in
* conjunction with the {@link JdbcCursorItemReader} to allow for the replacement
* of bind variables when generating the cursor. The order of the list will be
* used to determine the ordering of setting variables. For example, the first
* item in the list will be the first bind variable set. (i.e. it will
* correspond to the first '?' in the SQL statement)
*
* @author Lucas Ward
*
*/
public class ListPreparedStatementSetter implements
PreparedStatementSetter, InitializingBean {
private List<?> parameters;
@Override
public void setValues(PreparedStatement ps) throws SQLException {
for (int i = 0; i < parameters.size(); i++) {
StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, parameters.get(i));
}
}
/**
* The parameter values that will be set on the PreparedStatement.
* It is assumed that their order in the List is the order of the parameters
* in the PreparedStatement.
*/
public void setParameters(List<?> parameters) {
this.parameters = parameters;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(parameters, "Parameters must be provided");
}
}

View File

@@ -28,6 +28,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.database.support.ListPreparedStatementSetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

View File

@@ -34,6 +34,7 @@ import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.support.ListPreparedStatementSetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

View File

@@ -30,7 +30,7 @@
</property>
</bean>
<bean id="pss" class="org.springframework.batch.core.resource.ListPreparedStatementSetter" scope="step">
<bean id="pss" class="org.springframework.batch.item.database.support.ListPreparedStatementSetter" scope="step">
<property name="parameters">
<list>
<value>#{jobParameters['min.id']}</value>