Tasklet to delete a table in spring batch

后端 未结 3 766
借酒劲吻你
借酒劲吻你 2020-12-12 03:45

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 tha

3条回答
  •  -上瘾入骨i
    2020-12-12 04:13

    For batch Java config. Step:

    @Bean
    private Step dropTable() {
      return stepBuilderFactory
        .get("dropTable")
        .transactionManager(transactionManager)
        .tasklet(dropTableTasklet())
        .build();
    }
    

    Tasklet:

    private Tasklet dropTableTasklet() {
      return (contribution, chunkContext) -> {
        new JdbcTemplate(this.dataSource).execute(DROP_SCRIPT);
        return RepeatStatus.FINISHED;
      };
    }
    

    Script (SQL server):

    private static final String DROP_SCRIPT = "IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES "
      + "WHERE TABLE_NAME = 'some_table') "
      + "BEGIN "
      + " DROP TABLE some_table "
      + "END";
    

提交回复
热议问题