我正在使用Spring批处理将数据从csv文件转储到数据库,但每当我运行我的springboot应用程序时,csv文件中的数据不断追加到数据库。
每当我运行我的springboot应用程序时,csv文件中的数据不断地附加到数据库中。
@Bean
public Job importUserJob(JobCompletionNotificationListener listener) {
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step1())
.end()
.build();
}
应用.属性
server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/database
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=false
spring.batch.jdbc.initialize-schema=always
spring.datasource.initialize=true
spring.sql.init.schema-locations=classpath:schema.sql
spring.mvc.pathmatch.matching-strategy= ANT_PATH_MATCHER
itemreader
@Bean
public FlatFileItemReader<Covid> reader() {
FlatFileItemReader<Covid> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("Cdata.csv"));
reader.setLinesToSkip(1);
reader.setLineMapper(new DefaultLineMapper<Covid>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setDelimiter(",");
setStrict(false);
setNames(new String[] {"id","firstname",lastname,ph});
setIncludedFields(new int[] {0,1,2,3});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Covid>() {{
setTargetType(Covid.class);
}});
}});
return reader;
}
1条答案
按热度按时间vxbzzdmp1#
默认情况下,Spring Batch框架是启用的,并且作业在Sping Boot 应用程序的每次启动时运行。为了避免在启动时处理输入文件,您可以在www.example.com中禁用Spring Batchapplication.properties: