gpt4 book ai didi

java - Orika 嵌套属性扁平化

转载 作者:太空宇宙 更新时间:2023-11-04 14:46:13 24 4
gpt4 key购买 nike

我需要帮助将 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();
}
}
}
//------------------------

最佳答案

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

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

关于java - Orika 嵌套属性扁平化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24355203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com