11

I have downloaded and testing these two mapping libraries. I wrote a program which has 100000 iterations and maps the beans of the same class:

public class IntBean {

    @JMap
    private int int1;
    @JMap
    private int int2;
    .
    .
    .
    @JMap 
    private int int10;
}

Mappers are created BEFORE iterations start:

private JMapper jmapper = new JMapper(IntBean.class, IntBean.class);
private MapperFactory orikaFactory = new DefaultMapperFactory.Builder().build();
private MapperFacade orikaFacade = null;
orikaFactory.registerClassMap(orikaFactory.classMap(IntBean.class,IntBean.class).byDefault().toClassMap());
orikaFacade = orikaFactory.getMapperFacade();

What is in each iteration:

this.orikaFacade.map(a1, a2);

or

a2 = (A) this.jmapper2.getDestination(a1);

Hand mapping: 1ms

Orika mapping: 32ms

Hand mapping: 6ms GREAT SPEED !!!

Dozer: 1140ms

I know, that Orika and Jmapper are great libraries from Google and they use reflection in a different way than for example Dozer, which is much slower, they se reflection to generete code somehow..

I have 3 questions:

1) How they work - when the code is generated, during maven build, in runtime - everytime when I create mapper in code? Are they change class code byte dynamically when I create mappers.?

2) Why there is this speed difference that I noticed? If the generate code somehow, then why there are different results

3) Which library would you choose and why? Both have the same capabilities? Why both come from Google? Why Google didnt develop Orika and created Jmapper instead?

4

3 回答 3

7

我对 Jmapper 不熟悉,所以我将专注于 Orika 和 Dozer

  1. 它们是如何工作的?它们的工作方式都不同。Dozer 使用反射和 Orika 使用字节码生成。在Maven构建期间?什么都没有发生,它都是在运行时完成的。Dozer 通过其 get 方法访问字段,并通过 setter 方法在目标对象中设置值。Orkia 生成字节码来完成这项工作,就好像您自己完成了手动映射一样。第一次转换速度很慢,之后的每个转换速度都应该更快。

  2. 推土机,应始终保持大致相同的速度,依靠反射。Orika,字节码生成,第一次运行在生成映射代码时应该慢很多。

  3. 简短的回答,这取决于。你想映射什么?如果类大致相似,Dozer 非常擅长从一种类型映射到另一种类型。它根本不处理地图。如果您的 Object 中有 Map,请准备编写自定义转换器代码

Orika 非常擅长在相同类型的两个对象之间映射数据。它的一些列表处理有点奇怪,它将列表视为单个对象而不是单个对象的集合。同样,准备为此编写一些代码。

两者都不能很好地处理大型对象图,准备为此编写大量配置。

除非您要在应用程序中进行大量映射,或者需要经常更改的映射。自己写

于 2014-05-06T13:10:36.977 回答
3

JMapper 基于 Javassist 框架,该框架的强大之处在于能够在不损失性能的情况下应用丰富、动态映射、多关系映射、继承映射和其他特性。
所有代码都是在构造函数阶段编写的。jmapper 的目标是:易于使用并具有手动编码的性能。

于 2014-07-08T16:06:57.340 回答
3

映射器

JMapper 框架是一个 java bean 到 java bean 的映射器,允许你通过注解和/或 XML 动态地执行数据的传递。jmapper 不仅限于在运行时生成代码,它还应用了许多开发人员通常不会做的优化。使用 JMapper,我们拥有动态映射的所有优点和静态代码的性能,内存消耗为 0 https://code.google.com/p/jmapper-framework/

优点: 1. 基于 XML 和注释的映射可用。2. 1 到 N 和 N 到 1 关系 3. 显式转换 4. 继承配置。5.性能比推土机好。

缺点:1. 聚合是通过显式转换实现的 2. 基本文档。3. JMapper 仍在开发中。

MapStruct:MapStruct 是 bean 映射的编译时代码生成器,可在运行时生成快速(不使用反射或类似的)、无依赖和类型安全的映射代码。 http://mapstruct.org/

优点:1.通过接口映射。2.编译时代码生成,速度快(不使用反射或类似的)。3. 源对象的嵌套映射。4. 隐式类型转换,例如在所有Java 基本类型(包括它们的包装器)和String 之间,例如在int 和String 或Boolean 和String 之间。5. 自定义映射器。6. 映射集合很容易。7.异常处理。8. 反向映射。9. 良好的文档和论坛。10.性能比Jmapper和dozer好。

缺点: 1. 目标属性不应嵌套。2. 对于反向映射,需要额外的映射或者需要创建新的映射器。3.我们可以使用手写代码代替MapStruct,它可以提供更好的性能。

于 2015-10-05T06:46:20.857 回答