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 source code, Java EE 颠覆者,Springboot action source cod (936个子文件)
CustomerSpecs$1.class 5KB
WebSecurityConfig.class 5KB
MyMvcConfig.class 5KB
TestControllerIntegrationTests.class 4KB
DataController.class 4KB
MyMessageConverter.class 4KB
WebSecurityConfig.class 3KB
Ch862Application.class 3KB
SysUser.class 3KB
Ch94Application.class 3KB
DemoAnnoController.class 3KB
ElConfig.class 3KB
DataController.class 2KB
WsController.class 2KB
LogAspect.class 2KB
ExceptionHandlerAdvice.class 2KB
DemoInterceptor.class 2KB
AwareService.class 2KB
PersonDao.class 2KB
Person.class 2KB
CustomRepositoryFactoryBean$CustomRepositoryFactory.class 2KB
WebSocketConfig.class 2KB
Ch72Application.class 2KB
CustomRepositoryImpl.class 2KB
Person.class 2KB
Main.class 2KB
PersonHystrixService.class 2KB
WebInitializer.class 2KB
Main.class 2KB
DemoRestController.class 2KB
Person.class 2KB
UploadController.class 1KB
Ch623Application.class 1KB
Ch935Application.class 1KB
Main.class 1KB
DemoListener.class 1KB
CustomerSpecs.class 1KB
ScheduledTaskService.class 1KB
DataController.class 1KB
CustomServletContainer.class 1KB
UiController.class 1KB
Ch522Application.class 1KB
PushService.class 1KB
TaskExecutorConfig.class 1KB
DemoBeanIntegrationTests.class 1KB
SseController.class 1KB
Main.class 1KB
Ch77Application.class 1KB
Main.class 1KB
Person.class 1KB
AsyncTaskService.class 1KB
Ch934Application.class 1KB
AdviceController.class 1KB
Main.class 1KB
Main.class 1KB
CustomUserService.class 1KB
CustomRepositoryFactoryBean.class 1KB
Main.class 1KB
SomeApplication.class 1KB
JavaConfig.class 1KB
Person.class 1KB
ContentController.class 1KB
ConditionConifg.class 1KB
WindowsCondition.class 1KB
WebMvcConfig.class 1KB
PrePostConfig.class 1KB
LinuxCondition.class 1KB
WebMvcConfig.class 1KB
AysncController.class 1KB
NormalController.class 1KB
Main.class 1KB
SomeHystrixService.class 1KB
HomeController.class 1KB
WebMvcConfig.class 1KB
Msg.class 1KB
Main.class 1KB
Main.class 1017B
ConverterController.class 999B
AuthorSettings.class 994B
Ch82Application.class 988B
ProfileConfig.class 976B
DemoPublisher.class 973B
UiApplication.class 967B
TestConfig.class 967B
Ch523Application.class 965B
DemoObj.class 959B
Main.class 946B
MonitorApplication.class 932B
MyRestController.class 930B
SysRole.class 929B
Receiver.class 924B
Receiver.class 923B
Person.class 915B
PersonRepository.class 909B
UseFunctionService.class 872B
JSR250WayService.class 871B
Person.class 861B
UseFunctionService.class 830B
Location.class 820B
CustomRepository.class 811B
共 936 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
zabaglione
- 粉丝: 14
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功