java 在 JdbcCursorItemReader 中使用作业参数作为准备好的语句参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12847266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:27:08  来源:igfitidea点击:

Using a job parameter as a prepared statement parameter in a JdbcCursorItemReader

javaspringspring-batch

提问by Nathan Hughes

I have a job that has to run multiple times with different job parameters. I want to set up a JdbcCursorItemReader to perform a query for the job, the itemReader is configured like this:

我有一个作业必须使用不同的作业参数运行多次。我想设置一个 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>

and the job is configured like this:

并且作业配置如下:

<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>

The parameterSetter is pretty minimal:

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);
    }
}

This is using spring-batch 2.1.8.

这是使用 spring-batch 2.1.8。

How do I get the job parameter into the query?

如何将作业参数放入查询中?

I think I'm close, I tried changing the parameter setter config to:

我想我已经接近了,我尝试将参数设置器配置更改为:

        <bean class="com.initech.reports.tps.ParameterSetter">
            <constructor-arg value="#{jobParameters['myParam']}"/>
        </bean>

but I get this error:

但我收到此错误:

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

I found a very similar question, the difference between that one and this is that I don't have a reader class to annotate, I just have the xml entry because I'd like to avoid having to create my own itemReader. (I could try overriding the jdbcCursorItemReader class just to be able to annotate it...)

我发现了一个非常相似的问题,这个问题和这个问题之间的区别在于我没有要注释的阅读器类,我只有 xml 条目,因为我想避免创建自己的 itemReader。(我可以尝试覆盖 jdbcCursorItemReader 类只是为了能够对其进行注释......)

采纳答案by Nathan Hughes

All it took was adding the scope attribute onto the parameterSetter class:

所需要的只是将 scope 属性添加到 parameterSetter 类中:

    <bean class="com.initech.reports.tps.ParameterSetter"
        scope="step">
        <constructor-arg value="#{jobParameters['myParam']}"/>
    </bean>