启动类
package com.springbatch._09item_reader_from_db;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBatchDemo项目启动类
*/
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {
public static void main(String[] args) {
SpringApplication.run(SpringBatchConfig.class, args);
}
}
配置类
package com.springbatch._09item_reader_from_db;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.RowMapper;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class ItemReaderFromDBJobConfig {
//创建 创建job的对象
@Autowired
private JobBuilderFactory jobBuilderFactory;
//创建 创建step的对象
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource;
@Autowired
private ItemWriter<? super User> itemWriteFromDb;
@Bean
public Job itemReaderFromDBJob() {
return jobBuilderFactory.get("ItemReaderFromDBJobConfig-01")
.start(itemReaderFromDBJobStep())
.build();
}
@Bean
public Step itemReaderFromDBJobStep() {
return stepBuilderFactory.get("itemReaderFromDBJobStep-01")
.<User, User>chunk(2)
.reader(itemReaderFromDB())
.writer(itemWriteFromDb)
.build();
}
@Bean
@StepScope
public JdbcPagingItemReader<User> itemReaderFromDB() {
JdbcPagingItemReader<User> jdbcPagingItemReader = new JdbcPagingItemReader<>();
jdbcPagingItemReader.setDataSource(dataSource);
jdbcPagingItemReader.setFetchSize(2);
jdbcPagingItemReader.setRowMapper(new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt(1));
user.setName(resultSet.getString(2));
user.setAge(resultSet.getInt(3));
return user;
}
});
MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
provider.setSelectClause("id,name,age");
provider.setFromClause("from user");
Map<String, Order> map = new HashMap<>(1);
map.put("id", Order.ASCENDING);
provider.setSortKeys(map);
jdbcPagingItemReader.setQueryProvider(provider);
return jdbcPagingItemReader;
}
}
对象类
package com.springbatch._09item_reader_from_db;
/**
* 用户
*
* @Package: com.springbatch._09item_reader_from_db
* @ClassName: User
* @author: zq
* @since: 2020/6/21 21:34
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
public class User {
/**
* id
*/
private int id;
/**
* name
*/
private String name;
/**
* age
*/
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("User{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append('}');
return sb.toString();
}
}
方法
package com.springbatch._09item_reader_from_db;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 写方法
*
*/
@Component("itemWriteFromDb")
public class ItemWriteFromDb implements ItemWriter<User> {
@Override
public void write(List<? extends User> list) throws Exception {
System.out.println("writing====================");
for (User user : list) {
System.out.println(user);
}
}
}
application.properties
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
server.port=8081
spring.batch.job.enabled=true
#spring.batch.job.names=parentJob