spring boot(spring batch)配置禁用自动创建数据库
Posted
技术标签:
【中文标题】spring boot(spring batch)配置禁用自动创建数据库【英文标题】:Spring boot ( spring batch) configuration disable the automatic creation of the database 【发布时间】:2017-01-16 13:01:36 【问题描述】:我将 Spring-boot 与 Spring-Batch 一起使用,我的 application.properties 中有数据源配置,但我不希望 Spring Batch 在我的数据库中创建表,因此我使用以下配置
@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: 表 “BATCH_JOB_INSTANCE”不存在
【问题讨论】:
【参考方案1】:仅仅因为您禁用了表的自动生成,您仍然需要一些方法来使这些实体可用。 Spring Batch 有两种机制。您可以手动或通过 Liquibase 等模式管理器创建这些数据库表。为此,请使用 spring 批处理核心资源https://github.com/spring-projects/spring-batch/tree/4.0.0.RELEASE/spring-batch-core/src/main/resources 中的 SQL 文件。否则,您可以使用这些表的内存表示。
【讨论】:
以上是关于spring boot(spring batch)配置禁用自动创建数据库的主要内容,如果未能解决你的问题,请参考以下文章
spring-boot-starter-jta-atomikos 和 spring-boot-starter-batch
Spring boot spring.batch.job.enabled=false 无法识别
Spring boot spring.batch.job.enabled=false 无法识别
spring boot(spring batch)配置禁用自动创建数据库