Springboot整合SpringBatch完成基本案例--从数据库读取数据
并写⼊⽂件
本案例旨在让新⼿从0开始完成⼀个批量任务的开发
第⼀步:建表,⼊参
CREATE TABLE `music_info` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`singer_name` varchar(100) NOT NULL COMMENT '歌⼿名',
`music_size` varchar(100) NOT NULL COMMENT '歌曲⼤⼩',
`music_name` varchar(100) NOT NULL COMMENT '歌曲名',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
insert into `music_info`(`id`,`singer_name`,`music_size`,`music_name`) values (1,'⼩三','3.2M','起风了'),(2,'刘德华','3.0M','忘情⽔'),(3,'猪点点','5.0M','会写程序的⼩猪');
第⼆步:搭建SpringBoot项⽬,使⽤IDEA或者Spring官⽹均可
第三步:导⼊依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
第四步:启动类上加注解@EnableBatchProcessing @SpringBootApplication
@EnableBatchProcessing
梁欢微博public class SpringbatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbatchApplication.class, args);
}
}
第五步:添加配置⽂件内容
#开发配置
#数据库连接参数的配置
spring.datasource.driverClassName = sql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 123456
#项⽬启动时的建表sql脚本,该脚本由Spring Batch提供
spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
#在项⽬启动时进⾏执⾏建表sql
#是否⽣成执⾏状态记录的表结构
spring.batch.initialize-schema=always
#禁⽌Spring Batch⾃动执⾏,既需要⽤户触发才能执⾏
spring.abled=true
第六步:创建实体类
public class Music {
// 主键id
private Integer id;
// 歌⼿名神代
private String singer_name;
// 歌曲⼤⼩
private String music_size;
// 歌曲名
private String music_name;
public Integer getId() {
}
public void setId(Integer id) {
this.id = id;
}读取配置文件失败
public String getSinger_name() {
return singer_name;
}
public void setSinger_name(String singer_name) {
this.singer_name = singer_name;
}
public String getMusic_size() {
return music_size;
}
public void setMusic_size(String music_size) {
this.music_size = music_size;
}
public String getMusic_name() {
return music_name;
}
public void setMusic_name(String music_name) {
this.music_name = music_name;
}
@Override
public String toString() {
return "Music{" +
"id=" + id +
", singer_name='" + singer_name + '\'' +
", music_size='" + music_size + '\'' +
", music_name='" + music_name + '\'' +
'}';
}
}
第七步:创建数据持久层代码
public interface MusicDao {
//通过id查询数据库记录
@Select("select id , singer_name , music_size , music_name from music_info where id = #{id};") public List<Music> queryInfoById(Map<String , Integer> map);
}
第⼋步:编写批量代码
@Configuration
public class BatchJobDemo {
@Autowired
JobBuilderFactory jobBuilderFactory;
@Autowired
StepBuilderFactory stepBuilderFactory;
@Autowired
SqlSessionFactory sqlSessionFactory;
private static final String JOB = "job";
private static final String STEP = "step";
//配置⼀个Job
家属宣告王相军遗体搜救结束@Bean(name = JOB)
Job job() {
(JOB)
.start(step())
.
build();
}
//配置⼀个Step
@Bean(name = STEP)
Step step() {
(STEP)
.<Music, Music>chunk(2)
.reader(itemReader())
.writer(itemWriter())
.build();
}
/
/配置itemReader
@Bean("itemReader")
@StepScope
MyBatisCursorItemReader<Music> itemReader() {
System.out.println("开始查询数据库");
MyBatisCursorItemReader<Music> reader = new MyBatisCursorItemReader<>();
Map<String , Object> map = new HashMap<>();
map.put("id" , 2);
reader.setQueryId("ample.springbatch.dao.MusicDao.queryInfoById");
reader.setSqlSessionFactory(sqlSessionFactory);
reader.setParameterValues(map);
return reader;
}
//配置itemWriter
@Bean("itemWriter")
@StepScope
FlatFileItemWriter<Music> itemWriter() {
System.out.println("开始写⼊⽂件中");
FlatFileItemWriter<Music> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("F:\\"));//系统⽬录
//将Music对象转换成字符串,并输出到⽂件
writer.setLineAggregator(new LineAggregator<Music>() {
@Override
public String aggregate(Music music) {
ObjectMapper mapper = new ObjectMapper();
String str = null;
try {
str = mapper.writeValueAsString(music);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return str;
}
雷峰夕照
});
return writer;
}
}
第⼋步:在磁盘根⽬录创建⽂件
第九步:启动服务
第⼗步:⽂件中的结果
{"id":2,"singer_name":"刘德华","music_size":"3.0M","music_name":"忘情⽔"}
发布评论