Spring启动(spring batch)配置禁用自动创建数据库

时间:2016-09-08 14:27:38

标签: spring-boot spring-batch

我在Spring-Batch中使用Spring-boot,我在application.properties中有数据源配置,但是我不希望spring批处理在我的数据库中创建表,所以我使用以下配置< / p>

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Bean
public Job myJob() {
    return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer())
            .listener(listener()).flow(step2()).end().build();
}

@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(final JobRegistry jobRegistry) {
    JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
    jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
    return jobRegistryBeanPostProcessor;
}

@Bean
public Step step2() {
    LOGGER.info("step2");
    return stepBuilderFactory.get("step2").tasklet(new PurchaseStatusBatch()).build();
}

@Bean
public JobOperator jobOperator() throws Exception {
    SimpleJobOperator jobOperator = new SimpleJobOperator();
    jobOperator.setJobExplorer(jobExplorer());
    jobOperator.setJobLauncher(jobLauncher());
    jobOperator.setJobRegistry(jobRegistry());
    jobOperator.setJobRepository(getJobRepo());
    return jobOperator;
}

@Bean
public JobRegistry jobRegistry() {
    return new MapJobRegistry();
}

@Bean
public JobExplorer jobExplorer() throws Exception {

    MapJobExplorerFactoryBean factoryExplorer = new MapJobExplorerFactoryBean(
            new MapJobRepositoryFactoryBean(getTransactionManager()));
    factoryExplorer.afterPropertiesSet();
    return factoryExplorer.getObject();
}

@Bean
public JobLauncher jobLauncher() throws Exception {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(getJobRepo());
    jobLauncher.afterPropertiesSet();
    return jobLauncher;
}

@Bean
public PlatformTransactionManager getTransactionManager() {
    return new ResourcelessTransactionManager();
}

@Bean
public JobRepository getJobRepo() throws Exception {
    return new MapJobRepositoryFactoryBean(getTransactionManager()).getObject();
}
}

我使用spring.batch.initializer.enabled=false禁用自动创建数据库表

在第一次运行应用程序时,一切正常但是当我尝试工作时,我得到了MySQLSyntaxErrorException:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Table   &#39; BATCH_JOB_INSTANCE&#39;不存在

1 个答案:

答案 0 :(得分:0)

仅仅因为您已禁用表的自动生成,您仍需要某种方法来使这些实体可用。 Spring Batch有两种机制。您可以手动创建这些数据库表,也可以通过Liquibase等模式管理器创建。为此,请使用spring批处理核心资源https://github.com/spring-projects/spring-batch/tree/4.0.0.RELEASE/spring-batch-core/src/main/resources中的SQL文件。否则,您可以使用这些表的内存中表示。