请给我一个springboot + springbatch项目,第一步读出一些数据,第二步把读出的数据写表的例子
时间: 2024-03-27 15:36:41 浏览: 54
Springboot项目示例
好的,以下是一个简单的Spring Boot和Spring Batch项目,用于读取CSV文件中的数据,并将其写入MySQL数据库中的表中。在此示例中,我们将使用Spring Boot版本2.5.4和Spring Batch版本4.3.3。
1. 首先,创建一个Spring Boot项目,并将以下依赖项添加到pom.xml文件中:
```
<dependencies>
<!-- Spring Batch dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<!-- MySQL Connector/J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- Spring JDBC dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Spring Web dependencies (optional) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
```
2. 创建一个包含以下内容的CSV文件:
```
id,name,age
1,John,30
2,Emily,28
3,Mark,35
```
3. 创建一个名为Person.java的POJO类,用于表示CSV文件中的每个记录:
```
public class Person {
private Long id;
private String name;
private Integer age;
// getters and setters
}
```
4. 创建一个名为PersonItemProcessor.java的Spring Batch ItemProcessor类,用于将CSV记录转换为数据库中的Person对象:
```
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
@Override
public Person process(Person person) throws Exception {
return person;
}
}
```
5. 创建一个名为PersonJobConfig.java的Spring Batch作业配置类,用于定义作业步骤:
```
@Configuration
public class PersonJobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource;
@Bean
public FlatFileItemReader<Person> reader() {
return new FlatFileItemReaderBuilder<Person>()
.name("personItemReader")
.resource(new ClassPathResource("data.csv"))
.delimited()
.names(new String[]{"id", "name", "age"})
.fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
setTargetType(Person.class);
}})
.build();
}
@Bean
public JdbcBatchItemWriter<Person> writer() {
return new JdbcBatchItemWriterBuilder<Person>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql("INSERT INTO person (id, name, age) VALUES (:id, :name, :age)")
.dataSource(dataSource)
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Person, Person>chunk(10)
.reader(reader())
.processor(new PersonItemProcessor())
.writer(writer())
.build();
}
@Bean
public Job importPersonJob() {
return jobBuilderFactory.get("importPersonJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
}
```
6. 在application.properties文件中添加以下内容,以配置数据库连接:
```
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
7. 最后,可以在Spring Boot应用程序的Main类中运行作业:
```
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchApplication implements CommandLineRunner {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job importPersonJob;
public static void main(String[] args) {
SpringApplication.run(SpringBatchApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.toJobParameters();
jobLauncher.run(importPersonJob, jobParameters);
}
}
```
这样,我们就创建了一个简单的Spring Boot和Spring Batch项目,用于读取CSV文件中的数据,并将其写入MySQL数据库中的表中。
阅读全文