Raj*_*eev 5 spring spring-batch
有一个 csv 文件有 100 列,但我们只需要 3-5 列需要加载到数据库中。
我不想在作业 xml 中指定 linetokenizer 中的所有 100 列。
请建议我们在这种情况下如何进行
小智 4
尝试使用自定义 fieldSetMapper。您可以像使用带索引的 ResultSet 一样使用它。仅当您想要自动映射时才必须列出所有列名称。仅指定分隔符,在您的情况下为“,”
<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="YOURFILE" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="fieldSetMapper">
<bean class="CUSTOMFIELDSETMAPPER" />
</property>
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value="," />
</bean>
</property>
</bean>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
自定义映射器可能是这样的,假设您想读取第 1 列和第 25 列:
public class CustomMapper implements FieldSetMapper<CustomPOJO>{
@Override
public CustomPOJO mapFieldSet(FieldSet fieldSet) throws BindException {
CustomPOJO result = new CustomPOJO();
result.setName(fieldSet.readString(0));
result.setAddress(fieldSet.readString(24));
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何使用阅读器的更多说明,请参阅本教程
| 归档时间: |
|
| 查看次数: |
2863 次 |
| 最近记录: |