How to use Classifier with ClassifierCompositeItemWriter?

后端 未结 1 1181
旧巷少年郎
旧巷少年郎 2020-12-18 16:22

have trouble implementing a ClassifierCompositeItemwriter...

I am reading a basic CSV File and i want to write them do a database. Depending on the data (Name + Name

相关标签:
1条回答
  • 2020-12-18 17:09

    You classifier should be like this:

    public class MySpecialClassifier implements Classifier<MyObject, ItemWriter<? super MyObject>> {
    
        @Autowired
        ItemWriter<MyObject> writer1;
    
        @Autowired
        ItemWriter<MyObject> writer2;
    
        @Override
        public ItemWriter<MyObject> classify(MyObject obj) {
    
            if (!obj.getName().isEmpty() && !obj.getName1().isEmpty()) {
                return writer1;
            } else {
                return writer2;
            }
        }
    
    }
    

    The only difference is implements Classifier<MyObject, ItemWriter<? super MyObject>> instead of implements Classifier<MyObject, ItemWriter<MyObject>>.

    This is because an item writer can not only write MyObject items but also items of a superclass of MyObject.

    0 讨论(0)
提交回复
热议问题