package com.wisely.ch9_2.batch;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.validator.Validator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;
import com.wisely.ch9_2.domain.Person;
@Configuration
@EnableBatchProcessing
public class TriggerBatchConfig {
@Bean
@StepScope
public FlatFileItemReader<Person> reader(@Value("#{jobParameters['input.file.name']}") String pathToFile) throws Exception {
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>(); //1
reader.setResource(new ClassPathResource(pathToFile)); //2
reader.setLineMapper(new DefaultLineMapper<Person>() {{ //3
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "name","age", "nation" ,"address"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
setTargetType(Person.class);
}});
}});
return reader;
}
@Bean
public ItemProcessor<Person, Person> processor() {
CsvItemProcessor processor = new CsvItemProcessor(); //1
processor.setValidator(csvBeanValidator()); //2
return processor;
}
@Bean
public ItemWriter<Person> writer(DataSource dataSource) {//1
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>(); //2
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
String sql = "insert into person " + "(id,name,age,nation,address) "
+ "values(hibernate_sequence.nextval, :name, :age, :nation,:address)";
writer.setSql(sql); //3
writer.setDataSource(dataSource);
return writer;
}
@Bean
public JobRepository jobRepository(DataSource dataSource, PlatformTransactionManager transactionManager)
throws Exception {
JobRepositoryFactoryBean jobRepositoryFactoryBean = new JobRepositoryFactoryBean();
jobRepositoryFactoryBean.setDataSource(dataSource);
jobRepositoryFactoryBean.setTransactionManager(transactionManager);
jobRepositoryFactoryBean.setDatabaseType("oracle");
return jobRepositoryFactoryBean.getObject();
}
@Bean
public SimpleJobLauncher jobLauncher(DataSource dataSource, PlatformTransactionManager transactionManager)
throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository(dataSource, transactionManager));
return jobLauncher;
}
@Bean
public Job importJob(JobBuilderFactory jobs, Step s1) {
return jobs.get("importJob")
.incrementer(new RunIdIncrementer())
.flow(s1) //1
.end()
.listener(csvJobListener()) //2
.build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader, ItemWriter<Person> writer,
ItemProcessor<Person,Person> processor) {
return stepBuilderFactory
.get("step1")
.<Person, Person>chunk(65000) //1
.reader(reader) //2
.processor(processor) //3
.writer(writer) //4
.build();
}
@Bean
public CsvJobListener csvJobListener() {
return new CsvJobListener();
}
@Bean
public Validator<Person> csvBeanValidator() {
return new CsvBeanValidator<Person>();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SpringBoot实战-汪云飞教程源码
共484个文件
java:207个
xml:99个
properties:30个
5星 · 超过95%的资源 需积分: 9 38 下载量 53 浏览量
2018-08-22
09:55:04
上传
评论
收藏 6.09MB ZIP 举报
温馨提示
SpringBoot实战-汪云飞教程源码,初学springboot可以参照源码学习
资源推荐
资源详情
资源评论
收起资源包目录
SpringBoot实战-汪云飞教程源码 (484个子文件)
WebSecurityConfig.class 5KB
WsController.class 2KB
WebSocketConfig.class 2KB
WebMvcConfig.class 1KB
Ch76ApplicationTests.class 761B
Ch76Application.class 704B
WiselyResponse.class 524B
WiselyMessage.class 452B
postman.crx 4.84MB
bootstrap.css 144KB
bootstrap.css 144KB
bootstrap.min.css 120KB
bootstrap.min.css 120KB
bootstrap.min.css 120KB
jquery-ui.min.css 29KB
bootstrap-theme.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
bootstrap-theme.min.css 23KB
application.css 198B
people.csv 150B
Dockerfile 208B
Dockerfile 206B
Dockerfile 205B
Dockerfile 205B
Dockerfile 203B
Dockerfile 199B
Dockerfile 130B
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
hello.groovy 451B
ws.html 2KB
ws.html 2KB
bootstrap.html 2KB
login.html 2KB
index.html 2KB
action.html 1KB
home.html 1KB
person.html 1KB
index.html 1KB
chat.html 1KB
chat.html 1KB
some.html 861B
login.html 668B
login.html 668B
view1.html 641B
view2.html 422B
angularjs.html 377B
index.html 197B
index.html 197B
404.html 140B
index.html 136B
favicon.ico 5KB
ch7_6.iml 7KB
TriggerBatchConfig.java 5KB
CsvBatchConfig.java 4KB
Ch94Application.java 4KB
MyMvcConfig.java 3KB
DataController.java 3KB
TestControllerIntegrationTests.java 3KB
CustomerSpecs.java 2KB
Ch104ApplicationTests.java 2KB
Ch74Application.java 2KB
ElConfig.java 2KB
SysUser.java 2KB
Ch862Application.java 2KB
WebSecurityConfig.java 2KB
DemoAnnoController.java 2KB
MyMessageConverter.java 2KB
CustomRepositoryFactoryBean.java 2KB
Ch72Application.java 1KB
DataController.java 1KB
CsvBeanValidator.java 1KB
LogAspect.java 1KB
DemoServiceImpl.java 1KB
WebSecurityConfig.java 1KB
WsController.java 1KB
AwareService.java 1KB
DemoServiceImpl.java 1KB
Person.java 1KB
ExceptionHandlerAdvice.java 1KB
TaskExecutorConfig.java 1KB
DemoInterceptor.java 1KB
Person.java 1KB
PersonDao.java 1KB
DemoController.java 1KB
UiController.java 987B
WebInitializer.java 976B
Person.java 960B
Person.java 957B
Person.java 957B
PersonController.java 950B
DemoApplication.java 949B
WebSocketConfig.java 948B
StatusEndPoint.java 948B
Ch935Application.java 910B
UploadController.java 872B
CustomRepositoryImpl.java 835B
JavaConfig.java 819B
共 484 条
- 1
- 2
- 3
- 4
- 5
资源评论
- yangyang_00012019-03-25In action 系列经典书籍,代码可以非常感谢
- taobai0212018-11-30代码可用,非常感谢
- helj2082018-10-10可 以用,做参考,谢谢
- luffy54592018-09-19代码可用,非常感谢。
- awonw2019-04-21代码可用,非常感谢
StrugglingBean
- 粉丝: 22
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功