Spring Batch SQL Command with JobParameters

痞子三分冷 提交于 2019-12-23 02:45:08

问题


I am new to spring-batch, here i am getting some data from DB using following reader statements. Here i need to pass value dynamically(thru arguments).

<bean id="ItemReader"
            class="org.springframework.batch.item.database.JdbcCursorItemReader">
            <property name="dataSource" ref="dataSource" />
            <property name="sql">
                <value>
                <![CDATA[
    select * from table where section = #{jobParameters['section']}
    ]]>
                </value>
            </property>
            <property name="rowMapper">
                <bean class="xyzRowMapper" />
            </property>
        </bean>

JUnit Code:

JobParameters jobParameters = = new JobParametersBuilder()
                    .addString("section", section);

Can any body help on this?


回答1:


As explained in §5.4 Late Binding of Job and Steps Attributes of official Spring Batch documentation, you need to add scope="step" to your step :

Using a scope of Step is required in order to use late binding since the bean cannot actually be instantiated until the Step starts, which allows the attributes to be found. Because it is not part of the Spring container by default, the scope must be added explicitly, either by using the batch namespace or by including a bean definition explicitly for the StepScope (but not both)

Giving this :

<bean id="ItemReader" scope="step" class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <property name="dataSource" ref="dataSource" />
    <property name="sql">
        <value>
            <![CDATA[
                select * from table where section = #{jobParameters['section']}
            ]]>
        </value>
    </property>
    <property name="rowMapper">
        <bean class="xyzRowMapper" />
    </property>
</bean>


来源:https://stackoverflow.com/questions/34899327/spring-batch-sql-command-with-jobparameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!