mapstruct-plus/docs/en/guide/configuration.md
2024-06-15 17:21:05 +08:00

9.5 KiB
Raw Blame History

title, order, category, description
title order category description
configuration 6
Guide
MapStructPlus MapStructPlus配置项 configuration

MapStructPlus provides multiple configuration items to specify some behavior when the conversion interface is generated.

How to use it

In the module that needs to be configured, create a new configuration class an annotate it with @MapperConfig annotation.

In a module, there can noly be one class with this annotation.

Also, note that the configuration classes must be placed in the module to be effective.

eg

@MapperConfig(adapterClassName = "DemoConvertMapperAdapter",
    adapterPackage = "io.github.linpeilie.adapter",
    mapAdapterClassName = "DemoMapConvertMapperAdapter")
public class MapStructPlusConfiguration {
}

In addition, the configuration property supports adding compilation parameters to the compiler in the form of -Akey=value.

For example, when using Maven, you can use the compilerArgs property in the maven-compiler-plugin plugin configuration to configure delivery, for example:

And configuration in this way takes precedence, that is, when the mode and configuration class exist together, the property configured in this way takes precedence. This feature is supported from 1.3.0.

eg:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
        <source>${maven.compiler.source}</source>
        <target>${maven.compiler.target}</target>
        <annotationProcessorPaths>
          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
          </path>
          <path>
            <groupId>io.github.linpeilie</groupId>
            <artifactId>mapstruct-plus-processor</artifactId>
            <version>${mapstruct-plus.version}</version>
          </path>
          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>0.2.0</version>
          </path>
        </annotationProcessorPaths>
        <compilerArgs>
          <arg>-Amapstruct.plus.adapterClassName=DemoConvertMapperAdapter</arg>
          <arg>-Amapstruct.plus.adapterPackage=io.github.linpeilie.adapter</arg>
          <arg>-Amapstruct.plus.mapAdapterClassName=DemoMapConvertMapperAdapter</arg>
        </compilerArgs>
      </configuration>
    </plugin>
  </plugins>
</build>

Configuration Item

mapperPackage

  • DescriptionThe package name of the generated Mapper transformation interfaces
  • TypeString
  • DefaultThe default is under the same package as the class to be converted.
  • Compile Parameter-Amapstruct.plus.mapperPackage

unmappedSourcePolicy

  • DescriptionPolicy when there is no corresponding attribute in the source class
  • TypeReportingPolicy
  • Optional
    • IGNOREignore
    • WARNprint warning log
    • ERRORthrow exception
  • DefaultIGNORE
  • Compile Parameter-Amapstruct.plus.unmappedSourcePolicy

unmappedTargetPolicy

  • DescriptionPolicy when there is no corresponding attribute in the target class
  • TypeReportingPolicy
  • Optional
    • IGNOREignore
    • WARNprint warning log
    • ERRORthrow exception
  • DefaultIGNORE
  • Compile Parameter-Amapstruct.plus.unmappedTargetPolicy

nullValueMappingStrategy

  • DescriptionNull object handing policy
  • TypeNullValueMappingStrategy
  • Optional
    • RETURN_NULLreturn null
    • RETURN_DEFAULTreturn default value
  • DefaultRETURN_NULL
  • Compile Parameter-Amapstruct.plus.nullValueMappingStrategy

nullValuePropertyMappingStrategy

  • DescriptionPolicy to deal with when the property value is null
  • TypeNullValuePropertyMappingStrategy
  • Optional
    • SET_TO_NULLsetting is null
    • SET_TO_DEFAULTsetting is default value
    • IGNOREignore
  • DefaultSET_TO_NULL
  • Compile Parameter-Amapstruct.plus.nullValuePropertyMappingStrategy

builder

  • DescriptionConstructor mode configuration, MapStruct loses the parent class property when used with Lombok's builder, so the default constructor mode is turned off.
  • TypeBuilder
  • Optional
    • buildMethodThe constructor creates the constructor when the type is to be build
    • disableBuilderOpen/Close the constructor, and if closed, use only regular getters/setters
  • Default
    • buildMethodbuild
    • disableBuildertrue
  • Compile Parameter
    • -Amapstruct.plus.builder.buildMethod
    • -Amapstruct.plus.builder.disableBuilder

adapterPackage

since 1.2.3

  • DescriptionThe package name of ConvertAdapterClass and MapConvertMapperAdapter
  • TypeString
  • Defaultio.github.linpeilie
  • Compile Parameter-Amapstruct.plus.adapterPackage

adapterClassName

