I feel like this is a really basic thing, but I can't find docs on how to do it. All the documentation/examples I'm finding assumes static queries. I can do this as a static query, but I want to know how to do it with variables. I'm trying to use Spring Batch with Postgres.
What I'm looking to do is a query like this:
SELECT * from SOME_TABLE WHERE SOURCE = ? AND (EXPIRES BETWEEN ? AND ?)
I've tried various ways of write the query, e.g replacing question marks with variables like :source. I'm not even sure if I'm using the correct ItemReader classes or if I need to write my own. This is my config:
@Bean
protected JdbcPagingItemReader<JpaEntitlement> itemReader(DataSource dataSource)
throws Exception {
JdbcPagingItemReader<JpaEntitlement> pagingItemReader = new JdbcPagingItemReader<>();
pagingItemReader.setDataSource(dataSource);
pagingItemReader.setPageSize(1);
PagingQueryProvider pagingQueryProvider = createQueryProvider(dataSource);
pagingItemReader.setQueryProvider(pagingQueryProvider);
pagingItemReader.setRowMapper(new BeanPropertyRowMapper<>(JpaClass.class));
return pagingItemReader;
}
private PagingQueryProvider createQueryProvider(DataSource dataSource) throws Exception {
SqlPagingQueryProviderFactoryBean pagingQueryProvider =
new SqlPagingQueryProviderFactoryBean();
pagingQueryProvider.setSelectClause("*");
pagingQueryProvider.setFromClause("FROM SOME_TABLE");
pagingQueryProvider.setWhereClause("WHERE SOURCE = ? AND (EXPIRES between ? AND ?)");
pagingQueryProvider.setDataSource(dataSource);
return pagingQueryProvider.getObject();
}
I guess the ultimate question is this: Is there something included in Spring Batch to do this? If not, what should I override to add this functionality?
To add, this is something that need to process in batches, as it's going to be potentially thousands of records.