Spring批量JdbcPagingItemReader无限循环

时间:2018-05-16 08:37:57

标签: spring spring-batch

我遇到了JdbcPagingItemReader的问题。我被困在无尽的循环中。我读到itemReader合约必须返回null ,但我没有设法正确实现它。有人可以给我看一个例子吗?

public List<TransactionDTO> getTransactions(Integer chunk, LocalDateTime startDate, LocalDateTime endDate)
            throws Exception {

        final TransactionMapper transactionMapper = new TransactionMapper();

        final SqlPagingQueryProviderFactoryBean sqlPagingQueryProviderFactoryBean = new SqlPagingQueryProviderFactoryBean();
        sqlPagingQueryProviderFactoryBean.setDataSource(dataSource);
        sqlPagingQueryProviderFactoryBean.setSelectClause(env.getProperty("sql.fromdates.select"));
        sqlPagingQueryProviderFactoryBean.setFromClause(env.getProperty("sql.fromdates.from"));
        sqlPagingQueryProviderFactoryBean.setWhereClause(env.getProperty("sql.fromdates.where"));
        sqlPagingQueryProviderFactoryBean.setSortKey(env.getProperty("sql.fromdates.sort"));

        final Map<String, Object> parametros = new HashMap<>();
        parametros.put("startDate", startDate);
        parametros.put("endDate", endDate);

        final JdbcPagingItemReader<TransactionDTO> itemReader = new JdbcPagingItemReader<>();
        itemReader.setDataSource(dataSource);
        itemReader.setQueryProvider(sqlPagingQueryProviderFactoryBean.getObject());
        // TODO esto debe ser el chunk
        itemReader.setPageSize(1);
        itemReader.setFetchSize(1);
        itemReader.setRowMapper(transactionMapper);
        itemReader.afterPropertiesSet();
        itemReader.setParameterValues(parametros);

        ExecutionContext executionContext = new ExecutionContext();
        itemReader.open(executionContext);
        List<TransactionDTO> list = new ArrayList<>();

        TransactionDTO primerDto = itemReader.read();
        while (primerDto != null) {
            list.add(itemReader.read());
        }
        itemReader.close();

        return list;

    }

0 个答案:

没有答案