你能添加一个从B
back to的关系A
吗?然后,您可以使用以下 bean 映射遍历每个子列表:
new String[] { "a.col1", "a.col2", "b1", "b2" }
这是一个如何使用它的示例。注意嵌套循环在 each 中迭代B
each A
。
public class DozerTest {
@Test
public final void test() throws IOException {
StringWriter out = new StringWriter();
ICsvDozerBeanWriter writer = new CsvDozerBeanWriter(out,
CsvPreference.STANDARD_PREFERENCE);
writer.configureBeanMapping(B.class,
new String[] { "a.col1", "a.col2", "b1", "b2" });
for (A a : generateData()) {
for (B b : a.getbList()) {
writer.write(b);
}
}
writer.flush();
System.out.println(out.toString());
}
private List<A> generateData() {
List<A> data = new ArrayList<A>();
for (int i = 0; i < 3; i++) {
A a = new A();
a.setCol1("col1 for a" + i);
a.setCol2("col2 for a" + i);
B firstB = new B();
firstB.setB1("first b1 for a" + i);
firstB.setB2("first b2 for a" + i);
firstB.setA(a);
B secondB = new B();
secondB.setB1("second b1 for a" + i);
secondB.setB2("second b2 for a" + i);
secondB.setA(a);
a.setbList(Arrays.asList(firstB, secondB));
data.add(a);
}
return data;
}
}
这打印:
col1 for a0,col2 for a0,first b1 for a0,first b2 for a0
col1 for a0,col2 for a0,second b1 for a0,second b2 for a0
col1 for a1,col2 for a1,first b1 for a1,first b2 for a1
col1 for a1,col2 for a1,second b1 for a1,second b2 for a1
col1 for a2,col2 for a2,first b1 for a2,first b2 for a2
col1 for a2,col2 for a2,second b1 for a2,second b2 for a2
更新以解决评论
如果你不能添加关系,那么你可以只使用 CellProcessors(你甚至不需要使用 Dozer)。我刚刚创建了 3 个简单的单元处理器:
ElementAt
- 只需在所需索引处抓取元素
GetB1
- 获取b1
a 的字段B
GetB2
- 获取b2
a 的字段B
他们在这里行动:
@Test
public final void test2() throws IOException {
StringWriter out = new StringWriter();
ICsvDozerBeanWriter writer = new CsvDozerBeanWriter(out,
CsvPreference.STANDARD_PREFERENCE);
writer.configureBeanMapping(A.class,
new String[] { "col1", "col2", "bList", "bList" });
for (A a : generateData()) {
for (int i = 0; i < a.getbList().size(); i++) {
CellProcessor[] processors = new CellProcessor[] { null, null,
new ElementAt(i, new GetB1()),
new ElementAt(i, new GetB2()) };
writer.write(a, processors);
}
}
writer.flush();
System.out.println(out.toString());
}
class GetB1 extends CellProcessorAdaptor {
public Object execute(Object value, CsvContext context) {
validateInputNotNull(value, context);
return ((B) value).getB1();
}
}
class GetB2 extends CellProcessorAdaptor {
public Object execute(Object value, CsvContext context) {
validateInputNotNull(value, context);
return ((B) value).getB2();
}
}
class ElementAt extends CellProcessorAdaptor {
private final int index;
public ElementAt(int index) {
this.index = index;
}
public ElementAt(int index, CellProcessor next) {
super(next);
this.index = index;
}
public Object execute(Object value, CsvContext context) {
validateInputNotNull(value, context);
Object element = ((List<?>) value).get(index);
return next.execute(element, context);
}
}
输出:
col1 for a0,col2 for a0,first b1 for a0,first b2 for a0
col1 for a0,col2 for a0,second b1 for a0,second b2 for a0
col1 for a1,col2 for a1,first b1 for a1,first b2 for a1
col1 for a1,col2 for a1,second b1 for a1,second b2 for a1
col1 for a2,col2 for a2,first b1 for a2,first b2 for a2
col1 for a2,col2 for a2,second b1 for a2,second b2 for a2
ps 我不建议您提供您自己的推土机映射 XML,除非您真的必须这样做(在这种情况下,它似乎没有必要)。