本文主要是介绍springbatch自学之路-03(将数据库替换为mysql),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1.添加依赖
2.添加数据库数据
3.执行job
4.运行日志
5.查看数据库已经建好了springbatch相关的数据表
1.添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.springbatch</groupId><artifactId>demo_03</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></dependency><!--<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency>--><!--将数据库转换成mysql begin--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--将数据库转换成mysql end--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.batch</groupId><artifactId>spring-batch-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.添加数据库数据
spring.datasource.url=jdbc:mysql:///springbatch_test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#用于初始化springbatch相关的表
spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
spring.batch.initialize-schema=always
3.执行job
package com.springbatch._01helloworld;import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 任务配置器** @Package: com.springbatch._01helloworld* @ClassName: jobConfig* @author: zq* @since: 2020/5/22 21:00* @version: 1.0* @Copyright: 2020 zq. All rights reserved.*/
@Configuration
public class JobConfig {//创建 创建job的对象@Autowiredprivate JobBuilderFactory jobBuilderFactory;//创建 创建step的对象@Autowiredprivate StepBuilderFactory stepBuilderFactory;@Beanpublic Job helloWorldJob() {return jobBuilderFactory.get("helloWorldJob").start(helloWorldStep()).build();}@Beanpublic Step helloWorldStep() {return stepBuilderFactory.get("helloWorldStep").tasklet(new Tasklet() {@Overridepublic RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {System.out.println("hello world !!!");return RepeatStatus.FINISHED;}}).build();}}
package com.springbatch._01helloworld;import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBatchDemo项目启动类** @Package: PACKAGE_NAME* @ClassName: com.springbatch._01helloworld.SpringBatchConfig* @author: zq* @since: 2020/5/22 20:31* @version: 1.0* @Copyright: 2020 zq. All rights reserved.*/
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {public static void main(String[] args) {SpringApplication.run(SpringBatchConfig.class, args);}}

4.运行日志
5.查看数据库已经建好了springbatch相关的数据表
这篇关于springbatch自学之路-03(将数据库替换为mysql)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!