1.导入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.23</version>
</dependency>
<!-- csv依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
`
2.创建数据库,数据表,并且保证字段属性要与实体类中数据类型对应 创建实体类:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Test {
Date time;
String base_station_name;
String integrity;
String energy_consumption;
String manufactor;
}
3.创建SpringBatch的配置类
@Configuration
public class CsvBatchJonConfig {
// 注入JobBuilderFactory,用来构建job
@Autowired
JobBuilderFactory jobBuilderFactory;
// 注入StepBuilderFactory,用来构建step
@Autowired
StepBuilderFactory stepBuilderFactory;
// 持久化操作----数据源
@Autowired
DataSource dataSource;
// 持久化操作----mybatis-plus
@Autowired
TestMapper testMapper;
// 配置一个ItemReader,即数据的读取逻辑
@Bean
@StepScope
FlatFileItemReader<Test> itemReader() {
// FlatFileItemReader 是一个加载普通文件的 ItemReader
FlatFileItemReader<Test> reader = new FlatFileItemReader<>();
// 由于data.csv文件第一行是标题,因此通过setLinesToSkip方法设置跳过一行
reader.setLinesToSkip(2);
// setResource方法配置data.csv文件的位置
reader.setResource(new ClassPathResource("子报表.csv"));
// 通过setLineMapper方法设置每一行的数据信息
reader.setLineMapper(new DefaultLineMapper<Test>(){{
setLineTokenizer(new DelimitedLineTokenizer(){{
// setNames方法配置了data.csv文件一共有4列,分别是id、username、以及sex,
setNames("time","base_station_name","integrity","energy_consumption","manufactor");
// 配置列与列之间的间隔符(这里是空格)
setDelimiter(",");
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper(){{
setTargetType(Test.class);
}});
}});
return reader;
}
// 配置ItemWriter,即数据的写出逻辑
@Bean
JdbcBatchItemWriter jdbcBatchItemWriter() {
// 使用的JdbcBatchltemWriter则是通过JDBC将数据写出到一个关系型数据库中。
JdbcBatchItemWriter writer = new JdbcBatchItemWriter();
// 配置使用的数据源
writer.setDataSource(dataSource);
// 配置数据插入SQL,注意占位符的写法是":属性名"
writer.setSql("insert into test(time,base_station_name,integrity,energy_consumption,manufactor) " +
"values(:time,:base_station_name,:integrity,:energy_consumption,:manufactor)");
// 最后通过BeanPropertyItemSqlParameterSourceProvider实例将实体类的属性和SQL中的占位符一一映射
writer.setItemSqlParameterSourceProvider(
new BeanPropertyItemSqlParameterSourceProvider<>());
return writer;
}
// 配置一个Step
@Bean
Step csvStep() {
// Step通过stepBuilderFactory进行配置
return stepBuilderFactory.get("csvStep") //通过get获取一个StepBuilder,参数数Step的name
.<Test, Test>chunk(2) //方法的参数2,表示每读取到两条数据就执行一次write操作
.reader(itemReader()) // 配置reader
.writer(jdbcBatchItemWriter()) // 配置writer
.build();
}
// 配置一个Job
@Bean
Job csvJob() {
// 通过jobBuilderFactory构建一个Job,get方法参数为Job的name
return jobBuilderFactory.get("csvJob")
.start(csvStep()) // 配置该Job的Step
.build();
}
}
`
4.主启动类添加开启springbatch
5.编写一个controller接口进行测试
`
@RestController
public class Hello {
//JobLauncher 由框架提供
@Autowired
JobLauncher jobLauncher;
// Job 为刚才配置的
@Autowired
Job job;
@GetMapping("/hello")
public String hello(){
try {
JobParameters jobParameters = new JobParametersBuilder()
.toJobParameters();
// 通过调用 JobLauncher 中的 run 方法启动一个批处理
jobLauncher.run(job, jobParameters);
} catch (Exception e) {
e.printStackTrace();
}
return "hello world";
}
}
`
6.完成!