Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom item reader in spring batch

In my app I am using sqlProcessor as database framework: https://github.com/hudec/sql-processor/wiki.

so when I want to read list of object I call:

List<MyClass> myClassList = myClassDao.list(...)

How should I iterate over this list in itemReader or how should I create my custom item reader which read data from database using sqlProcessor

like image 886
hudi Avatar asked Dec 26 '22 22:12

hudi


1 Answers

Use an ItemReaderAdapter.
From Javadoc:
Invokes a custom method on a delegate plain old Java object which itself provides an item.

<bean id="itemReader" class="org.springframework.batch.item.adapter.ItemReaderAdapter">
    <property name="targetObject" ref="myClassDao" />
    <property name="targetMethod" value="list" />
    <property name="arguments">
      <list>
        <!-- add arguments list -->
      </list>
    </property>
</bean>

<bean id="myClassDao" class="path.to.MyClassDAO" /

If you have special condition, arguments and other needs you can create your own ItemReader, but you can extends ItemReaderAdapter to reuse your DAO and save time.

like image 55
Luca Basso Ricci Avatar answered Jan 09 '23 07:01

Luca Basso Ricci