当前位置: 首页 > 知识库问答 >
问题:

Spring Batch rowMapper:找不到匹配的编辑器或转换策略

邵伟
2023-03-14

我正在尝试运行我的第一批Spring应用程序,但在运行时,行映射器出现以下问题:

无法将类型[com.tutoref.batch.productMapper]的值转换为属性'rowMapper'所需的类型[org.springframework.jdbc.core.RowMapper]:找不到匹配的编辑器或转换策略

程序必须将一些数据从我的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();
        }

    }
}

异常的堆栈跟踪:

信息:未设置TaskExecutor,默认为synchronous executor。正在加载类com.mysql。jdbc。司机'。这是不赞成的。新的驱动程序类别是com.mysql。希杰。jdbc。司机'。驱动程序通过SPI自动注册,通常不需要手动加载驱动程序类。2017年7月30日下午2:09:59组织。springframework。jdbc。数据源。DriverManager数据源setDriverClassName信息:加载的JDBC驱动程序:com.mysql。jdbc。线程“main”组织中的驱动程序异常。springframework。豆。工厂BeanCreationException:创建在类路径资源[job products.xml]中定义了名为“productItemReader”的bean时出错:bean初始化失败;嵌套的异常是org。springframework。豆。ConversionNotSupportedException:无法转换“com”类型的属性值。图托雷夫。一批ProductMapper“到所需类型”组织。springframework。jdbc。果心属性“RowMapper”的“RowMapper”;嵌套的异常是java。lang.IllegalStateException:无法将[com.tutoref.batch.ProductMapper]类型的值转换为属性“RowMapper”所需的类型[org.springframework.jdbc.core.RowMapper]:在org上找不到匹配的编辑器或转换策略。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。org上的doCreateBean(AbstractAutowireCapableBeanFactory.java:547)。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。在org上创建Bean(AbstractAutowireCapableBeanFactory.java:475)。springframework。豆。工厂支持AbstractBeanFactory 1美元。org上的getObject(AbstractBeanFactory.java:304)。springframework。豆。工厂支持DefaultSingletonBeanRegistry。getSingleton(DefaultSingletonBeanRegistry.java:228)位于org。springframework。豆。工厂支持抽象工厂。org上的doGetBean(AbstractBeanFactory.java:300)。springframework。豆。工厂支持抽象工厂。org上的getBean(AbstractBeanFactory.java:195)。springframework。豆。工厂支持DefaultListableBeanFactory。在org上预实例化Singleton(DefaultListableBeanFactory.java:703)。springframework。上下文支持AbstractApplicationContext。在org上完成BeanFactoryInitialization(AbstractApplicationContext.java:760)。springframework。上下文支持AbstractApplicationContext。在org上刷新(AbstractApplicationContext.java:482)。springframework。上下文支持ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)位于org。springframework。上下文支持ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)在com上。图托雷夫。一批应用程序。main(App.java:19)由:org引起。springframework。豆。ConversionNotSupportedException:无法转换“com”类型的属性值。图托雷夫。一批ProductMapper“到所需类型”组织。springframework。jdbc。果心属性“RowMapper”的“RowMapper”;嵌套的异常是java。lang.IllegalStateException:无法将[com.tutoref.batch.ProductMapper]类型的值转换为属性“RowMapper”所需的类型[org.springframework.jdbc.core.RowMapper]:在org上找不到匹配的编辑器或转换策略。springframework。豆。豆瓣菜。ConvertifEssential(beanwraperImpl.java:474)位于org。springframework。豆。豆瓣菜。org上的convertForProperty(beanwraperImpl.java:511)。springframework。豆。豆瓣菜。org上的convertForProperty(beanwraperImpl.java:505)。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。org上的convertForProperty(AbstractAutowireCapableBeanFactory.java:1502)。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。org上的ApplyPropertyValue(AbstractAutowireCapableBeanFactory.java:1461)。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。populateBean(AbstractAutowireCapableBeanFactory.java:1197)位于org。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。doCreateBean(AbstractAutowireCapableBeanFactory.java:537)。。。还有11个原因是:java。lang.IllegalStateException:无法将[com.tutoref.batch.ProductMapper]类型的值转换为属性“RowMapper”所需的类型[org.springframework.jdbc.core.RowMapper]:在org上找不到匹配的编辑器或转换策略。springframework。豆。TypeConverterDelegate。ConvertifEssential(TypeConverterDelegate.java:267)位于org。springframework。豆。豆瓣菜。ConvertifEssential(beanwraperImpl.java:459)。。。还有17个

谢啦

共有1个答案

韩靖琪
2023-03-14

无法将类型[com.tutoref.batch.productMapper]的值转换为属性'rowMapper'所需的类型[org.springframework.jdbc.core.RowMapper]:找不到匹配的编辑器或转换策略

此异常意味着productItemReaderbean正在等待org。springframework。jdbc。果心RowMapper属性中的RowMapper对象,但它正在接收com类型的引用对象。图托雷夫。一批ProductMapper

问题出现在公共类ProductMapper实现FieldSetMapper中

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

这是一个如何实现RowMapper接口的示例,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;
    }

}

 类似资料: