简体   繁体   English

如何使用推土机将简单的ArrayList映射到VO

[英]How to map a simple ArrayList to a VO using Dozer

I'm using Dozer to convert my objects. 我正在使用推土机转换我的对象。 But I've a problem to map a simple List... I retrieve a ResultSet from Hibernate as an Object List and I want to map it to my complex type object. 但是我在映射一个简单的列表时遇到了问题...我从Hibernate检索一个ResultSet作为Object List ,我想将其映射到我的复杂类型对象。

So, my source is like : 所以,我的来源就像:

List < Object > list = new ArrayList< Object > ();
list.add("Name");
list.add("Address");

And my Value Object is : 我的价值对象是:

public class MyClass 
{ 
    public String name; 
    public String address; 
}

I just want to map list[0] ==> MyClass.name and list[1] ==> MyClass.address properties but I don't find how... 我只想映射list[0] ==> MyClass.namelist[1] ==> MyClass.address属性,但是我不知道如何...

Thanks for your help ! 谢谢你的帮助 !

For some reason Dozer does Not support this (the ideal situation): 由于某种原因,推土机支持此操作(理想情况):

<mapping>
    <class-a>MyClass</class-a>
    <class-b>java.util.List</class-b>       
    <field>          
        <a is-accessible="true">name</a>
        <b>this[0]</b>
    </field>        
</mapping>

It would only map to name the whole string representation of the List , so your name property would end up with the value [Name, Address] . 它只会映射以name List的整个字符串表示形式,因此您的name属性将以[Name,Address]值结尾。

Your best option would be to put your List in a holder class and map it like this: 最好的选择是将List放在一个holder类中,并像这样映射它:

<mapping>
    <class-a>MyClass</class-a>
    <class-b>MyHolder</class-b>     
    <field>          
        <a is-accessible="true">name</a>
        <b>holded[0]</b>
    </field>
    <field>          
        <a is-accessible="true">address</a>
        <b>holded[1]</b>
    </field>    
</mapping>

MyHolder class just contains your List instance in the holded field and provide access to it with a getter method. MyHolder类仅在holded字段中包含List实例,并使用getter方法提供对其的访问。

In the field mappings is-accessible="true" is required because MyClass properties are public and have no accessors. 在该字段中,必须使用is-accessible="true"映射,因为MyClass属性是public并且没有访问器。 I recommend you to make those properties private and create accessor methods. 我建议您将这些属性设为private并创建访问器方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM