1

我需要帮助将 n 级深层结构展平为单级对象属性。约定是使用下划线字符 (level1_level2_level3)。我正在使用 orika ( http://orika-mapper.github.io/orika-docs/index.html )

例如。

Variable1.Property1.Property2.Property3

其中 Property1 是 Set,Property2 是类,Property3 是 String。

我在 orika 中需要的映射可以使用这个设置来实现:

.field("Property1{Property2.Property3}", "Property1_Property2_Property3")

问题是我需要可以是 n 级深度的通用解决方案。如果类型是列表/集合,我将只取第一个元素(如果列表/集合不为空)。

这是我想要实现的目标:

Class1 {

String property1;
Set<Class2> property2;

}

Class2 {

String property3;
Class3 property4;

}

Class3 {

String property5;    

}



MapHere {

String property1;
String property2_property3;
String property2_property4_property5;

}

更新:

这是 FlattenClassMapBuilder 的实现,以防有人需要它:http ://pastie.org/9324242

而且,按包映射:

    String aPrefix = "com.something.domain";
    String bPrefix = "com.something.dto";

    //----------------------
    Reflections reflections = new Reflections(aPrefix, new SubTypesScanner(false));
    //uzmi sve klase iz paketa koji je dat u aPrefix putanji (recimo "com.nesto.nesto1")
    Set<Class<?>> aClasses = reflections.getSubTypesOf(Object.class);
    reflections = new Reflections(bPrefix, new SubTypesScanner(false));
    //uzmi sve klase iz paketa koji je dat u bPrefix putanji (recimo "com.nesto.nesto2")
    Set<Class<?>> bClasses = reflections.getSubTypesOf(Object.class);
    for(Class<?> aClass : aClasses)
    {
        for(Class<?> bClass : bClasses)
        {
            if(aClass.getSimpleName().equals(bClass.getSimpleName()))
            {
                //za sve klase iz aPrefix-a cije se ime poklapa sa klasom iz bPrefix-a
                mapperFactory.classMap(aClass, bClass).byDefault().register();
            }
        }
    }
    //------------------------
4

1 回答 1

0

事实上 Orika 可以处理这个问题,但是您应该通过提供DefaultFieldMapper的实现来提供此自动映射策略

在 Orika 源代码/测试中,有一些示例。

于 2014-06-22T21:02:08.580 回答