1
votes

I'm using a BeanWrapperFieldSetMapper to map the contents of a CSV file to a custom bean. Is there a spring batch utility class that will convert this bean into an object which implements the SQLParameterSource interface. I'm looking for a class which would use reflection to populate the MapSqlParameterSource class, using the custom beans field name and associated value and the key/value pair.

This is my current job configuration

<bean id="abcFileReaderDBLoaderJob" parent="simpleJob">
    <property name="steps">
        <list>
            <bean parent="simpleStep">
                <property name="itemReader">
                    <bean class="org.springframework.batch.item.file.FlatFileItemReader">
                        <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="strict" value="false"/>
                                        <property name="names" value="a,b,c" />
                                    </bean>
                                </property>
                                <property name="fieldSetMapper">
                                    <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                                        <property name="prototypeBeanName" value="abcBean"/>
                                    </bean>
                                </property>
                            </bean>
                        </property>
                        <property name="resource">
                            <bean class="org.springframework.core.io.FileSystemResource">
                                <constructor-arg value="${abc.file}" />
                            </bean>
                        </property>
                    </bean>
                </property>
                <property name="itemWriter" ref="abcTableWriter"/>
            </bean>
        </list>
    </property>
</bean>

And my generic table writer has this interface

/**
 * Takes a list of SqlParameterSource and writes to the DB.
 */
public class TableWriter implements ItemWriter<SqlParameterSource> {
    ....
}
1

1 Answers

1
votes

Did you mean something like BeanPropertySqlParameterSource?

SqlParameterSource implementation that obtains parameter values from bean properties of a given JavaBean object. The names of the bean properties have to match the parameter names.

Uses a Spring BeanWrapper for bean property access underneath.