1、spring boot啟動(dòng)類加注解@EnableTask 颠猴、 @EnableBatchProcessing洛巢。
2、基于注解@Configuration 的配置類中,定義Job和step援岩,二者需要加@Bean注解歼狼,注入到spring 的容器中
3、eclipse啟動(dòng)時(shí)享怀,在springboot主類啟動(dòng)參數(shù)中加 spring.batch.job.names=jobname 羽峰, spring.cloud.task.name=taskname。 其中添瓷,spring.batch.job.names為指定需要執(zhí)行的job梅屉,job在多次執(zhí)行過程中不變,spring.cloud.task.name為執(zhí)行本次任務(wù)的命名鳞贷,未配置可重復(fù)執(zhí)行屬性時(shí)坯汤,task命名不變時(shí),只允許執(zhí)行一次悄晃。多次執(zhí)行需要修改該屬性名玫霎。或者清理spring batch / spring cloud task提供的數(shù)據(jù)表妈橄,測試階段可進(jìn)行這種騷操作庶近,正式代碼需要參考官網(wǎng)文檔進(jìn)行實(shí)際的處理
mybatis + spring batch 鏈接數(shù)據(jù)庫配置,參考:mybatis-spring
springboot啟動(dòng)類 Application
@EnableBatchProcessing
@EnableTask
@SpringBootApplication
public class Application {
public static void mian(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- @EnableBatchProcessing眷蚓,該注解在應(yīng)用中只需要出現(xiàn)一次鼻种,就能提供JobRepository開箱即用的功能。單獨(dú)使用該注解沙热,job的啟動(dòng)可通過restful接口調(diào)用方式啟動(dòng)job叉钥。或者命名行方式啟動(dòng)篙贸,具體使用參考spring batch官方文檔投队。
- @EnableTask,該注解在應(yīng)用中只需要出現(xiàn)一次爵川,就能提供TaskRepository的功能敷鸦。
- @EnableBatchProcessing、@EnableTask同時(shí)使用寝贡,job可通過任務(wù)調(diào)度方式啟動(dòng)扒披。依賴spring cloud task的調(diào)度,指定job執(zhí)行圃泡。
基于Configuration的配置類TestConfiguration
定義了job=testjob碟案。
@Lazy
@Configuration
public class TestConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuildFactory stepBuildFactory;
@Autowired
private SqlSessionFactory sqlSessionFactory
//@Bean注解不能少,需要在spring啟動(dòng)時(shí)颇蜡,將構(gòu)建的命名為testjob的Job注入到容器中价说,以便啟動(dòng)時(shí)能被調(diào)用到
@Bean
public Job testJob() throws Exception {
return jobBuilderFactory.get("testjob") //定義job辆亏,指定job命名為testjob, 定義多個(gè)job時(shí)熔任,可在啟動(dòng)時(shí)通過spring.batch.job.names=testjob褒链,指定待啟動(dòng)job
.start(testStep())
.build();
}
//@Bean注解不能少,需要在spring啟動(dòng)時(shí)疑苔,將構(gòu)建的命名為testjob的Job注入到容器中甫匹,以便啟動(dòng)時(shí)能被調(diào)用到
@Bean
public Step testStep() throws Exception {
return stepBuildFactory.get("teststep")
.<Car, Car> chunk(1) //三個(gè)參數(shù), 泛型的I/O, 括號內(nèi)的為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("執(zhí)行計(jì)劃,薪贫,恍箭,,");
}
}
public class CarWriter<T> implements ItemWriter<T> {
@Override
public void write(List<? extends T> items ) throws Exception {
System.out.println("執(zhí)行wirter計(jì)劃瞧省,扯夭,,");
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中數(shù)據(jù)庫的連接配置
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>
調(diào)用接口類定義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();
}
}