当前位置:网站首页>Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!

Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!

2022-06-21 11:38:00 Java confidant_

The 3rd of this month ,MapStruct 1.5.0 Final Release , This official version is almost past the release of the last official version 7 year ( The last official version was released in 2015 year 11 month ), This release fixes 110 Multiple bug Outside , There are also some new features to watch out for :

  • Support Map<String,?> To bean Transformation

  • Support more complete conditional conversion (Conditional mapping)

  • Support the conversion between subclasses (Support for subclass mapping)

1. newly added Map To Java bean Transformation (Mapping from Map to Bean)

If we have the following Java Bean

public class Customer {
    private Long id;
    private String name;
    //getters and setter omitted for brevity
}

Corresponding MapStruct The code is as follows :

@Mapper
public interface CustomerMapper {
    @Mapping(target = "name", source = "customerName")
    Customer toCustomer(Map<String, String> map);
}

That will eventually generate conversion code similar to the following :

// GENERATED CODE
public class CustomerMapperImpl implements CustomerMapper {
 
    @Override
    public Customer toCustomer(Map<String, String> map) {
        // ...
        if ( map.containsKey( "id" ) ) {
            customer.setId( Integer.parseInt( map.get( "id" ) ) );
        }
        if ( map.containsKey( "customerName" ) ) {
            customer.setName( source.get( "customerName" ) );
        }
        // ...
    }
}

But be careful , To be converted Map Of key Must be String Type of , otherwise , The conversion code will skip this key

2. More complete conditional transformation ( Conditional Mapping

from 1.5.0 Final Before the release , If Java bean contains hasXXX perhaps isXXX Such methods (XXX yes bean Attribute name in ), be MapStruct In the generated code, this kind of method will be called to determine whether it is in the converted bean Whether the original attribute , But unfortunately , Most of the time , We cannot directly modify the original bean Code for . Based on this ,1.5.0 Final Version introduced org.mapstruct.Condition Annotation to implement conditional transformation . For example, we have the following conversion code :

@Mapper
public interface CarMapper {
 
    CarDto carToCarDto(Car car);
 
    @Condition
    default boolean isNotEmpty(String value) {
        return value != null && !value.isEmpty();
    }
}

be Map Struct 1.5.0 Final The generated code is :

// GENERATED CODE
public class CarMapperImpl implements CarMapper {
 
    @Override
    public CarDto carToCarDto(Car car) {
        if ( car == null ) {
            return null;
        }
        CarDto carDto = new CarDto();
        if ( isNotEmpty( car.getOwner() ) ) {
            carDto.setOwner( car.getOwner() );
        }
        // Mapping of other properties
        return carDto;
    }
}

org.mapstruct.Condition Except for the whole bean You can also modify specific attribute values , Realization bean Conditional transformation of attribute dimension .

3. Add support for subclass conversion (Subclass Mapping)

If there is a parent class Fruit And two subclasses Apple and Banana, With the support of new features, our conversion code can be written more succinctly :

@Mapper
public interface FruitMapper {

    @SubclassMapping( source = AppleDto.class, target = Apple.class )
    @SubclassMapping( source = BananaDto.class, target = Banana.class )
    Fruit map( FruitDto source );
}

If Fruit It is an abstract class or interface , Compile error will be reported .

recommend

Java Interview questions

Technical involution group , Learn together !!

1fc0ce5feb42997ac33c6856e96b695a.png

PS: Because the official account platform changed the push rules. , If you don't want to miss the content , Remember to click after reading “ Looking at ”, Add one “ Star standard ”, In this way, each new article push will appear in your subscription list for the first time . spot “ Looking at ” Support us !

原网站

版权声明
本文为[Java confidant_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211122576130.html