FlatFileItemReader 和 FlatFileItemWriter 使用批处理作业
FlatFileItemReader and FlatFileItemWriter Using Batch Job
我想从文本文件中读取、解析和写入数据。我做到了这一点,但我对标识符的声明有疑问。我在下面显示我的class。
CustomerDataMapper.java
public class CustomerDataMapper implements FieldSetMapper<Customer> {
private static Log log = LogFactory.getLog(CustomerDataMapper.class);
@Override
public RawData mapFieldSet(FieldSet fieldSet) throws BindException {
Customer customer = new Customer();
try {
customer.setCustNum(fieldSet.readInt("id"));
customer.setNameSurname(fieldSet.readString("name"));
}catch (Exception e){
log.error("Mapping error, " + e);
}
return customer;
}
}
Customer.java
@Component
@Scope("prototype")
public class Customer implements Serializable{
private Integer custNum;
private String nameSurname;
public String getCustNum) {
return custNum;
}
public void setCustNum(String custNum) {
this.custNum = custNum;
}
public String getNameSurname() {
return nameSurname;
}
public void setNameSurname(String nameSurname) {
this.nameSurname = nameSurname;
}
}
fileManager.xml
<bean id="customerFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name= "encoding" value ="Cp1254"/>
<property name="resource" value="#{jobParameters['processPath']}"/>
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
<property name="names" value="id, name"/>
<property name="columns" ref="1-9, 10-30"/>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="mapper.CustomerDataMapper ">
<property name="fileName" value="#{jobParameters['fileNameAndTime']}"/>
</bean>
</property>
</bean>
</property>
<bean id="customerFileItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource"/>
<property name="sql">
<value>
<![CDATA[
insert into CUSTOMER(CUST_NUM, NAME_SURNAME)
values (:custNum, :nameSurname)
]]>
</value>
</property>
<property name="itemSqlParameterSourceProvider">
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider"/>
</property>
<bean id = "customerSkipListener" class="listener.CustomerSkipListener"/>
<batch:job id="customerImportJob">
<batch:step id="smsPasswordDataImportStep">
<batch:tasklet>
<batch:chunk reader="customerFileItemReader" writer="customerFileItemWriter" commit-interval="1" skip-limit="1000">
<batch:skippable-exception-classes>
<batch:include class="java.lang.Exception"/>
<batch:exclude class="java.io.FileNotFoundException"/>
</batch:skippable-exception-classes> <batch:listeners>
<batch:listener ref="customerSkipListener" />
</batch:listeners>
</batch:chunk>
</batch:tasklet>
</batch:step>
我想问一下,下面这个class可以用在FieldSetMapper中吗?如果我在定义标识符时不使用 Camelcase 表示法,则无法写入数据库。有没有关于这个的方法或方法。非常感谢。
Customer.java
@Component
@Scope("prototype")
public class Customer implements Serializable{
private Integer Cust_Num;
private String Name_Surname;
public String getCust_Num() {
return Cust_Num;
}
public void setCust_Num(String Cust_Num) {
this.Cust_Num = Cust_Num;
}
public String getName_Surname() {
return Name_Surname;
}
public void setName_Surname(String Name_Surname) {
this.Name_Surname = Name_Surname;
}
}
I want to ask this, Can use this class for FieldSetMapper
是的,你可以使用这个 class。由于您编写了自定义 FieldSetMapper
,因此您可以控制映射逻辑。您的映射器类似于:
public class CustomerDataMapper implements FieldSetMapper<Customer> {
private static Log log = LogFactory.getLog(CustomerDataMapper.class);
@Override
public RawData mapFieldSet(FieldSet fieldSet) throws BindException {
Customer customer = new Customer();
try {
customer.setCust_Num(fieldSet.readInt("id"));
customer.setName_Surname(fieldSet.readString("name"));
}catch (Exception e){
log.error("Mapping error, " + e);
}
return customer;
}
}
但是您的问题不在阅读方面(和 FieldSetMapper
),而是在作者方面。 JdbcBatchItemWriter
使用 ItemSqlParameterSourceProvider
从 Customer
对象中提取数据并将它们设置在 sql 查询中。默认情况下,使用 BeanPropertyItemSqlParameterSourceProvider
,此实现根据 JavaBeans 命名约定提取数据。但是,您可以提供自定义实现,在您的情况下类似于:
class CustomerItemSqlParameterSourceProvider implements ItemSqlParameterSourceProvider<Customer> {
@Override
public SqlParameterSource createSqlParameterSource(Customer customer) {
Map<String, Object> params = new HashMap<>();
params.put("custNum", customer.getCust_Num());
params.put("nameSurname", customer.getName_Surname());
return new MapSqlParameterSource(params);
}
}
然后你把这个 CustomerItemSqlParameterSourceProvider
设置在你的 writer 上。
希望这对您有所帮助。
我想从文本文件中读取、解析和写入数据。我做到了这一点,但我对标识符的声明有疑问。我在下面显示我的class。
CustomerDataMapper.java
public class CustomerDataMapper implements FieldSetMapper<Customer> {
private static Log log = LogFactory.getLog(CustomerDataMapper.class);
@Override
public RawData mapFieldSet(FieldSet fieldSet) throws BindException {
Customer customer = new Customer();
try {
customer.setCustNum(fieldSet.readInt("id"));
customer.setNameSurname(fieldSet.readString("name"));
}catch (Exception e){
log.error("Mapping error, " + e);
}
return customer;
}
}
Customer.java
@Component
@Scope("prototype")
public class Customer implements Serializable{
private Integer custNum;
private String nameSurname;
public String getCustNum) {
return custNum;
}
public void setCustNum(String custNum) {
this.custNum = custNum;
}
public String getNameSurname() {
return nameSurname;
}
public void setNameSurname(String nameSurname) {
this.nameSurname = nameSurname;
}
}
fileManager.xml
<bean id="customerFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name= "encoding" value ="Cp1254"/>
<property name="resource" value="#{jobParameters['processPath']}"/>
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
<property name="names" value="id, name"/>
<property name="columns" ref="1-9, 10-30"/>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="mapper.CustomerDataMapper ">
<property name="fileName" value="#{jobParameters['fileNameAndTime']}"/>
</bean>
</property>
</bean>
</property>
<bean id="customerFileItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource"/>
<property name="sql">
<value>
<![CDATA[
insert into CUSTOMER(CUST_NUM, NAME_SURNAME)
values (:custNum, :nameSurname)
]]>
</value>
</property>
<property name="itemSqlParameterSourceProvider">
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider"/>
</property>
<bean id = "customerSkipListener" class="listener.CustomerSkipListener"/>
<batch:job id="customerImportJob">
<batch:step id="smsPasswordDataImportStep">
<batch:tasklet>
<batch:chunk reader="customerFileItemReader" writer="customerFileItemWriter" commit-interval="1" skip-limit="1000">
<batch:skippable-exception-classes>
<batch:include class="java.lang.Exception"/>
<batch:exclude class="java.io.FileNotFoundException"/>
</batch:skippable-exception-classes> <batch:listeners>
<batch:listener ref="customerSkipListener" />
</batch:listeners>
</batch:chunk>
</batch:tasklet>
</batch:step>
我想问一下,下面这个class可以用在FieldSetMapper中吗?如果我在定义标识符时不使用 Camelcase 表示法,则无法写入数据库。有没有关于这个的方法或方法。非常感谢。
Customer.java
@Component
@Scope("prototype")
public class Customer implements Serializable{
private Integer Cust_Num;
private String Name_Surname;
public String getCust_Num() {
return Cust_Num;
}
public void setCust_Num(String Cust_Num) {
this.Cust_Num = Cust_Num;
}
public String getName_Surname() {
return Name_Surname;
}
public void setName_Surname(String Name_Surname) {
this.Name_Surname = Name_Surname;
}
}
I want to ask this, Can use this class for FieldSetMapper
是的,你可以使用这个 class。由于您编写了自定义 FieldSetMapper
,因此您可以控制映射逻辑。您的映射器类似于:
public class CustomerDataMapper implements FieldSetMapper<Customer> {
private static Log log = LogFactory.getLog(CustomerDataMapper.class);
@Override
public RawData mapFieldSet(FieldSet fieldSet) throws BindException {
Customer customer = new Customer();
try {
customer.setCust_Num(fieldSet.readInt("id"));
customer.setName_Surname(fieldSet.readString("name"));
}catch (Exception e){
log.error("Mapping error, " + e);
}
return customer;
}
}
但是您的问题不在阅读方面(和 FieldSetMapper
),而是在作者方面。 JdbcBatchItemWriter
使用 ItemSqlParameterSourceProvider
从 Customer
对象中提取数据并将它们设置在 sql 查询中。默认情况下,使用 BeanPropertyItemSqlParameterSourceProvider
,此实现根据 JavaBeans 命名约定提取数据。但是,您可以提供自定义实现,在您的情况下类似于:
class CustomerItemSqlParameterSourceProvider implements ItemSqlParameterSourceProvider<Customer> {
@Override
public SqlParameterSource createSqlParameterSource(Customer customer) {
Map<String, Object> params = new HashMap<>();
params.put("custNum", customer.getCust_Num());
params.put("nameSurname", customer.getName_Surname());
return new MapSqlParameterSource(params);
}
}
然后你把这个 CustomerItemSqlParameterSourceProvider
设置在你的 writer 上。
希望这对您有所帮助。