gpt4 book ai didi

java - Spring 批rowMapper : no matching editors or conversion strategy found

转载 作者:行者123 更新时间:2023-12-02 02:41:13 27 4
gpt4 key购买 nike

我正在尝试运行我的第一个 Batch Spring 应用程序,但行映射器在运行时遇到以下问题:

Cannot convert value of type [com.tutoref.batch.ProductMapper] to required type [org.springframework.jdbc.core.RowMapper] for property 'rowMapper': no matching editors or conversion strategy found

该程序必须将一些数据从我的 sql 导出到平面文件 (csv)。下面我包含了主要文件(如果需要,我可以添加任何文件)。我还关心 pom.xml 中的版本。

我的IDE是eclipse。这是我的 pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.tutoref</groupId>
<artifactId>spring-batch-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-batch-example</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- Spring core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- Spring batch core -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>3.0.8.RELEASE</version>
</dependency>
<!-- MySQL connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- Spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- The JAXB Api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.6</version>
</dependency>
<!-- Junit for unit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

我的工作定义 xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
">

<import resource="spring-context.xml" />
<import resource="datasource.xml" />

<bean id="product" class="com.tutoref.batch.entity.Product" scope="prototype" />
<bean id="itemProcessor" class="com.tutoref.batch.ProductItemProcessor" />
<bean id="jobListener" class="com.tutoref.batch.ProductJobListener" />


<!-- Reading from the database and returning a mapper row -->
<bean id="productItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="productDataSource" />
<property name="sql" value="SELECT * FROM products" />
<property name="rowMapper">
<bean class="com.tutoref.batch.ProductMapper" />
</property>

</bean>

<!-- Writing a line into an output flat file -->
<bean id="productFlatFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="file:csv/products.csv" />
<!-- Converting a product object into delimited list of strings -->
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="|" />
<property name="fieldExtractor">
<!-- Returning the value of beans property using reflection -->
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="name,unitPrice,quantity" />
</bean>
</property>
</bean>
</property>
</bean>

<!-- And finally ... the job definition -->
<batch:job id="productJob">
<batch:step id="step1">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="productItemReader" writer="productFlatFileItemWriter"
processor="itemProcessor" commit-interval="10" />
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="jobListener" />
</batch:listeners>
</batch:job>
</beans>

行映射器代码:

public class ProductMapper implements FieldSetMapper<Product> {

@Override
public Product mapFieldSet(FieldSet fieldSet) throws BindException {
Product product = new Product();
product.setId(fieldSet.readInt(0));
product.setName(fieldSet.readString(1));
product.setQuantity(fieldSet.readInt(2));
product.setUnitPrice(fieldSet.readDouble(3));
return product;
}


}

和主类:

public class App 
{
public static void main(String[] args){

ApplicationContext context = new ClassPathXmlApplicationContext("job-products.xml");

JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("productJob");

try {
JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("Job Exit Status : "+ execution.getStatus());

} catch (JobExecutionException e) {
System.out.println("The Job has failed :" + e.getMessage());
e.printStackTrace();
}

}
}

异常的堆栈跟踪:

INFO: No TaskExecutor has been set, defaulting to synchronous executor. Loading class com.mysql.jdbc.Driver'. This is deprecated.
The new driver class is
com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. Jul 30, 2017 2:09:59 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName INFO: Loaded JDBC driver: com.mysql.jdbc.Driver Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productItemReader' defined in class path resource [job-products.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.tutoref.batch.ProductMapper' to required type 'org.springframework.jdbc.core.RowMapper' for property 'rowMapper'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.tutoref.batch.ProductMapper] to required type [org.springframework.jdbc.core.RowMapper] for property 'rowMapper': no matching editors or conversion strategy found at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at com.tutoref.batch.App.main(App.java:19) Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.tutoref.batch.ProductMapper' to required type 'org.springframework.jdbc.core.RowMapper' for property 'rowMapper'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.tutoref.batch.ProductMapper] to required type [org.springframework.jdbc.core.RowMapper] for property 'rowMapper': no matching editors or conversion strategy found at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:474) at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511) at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1502) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1461) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ... 11 more Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.tutoref.batch.ProductMapper] to required type [org.springframework.jdbc.core.RowMapper] for property 'rowMapper': no matching editors or conversion strategy found at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267) at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459) ... 17 more

谢谢

最佳答案

Cannot convert value of type [com.tutoref.batch.ProductMapper] to required type [org.springframework.jdbc.core.RowMapper] for property 'rowMapper': no matching editors or conversion strategy found

此异常意味着 productItemReader bean 正在等待 org.springframework.jdbc.core.RowMapper rowmapper 中的对象属性,但它正在接收 com.tutoref.batch.ProductMapper 类型的引用对象。

问题位于 public class ProductMapper implements FieldSetMapper<Product>

转到ProductMapper类并实现org.springframework.jdbc.core.RowMapper而不是FieldSetMapper

这是如何实现 RowMapper 接口(interface)的示例,MessageContainer 是一个 pojo 示例,如 Product您项目中的类。

public class MessageContainerMapper implements RowMapper<MessageContainer> {

@Override
public MessageContainer mapRow(ResultSet rs, int rowNum) throws SQLException {
MessageContainer mc = new MessageContainer();
mc.setId(rs.getInt("id"));
mc.setName(rs.getString("name"));
return mc;
}

}

关于java - Spring 批rowMapper : no matching editors or conversion strategy found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45403268/

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