Tasklet to delete table in spring batch

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

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

I use ibatis as an ORM

+1
java spring spring-batch ibatis
source share
2 answers

you mean even simpler than tasklet, i.e. how is this pseudo code?

<!-- 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; } } 
+10
source share

Fyi. Instead of the trick, you can use <jdbc:initialize-database> to indicate initialization of the script with all your SQL queries used to initialize db. This way, 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> 

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"> 
-one
source share

All Articles