如何在 Spring 批处理中处理此用例 - 不同的名称?

How to handle this use case in Spring Batch - Different Names?

有一个包含许多记录的 CSV 文件,其中 CSV header 名称和数据库域 object 名称不同。如何获取 CSV 数据并创建模型以在 Spring 批次中持久保存在数据库中?下面抛出错误,因为 header 和 db 或 jpa 名称不同

示例,employee.csv

Employee Id, Employee Name, Employee Address, Employee Address 2, Date Of Birth

域名ObjectEmployee.java

public string empId;   
public string empName;   
public string empAddress;   
public string empAddress2;   
public string empDOB;

平面文件出现错误reader

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepScope;
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.mapping.FieldSetMapper;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;

@Component
@JobScope
public class EmployeeFlatFileItemReader extends FlatFileItemReader<Employee> implements StepExecutionListener {

private String fileName;
private String filePath;

@Override
public void beforeStep(StepExecution stepExecution) {
    fileName = (String) stepExecution.getJobExecution().getJobParameters().getString("fileName");  
    filePath = (String) stepExecution.getJobExecution().getJobParameters().getString("filePath");  
    setResource(new FileSystemResource(filePath));  
}

public EmployeeFlatFileItemReader() {
    try {
        //init();
        DefaultLineMapper<Employee> lineMapper = new DefaultLineMapper<Employee>() {
            {
                setLineTokenizer(new DelimitedLineTokenizer() {
                    {
                        setNames("Employee Id" + "," + "Employee Name" + "," + "Employee Address" + "," + "Employee Address 2" + "," + "Date Of Birth");
                    }
                });
                setFieldSetMapper(new BeanWrapperFieldSetMapper<Employee>() {
                    {
                        setTargetType(Employee.class); // Throws error
                    }
                });
            }
        };
        setLineMapper(lineMapper);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    return stepExecution.getExitStatus();
}

}

您正在 setNames 方法中传递单个字符串:

setNames("Employee Id" + "," + "Employee Name" + "," + "Employee Address" + "," + "Employee Address 2" + "," + "Date Of Birth");

此方法采用与域类型中的字段名称相对应的字符串数组,而不是文件的 header(应使用 setLinesToSkip(1).[=14 的方式跳过=]

所以你应该用类似的东西更新那行:

setNames("empId" , "empName", "empAddress", "empAddress2", "empDOB");

字段名称的顺序应与它们在 csv 文件中的顺序相同。