spring 批处理,
目录
1.添加依赖2.添加配置信息3.在项目启动类上添加@EnableBatchProcessing注解开开启跳羚队支持4.配置批处理5.实体类6.创建控制器弹簧批次是一个开源的、全面的、轻量级的批处理框架,通过Spring Boot可以实现强大的批处理应用程序开发。
春季批次提供了项目阅读器、项目处理器和项目作者来完成数据的读取、处理及写出操作,并且可以将批处理的执行状态持久化到数据库中。
https://spring.io/projects/spring-batch
1.添加依赖
依赖groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-batch/artifact id/dependency依赖groupIdorg.springframework.boot/groupId artifactId spring-boot-starter-JDBC/artifactId/依赖项依赖groupIdcom.alibaba/groupId人工德鲁伊-弹簧-启动-启动/人工版本1 .1 .9/version/dependency依赖关系groupId MySQL/groupId artifact id MySQL-connector-Java/artifact id/dependency
2.添加配置 信息
服务器。port=9000 spring。http。编码。力-响应=真正的弹簧。数据来源。type=com。阿里巴巴。德鲁伊。游泳池。druiddatasourcespling。数据来源。URL=JDBC :我的SQL :///批处理spring。数据来源。用户名=root spring。数据来源。密码=kangxg 198811 #项目启动时创建数据表的结构化查询语言脚本春天。数据来源。schema=类路径:/org/spring framework/batch/core/schema-MySQL。SQL #项目启动时执行建表sqlspring。批量。initialize-schema=始终#配置后不会自动执行spring.batch.job.enabled=false
3.在项目启动类上添加@EnableBatchProcessing注解开开启Spring Batch支持
@ spring boot application @ enablebatchprocessing public类批处理应用程序{ public static void main(String[]args){ spring应用程序。运行(批处理应用程序。class,args);} }
4.配置批处理
@配置公共类CsvBatchJobConfig { @ Autowired JobBuilderFactory JobBuilderFactory;@ Autowired StepBuilderFactory StepBuilderFactory;@自动连线数据源数据源;类CustomLineCallbackHandler实现LineCallbackHandler { @ Override public void handleLine(String result){ system。牛津大学
t.println(result); } } @Bean ItemReader<User> itemReader() { FlatFileItemReader<User> reader = new FlatFileItemReader<User>(); reader.setLinesToSkip(1); reader.setResource(new ClassPathResource("data.csv")); reader.setLineMapper(new DefaultLineMapper<User>(){ { setLineTokenizer(new DelimitedLineTokenizer(){{ setNames(new String[]{"id","username","address","gender"}); //setDelimiter("t"); }}); setFieldSetMapper(new BeanWrapperFieldSetMapper<User>(){{ setTargetType(User.class); }}); } }); reader.setSkippedLinesCallback(new CustomLineCallbackHandler()); return reader; } @Bean JdbcBatchItemWriter jdbcBatchItemWriter() { JdbcBatchItemWriter writer = new JdbcBatchItemWriter(); writer.setDataSource(dataSource); writer.setSql("insert into user(id,username,address,gender)" + "values(:id,:username,:address,:gender)"); writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider()); return writer; } @Bean Step csvStep() { return stepBuilderFactory.get("csvStep") .<User,User>chunk(2) .reader(itemReader()) .writer(jdbcBatchItemWriter()) .build(); } @Bean Job csvJob() { return jobBuilderFactory.get("csvJob") .start(csvStep()) .build(); } }
5.实体类
public class User { private Integer id; private String username; private String address; private String gender; public User(Integer id,String username,String address,String gender) { this.username= username; this.id = id; this.address = address; this.gender = gender; } public User() { super(); } public void setId(Integer id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setGender(String gender) { this.gender = gender; } public void setAddress(String address) { this.address = address; } public String getUsername() { return username; } public String getGender() { return gender; } public Integer getId() { return id; } public String getAddress() { return address; } @Override public String toString() { return "User{" + "id=" + id + ", username=" + username + + ", address=" + address + + ", gender=" + gender + + }; }}
6.创建Controller
@RestControllerpublic class BatchController { @Autowired JobLauncher jobLauncher; @Autowired Job job; @GetMapping("/batch") public void batch() { try { jobLauncher.run(job,new JobParametersBuilder().toJobParameters()); }catch (Exception e) { e.printStackTrace(); } } }
这里配置就结束了,数据库会增加一些自动创建到表,但是User表不能自动创建和字段需要提前设置,否则会出现错误,还需要进一步处理,待完成。暂做标记!
到此这篇关于SpringBoot批处理的实现示例的文章就介绍到这了,更多相关SpringBoot批处理内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。