java Tasklet 在 spring 批处理中删除表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7834304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:37:34  来源:igfitidea点击:

Tasklet to delete a table in spring batch

javaspringibatisspring-batch

提问by user2434

I have steps in the batch job that does different things.

我在批处理作业中有不同的步骤。

But before I begin all these steps, I need to clear a table. Is there any simple way to write a tasklet that will delete the table directly from the job xml file ?

但在开始所有这些步骤之前,我需要清理一张桌子。有没有什么简单的方法可以编写一个 tasklet 来直接从作业 xml 文件中删除表?

I am using ibatis as ORM

我使用 ibatis 作为 ORM

回答by Michael Pralow

you mean even more simple than a tasklet, e.g. like this pseudocode ?

你的意思是比 tasklet 更简单,例如像这个伪代码?

<!-- xml bean config -->
<bean id="deleteTableTaskletStep" class="...">
   <property name="dataSource" ref="dataSource" />
   <property name="sql" value="delete from ..." />
</bean>

// java code
public class DeleteTableTasklet implements Tasklet {

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    new JdbcTemplate(this.dataSource).executeQuery(this.sql)
    return RepeatStatus.FINISHED;
}
}

回答by eureka

FYI, Instead of a tasklet, you can use the <jdbc:initialize-database>to point to an initialization script with all your SQL queries used to initialize the db. That way, the queries will be easier to maintain.

仅供参考,您可以使用<jdbc:initialize-database>指向一个初始化脚本,而不是 tasklet,您的所有 SQL 查询都用于初始化数据库。这样,查询将更容易维护。

<!-- xml bean config -->
<jdbc:initialize-database data-source="dataSource">
       <jdbc:script location="file:C:/db/initial-query.sql" />
</jdbc:initialize-database>

Just remember to include this at the top

请记住将此包含在顶部

<beans xmlns="http://www.springframework.org/schema/beans"
       ...
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="...
           http://www.springframework.org/schema/jdbc
           http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">