java-在JdbcCursorItemReader中将作业参数用作准备好的语句参数
作者:互联网
我有一个作业必须使用不同的作业参数运行多次.我想设置一个JdbcCursorItemReader来执行作业查询,itemReader的配置如下:
<bean id="tpsItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="myDataSource"/>
<property name="sql" value="#{sqlQueries['tps.findStuffforSomeSubset']}"/>
<property name="preparedStatementSetter">
<bean class="com.initech.reports.tps.ParameterSetter">
<!-- can't hardcode this, I want a job parameter here -->
<constructor-arg value="A"/>
</bean>
</property>
<property name="rowMapper">
<bean class="com.initech.reports.tps.CustomerRowMapper"/>
</property>
</bean>
作业配置如下:
<batch:job id="tpsReportJob">
<batch:step id="tpsReportJob.generateReport">
<batch:tasklet>
<batch:chunk reader="tpsItemReader"
processor="tpsItemProcessor"
writer="tpsItemWriter" commit-interval="100000"/>
</batch:tasklet>
</batch:step>
</batch:job>
parameterSetter非常小:
package com.initech.reports.tps;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.jdbc.core.PreparedStatementSetter;
public class ParameterSetter implements PreparedStatementSetter {
private final String x;
public ParameterSetter(String x) {this.x = x;}
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, x);
}
}
这是使用spring-batch 2.1.8.
如何将job参数放入查询中?
我想我接近了,我尝试将参数设置器配置更改为:
<bean class="com.initech.reports.tps.ParameterSetter">
<constructor-arg value="#{jobParameters['myParam']}"/>
</bean>
但是我得到这个错误:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:208)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:72)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:52)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:88)
at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:139)
... 51 more
我发现a very similar question,它与这个之间的区别是我没有要注释的阅读器类,我只有xml条目,因为我想避免不得不创建自己的itemReader. (我可以尝试重写jdbcCursorItemReader类,以便能够对其进行注释…)
解决方法:
所要做的就是将scope属性添加到parameterSetter类上:
<bean class="com.initech.reports.tps.ParameterSetter"
scope="step">
<constructor-arg value="#{jobParameters['myParam']}"/>
</bean>
标签:spring-batch,spring,java 来源: https://codeday.me/bug/20191127/2074812.html