mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
* #2715: added an example with an update mapper for Conditional behavior.
This commit is contained in:
parent
7bb85d05c0
commit
9b434f80f8
@ -354,6 +354,56 @@ public class CarMapperImpl implements CarMapper {
|
|||||||
----
|
----
|
||||||
====
|
====
|
||||||
|
|
||||||
|
When using this in combination with an update mapping method it will replace the `null-check` there, for example:
|
||||||
|
|
||||||
|
.Update mapper using custom condition check method
|
||||||
|
====
|
||||||
|
[source, java, linenums]
|
||||||
|
[subs="verbatim,attributes"]
|
||||||
|
----
|
||||||
|
@Mapper
|
||||||
|
public interface CarMapper {
|
||||||
|
|
||||||
|
CarDto carToCarDto(Car car, @MappingTarget CarDto carDto);
|
||||||
|
|
||||||
|
@Condition
|
||||||
|
default boolean isNotEmpty(String value) {
|
||||||
|
return value != null && !value.isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
====
|
||||||
|
|
||||||
|
The generated update mapper will look like:
|
||||||
|
|
||||||
|
.Custom condition check in generated implementation
|
||||||
|
====
|
||||||
|
[source, java, linenums]
|
||||||
|
[subs="verbatim,attributes"]
|
||||||
|
----
|
||||||
|
// GENERATED CODE
|
||||||
|
public class CarMapperImpl implements CarMapper {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CarDto carToCarDto(Car car, CarDto carDto) {
|
||||||
|
if ( car == null ) {
|
||||||
|
return carDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isNotEmpty( car.getOwner() ) ) {
|
||||||
|
carDto.setOwner( car.getOwner() );
|
||||||
|
} else {
|
||||||
|
carDto.setOwner( null );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mapping of other properties
|
||||||
|
|
||||||
|
return carDto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
====
|
||||||
|
|
||||||
[IMPORTANT]
|
[IMPORTANT]
|
||||||
====
|
====
|
||||||
If there is a custom `@Condition` method applicable for the property it will have a precedence over a presence check method in the bean itself.
|
If there is a custom `@Condition` method applicable for the property it will have a precedence over a presence check method in the bean itself.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user