since 1.2.3

  • Descriptionthe class name of ConvertAdapterClass
  • TypeString
  • DefaultConvertMapperAdapter
  • Compile Parameter-Amapstruct.plus.adapterClassName

mapAdapterClassName

since 1.2.3

  • Descriptionthe class name of MapConvertMapperAdapter
  • TypeString
  • DefaultMapConvertMapperAdapter
  • Compile Parameter-Amapstruct.plus.mapAdapterClassName

autoConfigPackage

since 1.3.6

  • DescriptionThe package path of the automatically generated configuration class --- AutoMapperConfig/AutoMapMapperConfig --- from the MapStructPlus framework.
  • TypeString
  • Defaultio.github.linpeilie
  • Compile Parameter-Amapstruct.plus.autoConfigPackage

autoMapperConfigClassName

since 1.3.6

  • DescriptionMapStructPlus framework automatically generates the name of the configuration class(transformation between configuration objects)
  • TypeString
  • DefaultAutoMapperConfig
  • Compile Parameter-Amapstruct.plus.autoMapperConfigClassName

autoMapMapperConfigClassName

since 1.3.6

  • DescriptionMapStructPlus framework automatically generates the name of the configuration class(which configures the transformation between the Map and the object)
  • TypeString
  • DefaultAutoMapMapperConfig
  • Compile Parameter-Amapstruct.plus.autoMapMapperConfigClassName

typeConversionPolicy

since 1.4.2

  • DescriptionHow lossy (narrowing) conversion, for instance: long to integer should be reported.
  • TypeReportingPolicy
  • Optional
    • IGNORE
    • WARN
    • ERROR
  • DefaultIGNORE
  • Compile Parameter-Amapstruct.plus.typeConversionPolicy

collectionMappingStrategy

since 1.4.2

  • DescriptionThe strategy to be applied when propagating the value of collection-typed properties. By default, only JavaBeans accessor methods (setters or getters) will be used, but it is also possible to invoke a corresponding adder method for each element of the source collection (e. g. orderDto.
  • TypeCollectionMappingStrategy
  • Optional
    • ACCESSOR_ONLY
    • SETTER_PREFERRED
    • ADDER_PREFERRED
    • TARGET_IMMUTABLE
  • DefaultACCESSOR_ONLY
  • Compile Parameter-Amapstruct.plus.collectionMappingStrategy

nullValueIterableMappingStrategy

since 1.4.2

  • DescriptionThe strategy to be applied when null is passed as source argument value to an IterableMapping. If no strategy is configured, the strategy given via nullValueMappingStrategy() will be applied, using NullValueMappingStrategy.RETURN_NULL by default.
  • TypeNullValueMappingStrategy
  • Optional
    • RETURN_NULL
    • RETURN_DEFAULT
  • DefaultRETURN_NULL
  • Compile Parameter-Amapstruct.plus.nullValueIterableMappingStrategy

nullValueMapMappingStrategy

since 1.4.2

  • Descriptionhe strategy to be applied when null is passed as source argument value to a MapMapping. If no strategy is configured, the strategy given via nullValueMappingStrategy() will be applied, using NullValueMappingStrategy.RETURN_NULL by default.
  • TypeNullValueMappingStrategy
  • Optional
    • RETURN_NULL
    • RETURN_DEFAULT
  • DefaultRETURN_NULL
  • Compile Parameter-Amapstruct.plus.nullValueMapMappingStrategy

nullValueCheckStrategy

since 1.4.2

  • DescriptionDetermines when to include a null check on the source property value of a bean mapping.
  • TypeNullValueCheckStrategy
  • Optional
    • ON_IMPLICIT_CONVERSION
    • ALWAYS
  • DefaultON_IMPLICIT_CONVERSION
  • Compile Parameter-Amapstruct.plus.nullValueCheckStrategy

mappingControl

since 1.4.2

  • DescriptionAllows detailed control over the mapping process.
  • TypeClass
  • DefaultMappingControl.class

unexpectedValueMappingException

since 1.4.2

  • DescriptionException that should be thrown by the generated code if no mapping matches for enums. If no exception is configured, IllegalArgumentException will be used by default.
  • TypeClass
  • DefaultIllegalArgumentException.class

suppressTimestampInGenerated

since 1.4.2

  • DescriptionFlag indicating whether the addition of a time stamp in the @Generated annotation should be suppressed. i. e. not be added. The method overrides the flag set through an annotation processor option.
  • Typeboolean
  • Defaultfalse
  • Compile Parameter-Amapstruct.plus.suppressTimestampInGenerated