java – 非平凡的推土机映射
作者:互联网
我正努力让Dozer屈服于我的意志,因为我觉得应该很简单.我有两个相似的模型,我想在它们之间进行映射,但是一个具有比另一个更“深”的层次结构,这在处理集合时会引起我的问题.考虑以下类:
来源类:
class Foo {
String id;
NameGroup nameGroup;
// Setters/Getters
}
class NameGroup {
private List<Name> names;
// Setters/Getters
}
class Name {
private String nameValue;
// Setters/Getters
}
目的地类:
class Bar {
private String barId;
private BarNames barNames;
// Setters/Getters
}
class BarNames {
private List<String> names;
// Setters/Getters
}
现在我想要以下单向映射:
Foo.id -> Bar.barId // Simple enough
但我需要:
Foo.nameGroup.names.nameValue -> Bar.barNames.names
因此,Foo.nameGroup.names中的每个Name实例都应该导致将一个String添加到BarNames.names列表中.这可能吗?
解决方法:
只要您的“Name”类包含String构造函数,就可以使用Dozer轻松完成此操作.
来自Dozer docs(http://dozer.sourceforge.net/documentation/simpleproperty.html)的引用:
Data type coversion is performed
automatically by the Dozer mapping
engine. Currently, Dozer supports the
following types of conversions: (these
are all bi-directional)…
String to
Complex Type if the Complex Type
contains a String constructor…
我已经用上面的类测试了这个(我遇到了同样的问题)并且它完美地工作.这是我使用的映射:
<mapping>
<class-a>com.test.bar.Bar</class-a>
<class-b>com.test.foo.Foo</class-b>
<field>
<a>barId</a>
<b>id</b>
</field>
<field>
<a>barNames.names</a>
<b>nameGroup.names</b>
<a-deep-index-hint>java.lang.String</a-deep-index-hint>
<b-deep-index-hint>com.test.foo.Name</b-deep-index-hint>
</field>
</mapping>
标签:java,javabeans,dozer 来源: https://codeday.me/bug/20190522/1151374.html