1、spring boot启动类加注解@EnableTask 、 @EnableBatchProcessing。
2、基于注解@Configuration 的配置类中,定义Job和step,二者需要加@Bean注解,注入到spring 的容器中
3、eclipse启动时,在springboot主类启动参数中加 spring.batch.job.names=jobname , spring.cloud.task.name=taskname。 其中,spring.batch.job.names为指定需要执行的job,job在多次执行过程中不变,spring.cloud.task.name为执行本次任务的命名,未配置可重复执行属性时,task命名不变时,只允许执行一次。多次执行需要修改该属性名。或者清理spring batch / spring cloud task提供的数据表,测试阶段可进行这种骚操作,正式代码需要参考官网文档进行实际的处理
mybatis + spring batch 链接数据库配置,参考:mybatis-spring
springboot启动类 Application
@EnableBatchProcessing
@EnableTask
@SpringBootApplication
public class Application {
public static void mian(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- @EnableBatchProcessing,该注解在应用中只需要出现一次,就能提供JobRepository开箱即用的功能。单独使用该注解,job的启动可通过restful接口调用方式启动job。或者命名行方式启动,具体使用参考spring batch官方文档。
- @EnableTask,该注解在应用中只需要出现一次,就能提供TaskRepository的功能。
- @EnableBatchProcessing、@EnableTask同时使用,job可通过任务调度方式启动。依赖spring cloud task的调度,指定job执行。
基于Configuration的配置类TestConfiguration
定义了job=testjob。
@Lazy
@Configuration
public class TestConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuildFactory stepBuildFactory;
@Autowired
private SqlSessionFactory sqlSessionFactory
//@Bean注解不能少,需要在spring启动时,将构建的命名为testjob的Job注入到容器中,以便启动时能被调用到
@Bean
public Job testJob() throws Exception {
return jobBuilderFactory.get("testjob") //定义job,指定job命名为testjob, 定义多个job时,可在启动时通过spring.batch.job.names=testjob,指定待启动job
.start(testStep())
.build();
}
//@Bean注解不能少,需要在spring启动时,将构建的命名为testjob的Job注入到容器中,以便启动时能被调用到
@Bean
public Step testStep() throws Exception {
return stepBuildFactory.get("teststep")
.<Car, Car> chunk(1) //三个参数, 泛型的I/O, 括号内的为commit interval,一般从配置文件读取。
.reader(reader())
.processor(processor())
.write(writer())
.build();
}
@Bean
@StepScope
public MyBatisCursorItemReader<Car> reader() throws Exception {
return new MyBatisCursorItemReaderBuilder<Car>()
.sqlSessionFactory(sqlSessionFactory)
.queryId("com.test.mapper.Itestquery.queryCar")
.build();
}
@Bean
@StepScope
public CarProcessor processor() {
return new CarProcessor();
}
@Bean
@StepScope
public CarWriter processor() {
return new CarWriter();
}
}
public class CarProcessor implements ItemProcessor<Car, Car> {
@Override
public Car process(Car item) {
System.out.println("执行计划,,,,");
}
}
public class CarWriter<T> implements ItemWriter<T> {
@Override
public void write(List<? extends T> items ) throws Exception {
System.out.println("执行wirter计划,,,");
for(T t:items) {
Car car = (Car)t;
System.out.println(car.toString());
}
}
}
public class Car{
private String carname;
private String price;
private String getCarname() {
return carname;
}
private void setCarname(String carname) {
this.carname = carname;
}
private String getPrice() {
return price;
}
private void setPrice(String price) {
this.price = price;
}
}
TestConfiguration中数据库的连接配置
hql语句定义在 com.test.mapper.TestMapper.xml中,存放路径为classpath下的src/main/resource
<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd ">
<mapper namespace="com.test.mapper.ITestMapper">
<resultMap id="basemap" type="com.test.Car">
<arg column=""/>
<arg column=""/>
</resultMap>
<sql id="base_list">
carname, price
</sql>
<select id="queryCar" resultMap="basemap">
select
<include refid="base_list" />
from CarTable
</select>
</mapper>
调用接口类定义com.test.mapper.ITestMapper ,接口类路径src/main/java
@Mapper
public interface ITestMapper {
List<Map> queryCar();
}
SqlSessionFactory 自定义
@Configuration
public class SqlSessionFactoryConfig {
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/test/mapper/*.xml"));
return factoryBean.getObject();
}
}