gpt4 book ai didi

java - Spring Batch Item Writer 没有被调用

转载 作者:行者123 更新时间:2023-12-01 16:50:27 42 4
gpt4 key购买 nike

我根据教程创建了一个简单的 spring 批处理,它使用 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>

InfraStructure 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 ");
}
}

}

最佳答案

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

关于java - Spring Batch Item Writer 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61700994/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com