gpt4 book ai didi

java - 如何使用spring批处理在postgresql中执行存储过程?

转载 作者:行者123 更新时间:2023-12-02 10:03:21 29 4
gpt4 key购买 nike

我正在使用 Spring Batch 进行批处理。首先,它读取一个 csv 文件,之后,我想在数据库中更新,为此我使用存储过程对每行执行不同的操作。代码是这样的:

Reader:

@Bean
@StepScope
public ItemReader<Comprobante> csvFileReader( @Value("#{jobParameters['filename']}") String filename) throws UnexpectedInputException, ParseException{
FlatFileItemReader<Comprobante> reader = new FlatFileItemReader<>();
reader.setResource(new FileSystemResource(fileDir + filename));
reader.setName("CSV-Reader");
reader.setLinesToSkip(1);
reader.setLineMapper(lineMapper());
reader.open(new ExecutionContext());
return reader;
}


@Bean
public LineMapper<Comprobante> lineMapper() {
DefaultLineMapper<Comprobante> defaultLineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
delimitedLineTokenizer.setDelimiter(",");
delimitedLineTokenizer.setStrict(false);
delimitedLineTokenizer.setNames(new String[] {"cid", "ruc", "tipo", "serie", "numero","fecha", "monto"});
BeanWrapperFieldSetMapper<Comprobante> beanWrapperFieldSetMapper = new BeanWrapperFieldSetMapper<>();
beanWrapperFieldSetMapper.setTargetType(Comprobante.class);
defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
defaultLineMapper.setFieldSetMapper(beanWrapperFieldSetMapper);
return defaultLineMapper;
}

Writer:

@Bean
public ItemWriter<Comprobante> csvFileWrite (DataSource dataSource, NamedParameterJdbcTemplate jdbcTemplate){
JdbcBatchItemWriter<Comprobante> writer = new JdbcBatchItemWriter<>();
writer.setDataSource(dataSource);
writer.setJdbcTemplate(jdbcTemplate);
writer.setSql("select validateVoucher(? , ?);"); // This is the code with error.
ItemPreparedStatementSetter<Comprobante> valueSetter = new comprobantePreparedStatementSetter();
writer.setItemPreparedStatementSetter(valueSetter);
return writer;
}


日志显示此消息:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select validateVoucher(? , ?);]; nested exception is java.sql.BatchUpdateException: Batch entry 0 select validateVoucher('40883884' , '0') was aborted: ERROR: function validatevoucher(character varying, character varying) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 8 Call getNextException to see other errors in the batch.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1444) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:632) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:646) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]

.......

最佳答案

您将需要创建自己的 ItemWriter 实现来处理此调用。框架内没有任何现成的东西支持写入存储过程。

关于java - 如何使用spring批处理在postgresql中执行存储过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55478280/

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