我不知道为什么这个应用程序没有写入任何文件。我正在从mysql数据库中读取数据并写入csv文件。我可以在itemprocessor日志中看到所需的结果集,但它不会在文件中写入任何内容。我的文件位于此处resources/userinfo.csv。没有错误或异常。花了很多时间来修好它。如果您能帮忙,我们将不胜感激。这是我的密码
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public DataSource dataSource;
@Bean
public JdbcCursorItemReader<UserInfo> reader(){
JdbcCursorItemReader<UserInfo> reader = new
JdbcCursorItemReader<UserInfo>();
reader.setDataSource(dataSource);
reader.setSql("SELECT first_name,last_name FROM db2.userinfo");
reader.setRowMapper(new UserRowMapper());
return reader;
}
@Bean
public UserInfoItemProcessor processor(){
return new UserInfoItemProcessor();
}
@Bean
public FlatFileItemWriter<UserInfo> writer(){
return new FlatFileItemWriterBuilder<UserInfo>()
.name("writer")
.resource(new ClassPathResource("userinfo.csv"))
.delimited()
.delimiter(",")
.names(new String[] {
"first_name",
"last_name"})
.shouldDeleteIfExists(true)
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").<UserInfo, UserInfo> chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
@Bean
public Job exportUserJob() {
return jobBuilderFactory.get("exportUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
}
userrawmapper类
public class UserRowMapper implements RowMapper<UserInfo> {
@Override
public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
UserInfo userInfo = new UserInfo();
userInfo.setFirst_name(rs.getString("first_name"));
userInfo.setLast_name(rs.getString("last_name"));
System.out.println(">>> USERINFO -> " + userInfo.getFirst_name());
// it prints fine here.....
return userInfo;
}
}
用户信息类
public class UserInfo {
private String first_name;
private String last_name;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
}
userinfoitemprocessor.class类
public class UserInfoItemProcessor implements ItemProcessor<UserInfo, UserInfo>{
@Override
public UserInfo process(UserInfo userInfo) throws Exception {
return userInfo;
}
}
应用程序.pr属性
spring.datasource.url=jdbc:mysql://localhost:3306/db2?useSSL=false
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
spring.batch.initialize-schema=always
spring.datasource.initialize=true
暂无答案!
目前还没有任何答案,快来回答吧!