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>();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
《Java EE开发的颠覆者Spring Boot实战》(图书作者汪云飞 ISBN9787121282089)源码集.zip (463个子文件)
config 254B
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
spring.factories 131B
FETCH_HEAD 117B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 74B
.gitignore 43B
.gitignore 43B
.gitignore 22B
.gitignore 12B
hello.groovy 451B
HEAD 130B
HEAD 23B
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
some.html 861B
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
pack-1e2561356dac71ee3eb2db552ad647bd76176f65.idx 26KB
index 54KB
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
共 463 条
- 1
- 2
- 3
- 4
- 5
资源评论
九转成圣
- 粉丝: 5165
- 资源: 2961
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 从XML生成可与Ajax共同使用的JSON中文WORD版最新版本
- silverlight通过WebService连接数据库中文WORD版最新版本
- 使用NetBeans连接SQLserver2008数据库教程中文WORD版最新版本
- XPath实例中文WORD版最新版本
- XPath语法规则中文WORD版最新版本
- XPath入门教程中文WORD版最新版本
- ORACLE数据库管理系统体系结构中文WORD版最新版本
- Sybase数据库安装以及新建数据库中文WORD版最新版本
- tomcat6.0配置oracle数据库连接池中文WORD版最新版本
- hibernate连接oracle数据库中文WORD版最新版本
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功