0
A
回答
1
你不需要做任何事情;推土機應該處理這個箱子。
考慮以下兩個bean:
Source.java
public class Source {
private String field1;
private String field2;
private String field3;
// Getters and setters omitted
}
Destination.java
public class Destination {
private String field1;
private String field2;
// Getters and setters omitted
}
您可以使用下面的映射在dozer.xml文件(這將映射兩個bean中名稱匹配的所有屬性):
<mapping>
<class-a>com.example.Source</class-a>
<class-b>com.example.Destination</class-b>
</mapping>
下面的單元測試表明,映射是雙向的:
@Test
public void sourceToDestination() {
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer.xml");
this.beanMapper = new DozerBeanMapper(mappingFiles);
Source source = new Source();
source.setField1("A");
source.setField2("B");
source.setField3("C");
Destination dest = beanMapper.map(source, Destination.class);
assertEquals("A", dest.getField1());
assertEquals("B", dest.getField2());
}
@Test
public void destinationToSource() {
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer.xml");
this.beanMapper = new DozerBeanMapper(mappingFiles);
Destination dest = new Destination();
dest.setField1("A");
dest.setField2("B");
Source source = beanMapper.map(dest, Source.class);
assertEquals("A", source.getField1());
assertEquals("B", source.getField2());
assertNull(source.getField3());
}
當我們映射源到目的地,因爲目標沒有一個名爲field3
則忽略屬性。當我們以另一種方式映射(Destination to Source)時,Source中的field3
爲空。
相關問題
- 1. c#反射參數計數不匹配
- 2. Dozer InstantiationException映射日曆類
- 3. 參數計數不匹配
- 4. Elasticsearch數據與映射不匹配
- 5. 反射字符串時引發的參數計數不匹配
- 6. dozer布爾屬性映射
- 7. XSLT 1.0:計數匹配塊的數量並創建映射
- 8. C#反射控制屬性參數計數不匹配異常
- 9. 參數類型不匹配
- 10. 斯卡拉類型不匹配映射
- 11. PHP:參數計數和參數計數不匹配
- 12. 參數計數不匹配參數值計數
- 13. System.Reflection.TargetParameterCountExeception:參數計數不匹配
- 14. SQLError#3115參數計數不匹配
- 15. 啓動時參數計數不匹配
- 16. QSqlError(「」,「參數計數不匹配」,「」)
- 17. TargetParameterCountException:參數計數不匹配
- 18. DynamicInvoke引發參數計數不匹配
- 19. Method.Invoke失敗,參數計數不匹配
- 20. 參數計數與調用不匹配?
- 21. PowerShell:BeginInvoke參數計數不匹配
- 22. WPF System.Reflection.TargetParameterCountException參數計數不匹配
- 23. 參數計數不匹配。線程
- 24. 調用時參數計數不匹配
- 25. 參數計數不匹配。 - 線程
- 26. 模式匹配映射作爲函數參數
- 27. Dozer多個包中的通用映射(通配符?)
- 28. 類型參數的類型不匹配
- 29. 科特林仿製藥:在通用映射參數類型不匹配
- 30. system.reflection.targetparametercountexception參數數量不匹配參數數量不匹配