Tasklet to delete a table in spring batch

给你一囗甜甜゛ 提交于 2019-11-30 06:04:04

问题


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 ?

I am using ibatis as ORM


回答1:


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

<!-- 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;
}
}



回答2:


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.

<!-- 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">


来源:https://stackoverflow.com/questions/7834304/tasklet-to-delete-a-table-in-spring-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!