gpt4 book ai didi

mysql - Spring batch itemwriter删除mysql中的记录

转载 作者:行者123 更新时间:2023-11-29 06:42:50 27 4
gpt4 key购买 nike

我一直在尝试 spring batch 示例,但我无法通过 itemwriter 删除数据库中的记录。 itemreader 工作正常,但在 itemwriter 中我不知道如何工作,因为我是 spring batch 的新手,可能我会犯一些错误。我从一些示例中尝试了以下代码,但它不起作用。我是否必须编写其他类(class)或其他内容?

<bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<!-- Why CDATA?

because < etc. is not allowed for xml values
when you use &lt; xml parser will work, but
now the sql won't because of the & spring assumes
a placeholder, see
- AbstractSqlPagingQueryProvider.init(...)
- JdbcParameterUtils.countParameterPlaceholders(...)

-->
<value>
<![CDATA[
DELETE FROM TEST
WHERE id = ?
and sub.id = ?
and ...
]]>
</value>
</property>
<property name="itemPreparedStatementSetter">
<bean class="...FieldSetItemPreparedStatementSetter" />
</property>
</bean>

ItemPreparedStatementSetter的实现

/**
* Implementation for {@link ItemPreparedStatementSetter},
* sets the values from {@link FieldSet}.
*
*/
public class FieldSetItemPreparedStatementSetter implements ItemPreparedStatementSetter<FieldSet> {

/** {@inheritDoc} */
@Override
public void setValues(FieldSet item, PreparedStatement ps) throws SQLException {
for (int i = 0; i < item.getValues().length; i++) {
// PreparedStatements start with 1
ps.setObject(i + 1, item.getValues()[i]);
}
}
}

我的错误是:-

 Encountered an error executing the step
java.lang.ClassCastException: com.mkyong.User cannot be cast to org.springframework.batch.item.file.transform.FieldSet
at com.mkyong.FieldSetItemPreparedStatementSetter.setValues(FieldSetItemPreparedStatementSetter.java:1)
at org.springframework.batch.item.database.JdbcBatchItemWriter$1.doInPreparedStatement(JdbcBatchItemWriter.java:190)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:617)
at org.springframework.batch.item.database.JdbcBatchItemWriter.write(JdbcBatchItemWriter.java:186)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:395)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:267)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:253)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:137)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:152)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:131)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:134)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:127)
at com.mkyong.App.run(App.java:34)
at com.mkyong.App.main(App.java:16)

Dec 06, 2013 3:50:56 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=testJob]] completed with the following parameters: [{}] and the following status: [FAILED]
Exit Status : FAILED
Exit Status : [java.lang.ClassCastException: com.mkyong.User cannot be cast to org.springframework.batch.item.file.transform.FieldSet]
Done

最佳答案

代码已“正式更正”,但您必须声明您的 ItemPreparedStatementSetter使用 User而不是 FieldSet因为ItemWriter使用由 ItemReader 创建的相同对象;我想您的读者返回类型为 User 的对象.

替换为ItemPreparedStatementSetter<User>setValues(...)根据新的通用有界类型。

关于mysql - Spring batch itemwriter删除mysql中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20417998/

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