【问题标题】:How can I use java configuration of spring batch without spring boot?如何在没有spring boot的情况下使用spring批处理的java配置?
【发布时间】:2021-10-11 06:05:57
【问题描述】:

我是 Spring Batch 的新手,我正在努力了解它。 我实现了数据源和批处理配置,但它似乎不起作用,因为

Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is java.sql.SQLSyntaxErrorException: Table '$tableName.batch_job_instance' doesn't exist

DataSourceConfiguration 类:

@Configuration
public class DataSourceConfiguration {
    @Bean
    public DataSource mysqlDataSource() throws SQLException {
        final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new com.mysql.cj.jdbc.Driver());
        dataSource.setUrl("jdbc:mysql://localhost:3306/task4");
        dataSource.setUsername("name");
        dataSource.setPassword("password");
        return dataSource;
    }
}

我有 Batch 配置类,但我不知道 @EnableBatchProcessing 注解在没有 springboot 的情况下是否有效

@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class BatchConfiguration {
    @Autowired
    private JobBuilderFactory jobs;
    @Autowired
    private StepBuilderFactory steps;
    @Autowired
    private DataSource dataSource;
    @Bean
    public ItemReader<User> itemReader() {
        return new JdbcCursorItemReaderBuilder<User>()
                .dataSource(this.dataSource)
                .name("creditReader")
                .sql("select userLogin, userEmail, userPassword, accountBalance from userInfo")
                .rowMapper(new UserRowMapper())
                .build();
    }
    @Bean
    public ItemWriter<User> simpleItemWriter(){
        return new SimpleUserWriter();
    }
    @Bean
    public Job sampleJob() {
        return this.jobs.get("sampleJob")
                .start(step1())
                .build();
    }
    @Bean
    public Step step1() {
        return this.steps.get("step1")
                .<User,User>chunk(10)
                .reader(itemReader())
                .writer(simpleItemWriter())
                .build();
    }
}

主类包含这些代码行:

 public class Demo {
    public static void main(String[] args) throws SQLException {
        ApplicationContext context = new AnnotationConfigApplicationContext(BatchConfiguration.class);
        context.getBean("jobRepository");
        JobLauncher jobLauncher =  context.getBean("jobLauncher",JobLauncher.class);
        Job job =  context.getBean("sampleJob", Job.class);
        try {
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Job Exit Status : "+ execution.getStatus());

        } catch (JobExecutionException e) {
            System.out.println("Job ExamResult failed");
            e.printStackTrace();
        }
    }
}

我该如何解决这个问题?感谢您的帮助

【问题讨论】:

    标签: java mysql spring configuration spring-batch


    【解决方案1】:

    错误似乎是说您缺少 Spring 批处理所需的表。

    查看https://docs.spring.io/spring-batch/docs/4.3.x/reference/html/schema-appendix.html

    也可以在spring批处理核心jar文件org/springframework/bacth/core/schema-&lt;DB&gt;.sql下找到为不同DB引擎准备的SQL文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-24
      • 2020-12-23
      • 2018-03-08
      • 2018-03-31
      • 2020-10-27
      • 2018-10-26
      • 1970-01-01
      • 2016-06-01
      相关资源
      最近更新 更多