4

In my project I have read multiple tables with different queries and consolidate those results sets in flat files. How do I achieve that. I mean JdbcReader is directly taking 1 select query, how can I customize it.

4

1 回答 1

5

如果 JdbcCursorItemReader 不适合您的需要,您始终可以通过实现 ItemReader 接口来自由地实现自定义阅读器。

public interface ItemReader<T> {
        T read() throws Exception, UnexpectedInputException, ParseException;
}

只需编写一个实现该接口的类,并注入一个 jdbcTemplate 即可查询多个表。

public Class MyCompositeJdbcReader implements ItemReader<ConsolidateResult>{
    private JdbcTemplate jdbcTemplate;

    public ConsolidateResult read() 
       throws Exception, UnexpectedInputException, ParseException{
    ConsolidateResult cr = new ConsolidateResult();     

    String name= this.jdbcTemplate.queryForObject(
        "select name from customer where id = ?",new Object[]{1}, String.class);
    String phoneNumber= this.jdbcTemplate.queryForObject(
        "select phone from customer_contact where custid = ?",
        new Object[]{1},String.class);

    cr.setName(name);
    cr.setPhone(phoneNumber);
    return cr;
 }

}

我没有编译代码,但我希望它能给出一个想法。

于 2012-07-14T16:59:13.443 回答