简体   繁体   English

MapStruct:BeanMapping 和 Mapping 的区别

[英]MapStruct: Difference between BeanMapping and Mapping

I have been going through the documentation of MapStruct and can't seem to figure out when the annotation @BeanMapping should be used.我一直在浏览 MapStruct 的文档,但似乎无法弄清楚何时应该使用注释@BeanMapping The documentation seems to extensively use just @Mapping for most cases as well.该文档似乎在大多数情况下也广泛使用了@Mapping Could someone explain the use of @BeanMapping annotation?有人可以解释@BeanMapping注释的使用吗? Perhaps with an example?也许举个例子?

The @Mapping annotation is one of the core annotation of MapStruct. @Mapping注释是@Mapping的核心注释之一。 It allows you to define mappings between properties that are not named the same. 它允许您定义未命名相同属性之间的映射。 From the JavaDoc: 从JavaDoc:

Configures the mapping of one bean attribute. 配置一个bean属性的映射。

The @BeanMapping annotation is there to control the behaviour of the entire mapping method. @BeanMapping批注用于控制整个映射方法的行为。 You can use @BeanMapping to set the NullValueCheckStrategy for the entire mapping method. 您可以使用@BeanMapping为整个映射方法设置NullValueCheckStrategy From the JavaDoc: 从JavaDoc:

Configures the mapping between two bean types 配置两个bean类型之间的映射

simply @BeanMapping let you customize the mapping process.只需@BeanMapping 即可让您自定义映射过程。

example 1: @BeanMapping(ignoreByDefault = true) means No automatic mapping will take place, so All mappings have to be defined manually.示例 1:@BeanMapping(ignoreByDefault = true) 表示不会发生自动映射,因此必须手动定义所有映射。

example 2: @BeanMapping(ignoreUnmappedSourceProperties = {"title"}) means ignore title field during the mapping process示例 2:@BeanMapping(ignoreUnmappedSourceProperties = {"title"}) 表示在映射过程中忽略标题字段

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

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