gpt4 book ai didi

java - Spring 批处理3.0.3 : How to set custom params for JdbcPagingItemReader query?

转载 作者:行者123 更新时间:2023-11-30 03:32:52 27 4
gpt4 key购买 nike

JdbcPagingItemReader 查询设置自定义参数的最佳方法是什么?

我的自定义JdbcPagingItemReader实现:

public class CustomItemReader extends JdbcPagingItemReader<Long> {
public CustomItemReader(DataSource dataSource) throws Exception {
SqlPagingQueryProviderFactoryBean queryProvider = new SqlPagingQueryProviderFactoryBean();
queryProvider.setDataSource(dataSource);
queryProvider.setSelectClause("SELECT t1.id");
queryProvider.setFromClause("FROM table1 t1 LEFT JOIN table2 t2 ON t2.fk_table1_id = t1.id");
queryProvider.setWhereClause("WHERE (t1.col1 = :param1) AND ((t2.id IS NULL) OR (t2.col3 = :param2))");
queryProvider.setSortKey("t1.id");

setDataSource(dataSource);
setFetchSize(10);
setRowMapper(new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong(1);
}
});
setQueryProvider(queryProvider.getObject());
}
}

最佳答案

要将参数从作业的 ExecutionContext 传递到 JdbcPagingItemReader,您需要按如下方式配置编写器:

<bean id="itemReader" class="org.springframework.batch.item.database.JdbcPagingItemReader" scope="step">
<property name="dataSource" ref="dataSource" />
<property name="rowMapper">
<bean class="MyRowMapper" />
</property>
<property name="queryProvider">
<bean class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="sortKeys">
<map>
<entry key="t1.id" value="ASCENDING"/>
</map>
</property>
<property name="selectClause" value="SELECT t1.id" />
<property name="fromClause" value="FROM table1 t1 LEFT JOIN table2 t2 ON t2.fk_table1_id = t1.id" />
<property name="whereClause" value=""WHERE (t1.col1 = :param1) AND ((t2.id IS NULL) OR (t2.col3 = :param2))" />
</bean>
</property>
<property name="pageSize" value="10" />
<property name="parameterValues">
<map>
<entry key="param1" value="#{jobExecutionContext[param1]}" />
<entry key="param2" value="#{jobExecutionContext[param2]}" />
</map>
</property>
</bean>

您可以在此处的 Spring Batch 示例中查看类似的配置:https://github.com/spring-projects/spring-batch/tree/master/spring-batch-samples

关于java - Spring 批处理3.0.3 : How to set custom params for JdbcPagingItemReader query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28595846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com