如何在 spring 批次中将编组对象值(reader)映射到休眠实体对象(编写器)

How to map the marshalled object values(reader) to hibernate entity object(writer) in spring batch

我是 Spring Batch 的新手,即使经过广泛的搜索,我也没有得到任何关于如何将 XML 编组对象值映射到休眠实体对象的线索。具体来说,我认为它遵循与 FieldSetMapper 相同的方法,但不确定如何获取 MarshallObject 而不是 FlatFieldSet。请帮忙。

仅供参考:我应该只使用 Hibernate 而不是 JdbcTemplate 或 preparedstatementsetter 等。

批处理配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <import resource="//context.xml" />
    <!-- <context:component-scan base-package="com.politico.batchhandle.marshallbatch" /> -->
    <!-- parallel job processing -->
    <job id="feedprocesser" xmlns="http://www.springframework.org/schema/batch">
        <!-- <split id="split1"> <flow> -->
        <step id="stepfeedprocess">
            <!-- <tasklet task-executor="taskExecutor" throttle-limit="10"> -->
            <tasklet>
                <chunk reader="xmlmultiReader" writer="dataWriter" processor="dataProcessor"
                    commit-interval="10" />
            </tasklet>
        </step>
        <!-- </flow> </split> -->
    </job>

    <!-- <bean id="testReader" class="com.politico.batchhandle.marshallbatch.DataReader" 
        /> -->
    <bean id="dataProcessor" class="com.politico.batchhandle.marshallbatch.DataProcessor" />
    <bean id="dataWriter" class="com.politico.batchhandle.marshallbatch.DataWriter" />


    <!-- <bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" 
        /> -->

    <bean id="xmlmultiReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
        <property name="fragmentRootElementName" value="crs-bill-summary" />
        <property name="resource" value="classpath:xmlfiles\Bill_Digest.xml" />
        <property name="unmarshaller" ref="reportUnmarshaller" />
        <property name="fieldSetMapper" ref="dataFieldSetMapper" />
    </bean>


    <bean id="reportUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.politico.jaxbconvert.readerTO.CrsBillSummary</value>
            </list>
        </property>
         <property name="mappingLocation" value="classpath:/userMapping.xml" />  
    </bean>

    <bean id="dataFieldSetMapper" class="com.politico.batchhandle.dao.DataFieldSetMapper">
    </bean>

</beans>

我假设xml marshall对象也可以使用FieldSetMapper映射,这里是对应的代码:

public class DataFieldSetMapper implements FieldSetMapper<CrsBillSummary>{

    @Override
    public CrsBillSummary mapFieldSet(FieldSet fieldset) throws BindException {
        // TODO Auto-generated method stub


        return null;
    }
}

我确信我为 fieldsetmapping 使用了错误的接口,所以请帮助我找到正确的方法..

请将此添加到您的 XML

<bean id="reportUnmarshaller"
      class="org.springframework.oxm.xstream.XStreamMarshaller">
    <property name="aliases">
        <util:map id="aliases">
            <entry key="trade"
                   value="XXX.CrsBillSummary" />
            <entry key="property1" value="java.math.BigDecimal" />
            <entry key="property2" value="java.lang.String" />
        </util:map>
    </property>
</bean>

注意:在你的情况下,我希望你应该使用 Pojo class 而不是 Entity one。然后您可以在 Writer 层转换为实体,然后将其持久化到数据库。我建议最好使用它来避免无人值守对象持久保存到数据库的错误。