1

我创建了一个基于教程的简单弹簧批处理,它使用 itemreader 和 item writer 读取文件并加载到数据库中。我遵循了相同的步骤,但只有项目阅读器被调用,项目作者没有被调用。谁能帮帮我吗。谢谢 !!!

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/batch 
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

<job id="importProducts" xmlns="http://www.springframework.org/schema/batch" >
    <step id="decompress" next="readWriteProducts">
        <tasklet ref="decompressTasklet" />
    </step>
    <step id="readWriteProducts">
        <tasklet>
            <chunk reader="reader" writer="writer" commit-interval="3" skip-limit="5">
                <skippable-exception-classes>
                    <include class="org.springframework.batch.item.file.FlatFileParseException" />
                </skippable-exception-classes>
            </chunk>
        </tasklet>          
    </step>
</job>

<bean id="decompressTasklet" class="com.saimuga.abp.fileupload.tasklet.ReadInputFile" scope="step">
    <property name="inputResource" value="#{jobParameters['inputResource']}" />
    <property name="targetDirectory" value="#{jobParameters['targetDirectory']}" />
    <property name="targetFile" value="#{jobParameters['targetFile']}" />
</bean>

<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
    <property name="resource" value="file:#{jobParameters['targetDirectory']+jobParameters['targetFile']}" />
    <property name="linesToSkip" value="1" />
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="PRODUCT_ID,NAME,DESCRIPTION,PRICE" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="com.saimuga.abp.fileupload.domain.mapper.FiletoDomainMapper" />
            </property>
        </bean>
    </property>
</bean>

<bean id="writer" class="com.saimuga.abp.fileupload.writer.ProductJdbcItemWriter" >
    <constructor-arg ref="dataSource" />
</bean>

基础设施 XML 文件

    <?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- 
    beans.xsd">

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
    </bean>

    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>

    <jdbc:embedded-database id="dataSource" type="H2">
        <jdbc:script location="create-tables.sql"/>
    </jdbc:embedded-database>

    <bean class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>

</beans>

物品作者

package com.saimuga.abp.fileupload.writer;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.JdbcTemplate;

import com.saimuga.abp.fileupload.domain.Product;



/**
 * 
 *
 */
public class ProductJdbcItemWriter implements ItemWriter<Product> {

    private static final String INSERT_PRODUCT = "insert into product (id,name,description,price) values(?,?,?,?)";

    private static final String UPDATE_PRODUCT = "update product set name=?, description=?, price=? where id = ?";

    private JdbcTemplate jdbcTemplate;

    public ProductJdbcItemWriter(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    /* (non-Javadoc)
     * @see org.springframework.batch.item.ItemWriter#write(java.util.List)
     */
    public void write(List<? extends Product> items) throws Exception {
        System.out.println("cxf ProductJdbcItemWriter starts ");
        for(Product item : items) {
            int updated = jdbcTemplate.update(UPDATE_PRODUCT,
                item.getName(),item.getDescription(),item.getPrice(),item.getId()
            );
            if(updated == 0) {
                jdbcTemplate.update(
                    INSERT_PRODUCT,
                    item.getId(),item.getName(),item.getDescription(),item.getPrice()
                );  
            }                               
            System.out.println("cxf ProductJdbcItemWriter ends ");
        }
    }

}
4

1 回答 1

2

我已经弄清楚了问题所在。我在 FieldSetMapper(ORM 的域映射器)类中返回 null,因此没有任何内容可写入 DB。这就是项目编写器没有运行的原因。是我的错!!!

于 2020-05-09T20:49:17.403 回答