基于grpc从零开始搭建一个准生产分布式应用(6) - 05 - MapStruct特殊实现

一、Clone

@Mapper(mappingControl = DeepClone.class)//这里需要注意用这个注解
public interface Cloner {
    Cloner MAPPER = Mappers.getMapper( Cloner.class );
    CustomerDto clone(CustomerDto customerDto);
}

二、特殊注解

2.1、@qualifiedBy

这个注解的作用是当有重复的转换类时,指定一个,但也可以用注解来实现,下面的例子就是从一个集合中的对象中特定的地方取值然后mapping到特定的属性上,下面是一个集合应用。

public class Source {
    private List myIntegers;
    private List myStrings;
}
public class Target {
    private Integer myInteger;
    private String myString;
}
public static void main( String[] args ) {
    Source s = new Source();
    s.setMyIntegers( Arrays.asList( 5, 3, 7 ) );
    s.setMyStrings( Arrays.asList( "five", "three", "seven " ) );

    Target t = SourceTargetMapper.MAPPER.toTarget( s );
    System.out.println( t.getMyInteger() );
    System.out.println( t.getMyString() );
}
//5
//seven
@Mapper( uses = IterableNonInterableUtil.class )
public interface SourceTargetMapper {
    SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );
    @Mapping( source = "myIntegers", target = "myInteger", qualifiedBy = FirstElement.class )
    @Mapping( source = "myStrings", target = "myString", qualifiedBy = LastElement.class )
    Target toTarget( Source s );
}

@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface FirstElement {
}

@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface LastElement {
}
public class IterableNonInterableUtil {

    @FirstElement
    public  T first( List in ) {
        if ( in != null && !in.isEmpty() ) {
            return in.get( 0 );
        }
        else {
            return null;
        }
    }

    @LastElement
    public  T last( List in ) {
        if ( in != null && !in.isEmpty() ) {
            return in.get( in.size() - 1 );
        }
        else {
            return null;
        }
    }
}

2.2、@ObjectFactory

@Mapper
public interface SourceTargetMapper {

    SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );

    @Mapping( target = "descriptionArticle1", source = "brush.description" )
    @Mapping( target = "descriptionArticle2", source = "paste.description" )
    CombinedOfferingEntity toEntity(Toothbrush brush, ToothPaste paste, @Context ArticleRepository repo);

    @ObjectFactory
    default  T lookup(Toothbrush brush, ToothPaste paste, @Context ArticleRepository repo,
        @TargetType Class targetType ) {
        ComposedKey key = new ComposedKey(brush.getName(), paste.getName() );
        CombinedOfferingEntity entity = repo.lookup( key );
        if ( entity == null ) {
            entity = new CombinedOfferingEntity();
        }
        return (T) entity;
    }
}

//生成的代码如下,上面代码大概的逻辑是先执行工厂方法,然后先执行工厂方法,再执行余下的映射
public CombinedOfferingEntity toEntity(Toothbrush brush, ToothPaste paste, ArticleRepository repo) {
    if ( brush == null && paste == null ) {
        return null;
    }

    CombinedOfferingEntity combinedOfferingEntity = lookup( brush, paste, repo, CombinedOfferingEntity.class );

    if ( brush != null ) {
        combinedOfferingEntity.setDescriptionArticle1( brush.getDescription() );
    }
    if ( paste != null ) {
        combinedOfferingEntity.setDescriptionArticle2( paste.getDescription() );
    }

    return combinedOfferingEntity;
}

2.3、@Context循环依赖

public class Employee {
    private String name;
    private Employee reportsTo;
    private List team;
}
public class EmployeeDto {
    private String employeeName;
    private EmployeeDto reportsTo;
    private List team;
}
@Mapper
public interface EmployeeMapper {

    EmployeeMapper MAPPER = Mappers.getMapper( EmployeeMapper.class );

    @Mapping(source = "employeeName", target = "name")
    Employee toEmployee(EmployeeDto employeeDto, @Context CycleAvoidingMappingContext context);

    @InheritInverseConfiguration
    EmployeeDto fromEmployee(Employee employee, @Context CycleAvoidingMappingContext context);
}
public class CycleAvoidingMappingContext {
    private Map knownInstances = new IdentityHashMap();

