Java 如何在春季批次中创建自定义项目阅读器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18379497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create custom item reader in spring batch
提问by hudi
In my app I am using sqlProcessor as database framework: https://github.com/hudec/sql-processor/wiki.
在我的应用程序中,我使用 sqlProcessor 作为数据库框架: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
我应该如何在 itemReader 中迭代这个列表,或者我应该如何创建我的自定义项目阅读器,它使用 sqlProcessor 从数据库中读取数据
采纳答案by Luca Basso Ricci
Use an ItemReaderAdapter.
From Javadoc:Invokes a custom method on a delegate plain old Java object which itself provides an item.
使用ItemReaderAdapter。
来自 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.
如果您有特殊条件、参数和其他需求,您可以创建自己的 ItemReader,但您可以扩展ItemReaderAdapter
以重用您的 DAO 并节省时间。
回答by Andrew White
If you already have a list then you can just create an instance of IteratorItemReader.
如果您已经有一个列表,那么您只需创建一个IteratorItemReader实例。
List<MyClass> myClassList = myClassDao.list(...)
ItemReader reader = new IteratorItemReader(myClassList);
回答by CrashCodes
Have a look at the ListItemReader
回答by Eric Nascimento
You just need to implement the ItemReader interface. For example:
您只需要实现 ItemReader 接口。例如:
public MyReader implements ItemReader<YourClass> {
@Override
public YourClass read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
// Implement your read logic, return null when no more items
}
}
this simple class will return your data and will just stop to read when all query data were retrieved,
这个简单的类将返回您的数据,并在检索到所有查询数据时停止读取,
I hope that it help you or other users :)
我希望它可以帮助您或其他用户:)