简体   繁体   English

使用第三方库在Java中映射两个相同的类

[英]Mapping two identical classes in java with third party library

I need to convert one object to other .The object has nested of many inner objects.But both source and destination objects are identical by data type , variable name , class name etc. Is there any effective tool to do so with less coding. 我需要将一个对象转换为另一个对象。该对象嵌套了许多内部对象。但是源对象和目标对象在数据类型,变量名,类名等方面都是相同的。有没有什么有效的工具可以用更少的编码做到这一点。

I have tried orika but its expecting to define like below 我已经尝试过orika,但其期望如下定义

factory.registerClassMap(factory
                .classMap

with all the fields and inner object fields.I ended up with lots of iterator and setting filed mappings. 包含所有字段和内部对象字段。最后我得到了很多迭代器并设置了字段映射。

Help me or guide me Is there any other way to map objects with identical objects. 帮助我或指导我还有其他方法可以将具有相同对象的对象映射。

You could have a look at MapStruct . 您可以看看MapStruct

Another alternative is Dozer . 另一种选择是推土机 Classes can be mapped 1 on 1 by defining in the mapper xml file attribute wildcard="true" . 通过在映射器xml文件中定义wildcard="true"属性,可以一对一地映射类。 This means that it will automatically try to map every property in the two objects. 这意味着它将自动尝试映射两个对象中的每个属性。 When the attribute is set to false it will only map explicitly defined fields. 当该属性设置为false时,它将仅映射显式定义的字段。

<?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">

    <mapping wildcard="true"> 
        <class-a>org.example.ObjectA</class-a>
        <class-b>org.example.ObjectB</class-b>   
    </mapping> 
</mappings>

最好使用适当的克隆库,请检查此http://blog.jadira.co.uk/blog/2013/6/25/announcing-jadira-cloning.html

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

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