Spring 使用自定义 FieldSetMapper 的批处理不保存数据?
Spring Batch with custom FieldSetMapper not saving data?
我需要将一个大的 .csv 文件加载到我的数据库中,我有一个引用了分销商的价格实体:
public class Price {
@Id
private String id;
private Date dtComu;
private Double price;
@ManyToOne
@JoinColumn(name = "idDistributor")
private Distributor distributor;
//constrcutor, getters&setters
我尝试上传的包含价格数据的 csv 参考了每个价格的分销商 ID。在 ItemReader 上我有:
public FlatFileItemReader<Price> priceReader() {
FlatFileItemReader<Price> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("file.csv"));
reader.setLinesToSkip(2);
reader.setRecordSeparatorPolicy(recordSeparatorPolicy);
reader.setLineMapper(new DefaultLineMapper<Price>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setStrict(false);
setDelimiter(";");
setNames(new String[] { "idDistributor", "price", "dtComu" });
}
});
setFieldSetMapper(customMapper());
}
});
return reader;
}
我的 customMapper 是:
public class CustomMapper implements FieldSetMapper<Price> {
@Autowired
DistributorRepository repository;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
@Override
public Price mapFieldSet(FieldSet fieldSet) throws BindException {
Distributor distributor = repository.findById(fieldSet.readInt("idDistributor")).orElse(null);
if (distributore == null) {
return null;
}
Price p = new Price();
p.setDistributor(distributor);
p.setPrice(fieldSet.readDouble("price"));
try {
p.setDtComu(formatter.parse(fieldSet.readString("dtComu")));
} catch (ParseException e) {
// TODO Auto-generated catch block
p.setDtComu(new Date());
}
//here i will crate an ID for the Price that i need to always be unique
p.setId(distributor.getIdImpianto()+ fieldSet.readString("dtComu"));
return p; }}
我的Writer如下:
public JdbcBatchItemWriter<Price> prezzoWriter() {
JdbcBatchItemWriter<price> writer = new JdbcBatchItemWriter<Price>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Prezzo>());
;
writer.setSql("INSERT INTO price (id_distributor, price, dt_comu) "
+ "VALUES (:idDistributor, :price, :dtComu");
writer.setDataSource(dataSource);
return writer;
}
我 运行 程序并在没有保存任何内容的情况下完成。我可能做错了什么吗?我刚开始使用 Sring Batch,必须详细说明一些复杂的东西..
JdbcBatchItemWriter
对 JPA 上下文一无所知。您需要使用 DataSourceTransactionManager
才能正常工作。
我需要将一个大的 .csv 文件加载到我的数据库中,我有一个引用了分销商的价格实体:
public class Price {
@Id
private String id;
private Date dtComu;
private Double price;
@ManyToOne
@JoinColumn(name = "idDistributor")
private Distributor distributor;
//constrcutor, getters&setters
我尝试上传的包含价格数据的 csv 参考了每个价格的分销商 ID。在 ItemReader 上我有:
public FlatFileItemReader<Price> priceReader() {
FlatFileItemReader<Price> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("file.csv"));
reader.setLinesToSkip(2);
reader.setRecordSeparatorPolicy(recordSeparatorPolicy);
reader.setLineMapper(new DefaultLineMapper<Price>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setStrict(false);
setDelimiter(";");
setNames(new String[] { "idDistributor", "price", "dtComu" });
}
});
setFieldSetMapper(customMapper());
}
});
return reader;
}
我的 customMapper 是:
public class CustomMapper implements FieldSetMapper<Price> {
@Autowired
DistributorRepository repository;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
@Override
public Price mapFieldSet(FieldSet fieldSet) throws BindException {
Distributor distributor = repository.findById(fieldSet.readInt("idDistributor")).orElse(null);
if (distributore == null) {
return null;
}
Price p = new Price();
p.setDistributor(distributor);
p.setPrice(fieldSet.readDouble("price"));
try {
p.setDtComu(formatter.parse(fieldSet.readString("dtComu")));
} catch (ParseException e) {
// TODO Auto-generated catch block
p.setDtComu(new Date());
}
//here i will crate an ID for the Price that i need to always be unique
p.setId(distributor.getIdImpianto()+ fieldSet.readString("dtComu"));
return p; }}
我的Writer如下:
public JdbcBatchItemWriter<Price> prezzoWriter() {
JdbcBatchItemWriter<price> writer = new JdbcBatchItemWriter<Price>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Prezzo>());
;
writer.setSql("INSERT INTO price (id_distributor, price, dt_comu) "
+ "VALUES (:idDistributor, :price, :dtComu");
writer.setDataSource(dataSource);
return writer;
}
我 运行 程序并在没有保存任何内容的情况下完成。我可能做错了什么吗?我刚开始使用 Sring Batch,必须详细说明一些复杂的东西..
JdbcBatchItemWriter
对 JPA 上下文一无所知。您需要使用 DataSourceTransactionManager
才能正常工作。