sql - Spring 批处理 : Passing runtime parameters to query in ItemReader

标签 sql spring-batch

我有一个 spring 批处理程序,它从一个数据库读取并写入一个文件。

它有项目阅读器,如下所示:

<beans:bean id="myItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <beans:property name="dataSource" ref="jobRepository-dataSource" />
    <beans:property name="sql" value="${dbTofileDataReadSQL}"/>
    <beans:property name="rowMapper">
        <beans:bean class="com.mypackage.MyRowMapper" />
    </beans:property>
</beans:bean>

sql是这样的:
select one, two, three, four from myTable where business_date='12/12/11'

这一行将进入我的属性文件:
dbTofileDataReadSQL = select one, two, three, four from myTable where business_date='12/12/11'

如何在项目阅读器中在运行时传递此业务日期,以便将其添加到项目阅读器中。

谢谢阅读!!

最佳答案

您可以使用 late-binding with step scope或简单的 PropertyPlaceholderConfigurer

<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="path to your properties file, either classpath: ... or c:/... " />
</bean>

如果您的属性文件中需要多行条目,请像这样
# sql, multiline property with \
sql=\
SELECT \
ID, \
NAME \
FROM TEST \
ORDER BY ID

或者从 Spring 3 开始,您可以使用 SpEL:
<util:properties id="myProperties" location="..." />
...
<beans:bean ...
   <beans:property name="sql" value="#{myProperties.sql}"/>
</beans:bean>

关于sql - Spring 批处理 : Passing runtime parameters to query in ItemReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8381908/

相关文章:

spring-batch - Spring Batch - 如何防止批处理在数据库中存储事务

mysql - 如何将 MySQL 表转换为另一个具有键值对的表?

SQL 按天分组,显示每天的订单

sql - 如果事务处于 Postgresql 中的可重复读隔离级别,那么事务会看到来自另一个并发事务的插入吗?

SQL 显示结果为零

java - Spring Batch 限制跨多个服务器的单个作业实例

sql server 将选择的结果存储在变量中?

spring - 在 Spring 中使特定方法成为非事务性的

java - Spring批处理如何根据条件跳过整个文件

java - Spring Batch如何设置Chunk tasklet中每次调用之间的时间间隔