我有两个类,并且希望使用org.dozer.Mapper(http://dozer.sourceforge.net/)将女性对象的属性映射到男性对象。
第一堂课是:
public class Male {
private String name;
private String surname;
private Map<String, List<Contact>> contacts;
....
第二类是:
public class Female {
private String name;
private String surname;
private String mobile;
private String dateOfBirth;
private Map<String, List<Contact>> contacts;
...
第三类是:
public class Contact {
private String street;
private String postcode;
private String email;
...
我正在使用的像对象属性的Map是LinkedHashMap,而Map中作为值的List是ArrayList。 当我尝试使用推土机映射它们时,哈希列表中的值即数组列表不是带有对象的列表,并且看起来像图片中的那样:
Map<String, List<Contact>> contact = new LinkedHashMap<>();
List<Contact> listOfContacts = new ArrayList<>();
Contact contactObj = new Contact();
contactObj.setEmail("lala@gmail.com");
contactObj.setPostcode("1233355");
contactObj.setStreet("street");
listOfContacts.add(contactObj);
contact.put("2131323213", listOfContacts);
femaleObj.setContact(contact);
Mapper objectMapper = new DozerBeanMapper();
Male maleObj = objectMapper.map(femaleObj, Male.class);
如何在“男性”对象的“列表”中获取对象列表?
答案 0 :(得分:2)
起初,我按原样尝试了您的代码,但行为却相同。
然后,我通过b-hint显式设置了映射配置(请参见documentation about this),然后我得到了所需的内容。
第一种情况- Java配置(创建一个扩展BeanMappingBuilder的类):
public class CustomMapper extends BeanMappingBuilder {
@Override
protected void configure() {
mapping(Female.class, Male.class).fields("contacts", "contacts", FieldsMappingOptions.hintB(Contact.class));
}
}
第二种情况- XML配置:
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<configuration>
<stop-on-errors>true</stop-on-errors>
<wildcard>true</wildcard>
</configuration>
<mapping>
<class-a>blog.valerioemanuele.dozer.Female</class-a>
<class-b>blog.valerioemanuele.dozer.Male</class-b>
<field>
<a>contacts</a>
<b>contacts</b>
<b-hint>blog.valerioemanuele.dozer.Contact</b-hint>
</field>
</mapping>
</mappings>
这是我执行的单元测试:
import org.dozer.DozerBeanMapper;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class TestFemaleToMaleConversion {
private static Female femaleObj;
@BeforeAll
private static void initData() {
Map<String, List<Contact>> contact = new LinkedHashMap<>();
List<Contact> listOfContacts = new ArrayList<>();
Contact contactObj = new Contact();
contactObj.setEmail("lala@gmail.com");
contactObj.setPostcode("1233355");
contactObj.setStreet("street");
listOfContacts.add(contactObj);
contact.put("2131323213", listOfContacts);
femaleObj = new Female();
femaleObj.setName("Elisabeth");
femaleObj.setSurname("Chesny");
femaleObj.setContacts(contact);
}
@Test
void testWithXmlMapping() {
DozerBeanMapper objectMapper = new DozerBeanMapper();
objectMapper.setMappingFiles(Arrays.asList("dozer-mapping.xml"));
Male maleObj = objectMapper.map(femaleObj, Male.class);
Assert.assertEquals("lala@gmail.com", maleObj.getContacts().get("2131323213").get(0).getEmail());
}
@Test
void testWithJavaMapping() {
DozerBeanMapper objectMapper = new DozerBeanMapper();
objectMapper.addMapping(new CustomMapper());
Male maleObj = objectMapper.map(femaleObj, Male.class);
Assert.assertEquals("street", maleObj.getContacts().get("2131323213").get(0).getStreet());
}
}
结果在这里:
您可以从my GitHub repository获取完整的代码。该示例是使用Java8,Maven和Junit5开发的。
编辑:我添加了Java映射配置案例。从another post
中汲取灵感答案 1 :(得分:2)
如果您想用JAVA代码实现相同的目的,而使用XML配置,请使用以下代码:
public class DemoProvider extends BeanMappingBuilder {
@Override
protected void configure() {
mapping(Female.class, Male.class,
TypeMappingOptions.oneWay()
)
.fields("contact", "contact",
FieldsMappingOptions.collectionStrategy(true, RelationshipType.NON_CUMULATIVE),
FieldsMappingOptions.hintA(Contact.class),
FieldsMappingOptions.hintB(Contact.class),
FieldsMappingOptions.oneWay()
);
}
}
用法:
DemoProvider demoProvider = new DemoProvider();
DozerBeanMapper objectMapper = new DozerBeanMapper();
objectMapper.addMapping(demoProvider);
Male maleObj = objectMapper.map(femaleObj, Male.class);