Orika 如何在不实例化新目标对象的情况下映射对象
How can Orika map objects without instantiating a new destination object
我需要用 Orika 替换 Dozer 映射,我想知道是否可以在 Orika 中使用 Dozer 的东西 mapper.map(obj1, obj2)
?到目前为止,我已经看到对于目的地你只能指定 class 类型而不是对象,所以我假设每次都会创建一个新实例。但在这种情况下,我只需要用 DTO 的某些字段更新以前找到的实体对象。这可能吗?
是的,可以将源对象 A
映射到目标 class B
- 然后 Orika 将自己实例化 B
- 或者已经创建了 B
.
的实例
// Let Orika create instance of B
A source = new A();
BoundMapperFacade<A, B> mapper = mapperFactory.getMapperFacade(A.class, B.class);
B target = mapper.map(source);
// Create instance of B yourself and let Orika fill it
A source = new A();
B target = new B();
BoundMapperFacade<A, B> mapper = mapperFactory.getMapperFacade(A.class, B.class);
mapper.map(source, target);
在第二种情况下,您可以通过某种方式自己设置 target
实例 - Orika 只会将属性从 A
映射到您在 [=] 中定义的 B
18=].
我需要用 Orika 替换 Dozer 映射,我想知道是否可以在 Orika 中使用 Dozer 的东西 mapper.map(obj1, obj2)
?到目前为止,我已经看到对于目的地你只能指定 class 类型而不是对象,所以我假设每次都会创建一个新实例。但在这种情况下,我只需要用 DTO 的某些字段更新以前找到的实体对象。这可能吗?
是的,可以将源对象 A
映射到目标 class B
- 然后 Orika 将自己实例化 B
- 或者已经创建了 B
.
// Let Orika create instance of B
A source = new A();
BoundMapperFacade<A, B> mapper = mapperFactory.getMapperFacade(A.class, B.class);
B target = mapper.map(source);
// Create instance of B yourself and let Orika fill it
A source = new A();
B target = new B();
BoundMapperFacade<A, B> mapper = mapperFactory.getMapperFacade(A.class, B.class);
mapper.map(source, target);
在第二种情况下,您可以通过某种方式自己设置 target
实例 - Orika 只会将属性从 A
映射到您在 [=] 中定义的 B
18=].