如何根据商品属性更改或选择特定的作者? 例如,我需要写入不同的SQL表(表A和B)。
if(item.myBoolean == true){写入A和B}否则{仅写在A上}
如何选择使用JdbcBatchItemWriter
? (实际上我使用CompositeItemWriter
写入我的JdbcBatchItemWriter
)
我是否需要创建自定义ItemWriter“decider”或者还有另一种最佳解决方案(来自处理器)?
答案 0 :(得分:2)
我使用ClassifierCompositeItemWriter
找到了解决方案ClassifierCompositeItemWriter<Subscription> classifierCompositeItemWriter = new ClassifierCompositeItemWriter<Subscription>();
classifierCompositeItemWriter.setClassifier(new Classifier<Subscription, ItemWriter<? super Subscription>>() {
@Override
public ItemWriter<? super Subscription> classify(Subscription classifiable) {
ItemWriter<? super Subscription> itemWriter = null;
if(classifiable.isComplex()) { // condition
itemWriter = compositeItemWriter; // I use compositeItemWriter to write on A and B
} else {
itemWriter = subscriptionJdbcWriter; // I use single JdbcBatchItemWriter to write only on A
}
log.info("Subscription would be classified with " + itemWriter.getClass().getSimpleName());
return itemWriter;
}
});
答案 1 :(得分:0)
@Bean
public ItemWriter<Person> itemWriter(DataSource dataSource) {
BackToBackPatternClassifier classifier = new BackToBackPatternClassifier();
classifier.setRouterDelegate(new AggGroupClassifier());
classifier.setMatcherMap(new HashMap<String, ItemWriter<? extends Person>>() {
{
put("A", writerA(dataSource));
put("B", writerB(dataSource));
put("C", writerC(dataSource));
}
});
ClassifierCompositeItemWriter<Person> writer = new ClassifierCompositeItemWriter<Person>();
writer.setClassifier(classifier);
return writer;
}
public class AggGroupClassifier {
@Classifier
public String classify(Person person) {
return person.getAgeGroup();
}
}