    @BeforeMapping
    public  T getMappedInstance(Object source, @TargetType Class targetType) {
        return (T) knownInstances.get( source );
    }

    @BeforeMapping
    public void storeMappedInstance(Object source, @MappingTarget Object target) {
        knownInstances.put( source, target );
    }
}

三、MapToObject互转

public class Source {
    private Map map;
}
public class Target {
    private String ip;
    private String server;
}

@Mapper( uses = MappingUtil.class )
public interface SourceTargetMapper {

    SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );

    @Mapping(source = "map", target = "ip", qualifiedBy = Ip.class )
    @Mapping(source = "map", target = "server", qualifiedBy = Server.class )
    Target toTarget(Source s);
}

public class MappingUtil {

    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.SOURCE)
    public @interface Ip {
    }

    @Qualifier
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.SOURCE)
    public static @interface Server {
    }

    @Ip
    public String ip(Map in) {
        return (String) in.get("ip");
    }

    @Server
    public String server(Map in) {
        return (String) in.get("server");
    }
}

@Test
public void testMapperOnExistingIpAndServer() {

    Map map = new HashMap<>();
    map.put("ip", "127.0.0.1");
    map.put("server", "168.192.1.1");

    Source s = new Source(map);
    Target t = SourceTargetMapper.MAPPER.toTarget( s );
    System.out.println(JSONUtil.toJsonStr(t));
    //{"server":"168.192.1.1","ip":"127.0.0.1"}
}
//下面的例子是从map中映射字段到object
public class Employee {

    private String id;
    private String name;
    private Department department;
}
public class Department {
    private String id;
    private String name;
}

@Mapper
public interface MapToBeanMapper {

    MapToBeanMapper INSTANCE = Mappers.getMapper(MapToBeanMapper.class);

    @Mapping(target = "department", ignore = true)
    Employee fromMap(Map map);

    @AfterMapping
    default void finishEmployee(@MappingTarget Employee employee, Map map) {
        employee.setDepartment(fromMapToDepartment(map));
    }

    @Mapping(target = "id", source = "did")
    @Mapping(target = "name", source = "dname")
    Department fromMapToDepartment(Map map);
}

@Test
public void shouldMapMapToBean() {
    Map map = new HashMap<>();
    map.put("id", "1234");
    map.put("name", "Tester");
    map.put("did", "4321"); //Department Id
    map.put("dname", "Test");// Depart name

    Employee employee = MapToBeanMapper.INSTANCE.fromMap(map);
}

四、SPI实现

//主要是用于一些特定的规则处理
step-1:
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy {

    @Override
    public boolean isGetterMethod(ExecutableElement method) {
        String methodName = method.getSimpleName().toString();
        return !methodName.startsWith( "with" ) && method.getReturnType().getKind() != TypeKind.VOID;
    }

    @Override
    public boolean isSetterMethod(ExecutableElement method) {
        String methodName = method.getSimpleName().toString();
        return methodName.startsWith( "with" ) && methodName.length() > 4;
    }

    @Override
    public String getPropertyName(ExecutableElement getterOrSetterMethod) {
        String methodName = getterOrSetterMethod.getSimpleName().toString();
        return Introspector.decapitalize( methodName.startsWith( "with" ) ? methodName.substring(  4 ) : methodName );
    }
}
step-2
在 resources/META-INF.services目录下新建文件
org.mapstruct.example.spi.CustomAccessorNamingStrategy
文件内容
org.mapstruct.example.spi.CustomAccessorNamingStrategy

五、lombok配置

这主要是配置,以指定comiple的顺序


    
        org.apache.maven.plugins
        maven-compiler-plugin
        3.8.1
        
            1.8
            1.8
            
                
                    org.projectlombok
                    lombok
                    ${lombok.version} //1.18.20 || 1.18.20
                
                
                    org.mapstruct
                    mapstruct-processor
                    ${mapstruct.version} //1.4.2.Final || 1.5.0.Beta2
                

                
                
                    org.projectlombok
                    lombok-mapstruct-binding
                    0.1.0  //0.1.0 || 0.2.0
                
            
        
    

你可能感兴趣的:(开发语言,DDD,系统架构,架构设计,grpc,springboot,java)