How to custom Spring Batch DelimitedLineTokenizer?

来源:互联网 发布:淘宝宝贝描述模板制作 编辑:程序博客网 时间:2024/06/06 02:36

今天在用spring batch分析csv文件的时候,由于csv文件是以$进行分割的,所以需要在配置文件中添加一个属性:

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:batch="http://www.springframework.org/schema/batch"     xmlns:task="http://www.springframework.org/schema/task"    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">    <bean id="report" class="com.mkyong.model.Report" scope="prototype" />    <batch:job id="reportJob">        <batch:step id="step1">            <batch:tasklet>                <batch:chunk reader="cvsFileItemReader" writer="mysqlItemWriter"                    commit-interval="2">                </batch:chunk>            </batch:tasklet>        </batch:step>    </batch:job>    <bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">        <!-- Read a csv file -->        <property name="resource" value="classpath:cvs/report.csv" />        <property name="lineMapper">            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">                <!-- split it -->                <property name="lineTokenizer">                    <bean                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">                        **<property name="delimiter" value="$"/>**                        <property name="names" value="id,docnum,pubdate,appldate,classipcr,title,pabstract,applicant,address,inventor" />                    </bean>                </property>                <property name="fieldSetMapper">                    <!-- return back to reader, rather than a mapped object. -->                    <!--                        <bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />                    -->                    <!-- map to an object -->                    <bean                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">                        <property name="prototypeBeanName" value="report" />                    </bean>                </property>            </bean>        </property>    </bean>    <bean id="mysqlItemWriter"        class="org.springframework.batch.item.database.JdbcBatchItemWriter">        <property name="dataSource" ref="dataSource" />        <property name="sql">            <value>            <![CDATA[                        insert into patent_invention(id,docnum,pubdate,appldate,classipcr,title,pabstract,applicant,                address,inventor) values (:id, :docnum, :pubdate, :appldate,:classipcr,:title,:pabstract,:applicant,:address,:inventor)            ]]>            </value>        </property>        <!-- It will take care matching between object property and sql name parameter -->        <property name="itemSqlParameterSourceProvider">            <bean                class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />        </property>    </bean></beans>
0 0
原创粉丝点击