PB使用JdbcBatchItemWriter和JAVA代码中的CompositeItemWriter - Spring-Batch [英] PB using JdbcBatchItemWriter with CompositeItemWriter in JAVA code- Spring-Batch

查看:1110
本文介绍了PB使用JdbcBatchItemWriter和JAVA代码中的CompositeItemWriter - Spring-Batch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展了官方Spring-Batch网站提供的批处理服务代码 - 批处理服务并修改了ItemWriter以生成CSV并写入数据库。

I have extended the batch service code provided by Official Spring-Batch site - Batch Processing Service and modified the ItemWriter to generate the CSV and to write to the database.

我使用CompositeItemWriter写入CSV和数据库。
但是JdbcBatchItemWriter与CompositeItemWriter无法正常工作。代码如下所示。

I have used CompositeItemWriter to write in both CSV and Database. However the JdbcBatchItemWriter is not working correctly with CompositeItemWriter. The code is shown below.

    @Bean
public ItemWriter<Person> writer(DataSource dataSource) {
CompositeItemWriter<Person> cWriter = new CompositeItemWriter<Person>();   

// For DataBase 
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
    writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
    writer.setDataSource(dataSource);

// For CSV
FlatFileItemWriter<Person> csvWriter = new FlatFileItemWriter<Person>();
csvWriter.setResource(new FileSystemResource(new File("./csv/new-data.csv")));
csvWriter.setShouldDeleteIfExists(true);
DelimitedLineAggregator<Person> lineAggregator = new DelimitedLineAggregator<Person>();
lineAggregator.setDelimiter(","); 

BeanWrapperFieldExtractor<Person> fieldExtractor = new BeanWrapperFieldExtractor<Person>();
String[] names = {"firstName", "lastName"};
fieldExtractor.setNames(names);
lineAggregator.setFieldExtractor(fieldExtractor);
csvWriter.setLineAggregator(lineAggregator);

List<ItemWriter<? super Person>> mWriter = new ArrayList<ItemWriter<? super Person>>();
mWriter.add(writer); // **Comment this line and the code works fine**
mWriter.add(csvWriter);
cWriter.setDelegates(mWriter);
    return cWriter;
}

评论此行 - mWriter.add(writer);运行代码。这表明CompositeItemWriter适用于FlatFileitemWriter,但不适用于JdbcBatchItemWriter。我得到的错误是 -

Comment this line - mWriter.add(writer); to run the code. This shows that CompositeItemWriter is working good with FlatFileitemWriter, but not with JdbcBatchItemWriter. The error I am getting is -

or.springframework.jdbc.BadSqlGrammerException: PreparedStatementCallback; bad SQL grammar 
[insert into people(first_name, last_name) VALUES (:firstName, :lastName)]

Caused by: Syntax error in SQl statement "insert into people((first_name, last_name) VALUES (:[*]firstName, :lastName)"; expected"), DEFAULT, NOT, EXISTS, INTERSECTS, SELECT, FROM"; SQL Statement:

如何解决JdbcBatchItemWriter与CompositeItemWriter一起正常工作?

How can I resolve JdbcBatchItemWriter to work correctly with CompositeItemWriter ?

推荐答案

jdbc auto config的问题在多个itemWriters的情况下没有运行。请以相同的顺序添加最后一行: -

Its the problem with jdbc auto config which was not running in case of multiple itemWriters. Pls add the last line in same sequence :-

JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
writer.setDataSource(dataSource);

writer.afterPropertiesSet();

这篇关于PB使用JdbcBatchItemWriter和JAVA代码中的CompositeItemWriter - Spring-Batch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