目标列表迭代mapstruct

时间:2019-02-04 21:57:26

标签: java spring-boot java-ee mapstruct

我正在使用mapstruct并尝试映射2种不同类型的对象。就是这个要求。
目标对象的结构如下(层次结构)

List<Object1>
Object1 have List<Object2>
Object 2 have List<Object3>
Object 3 have List<Object4>

每个对象都有String对象,分别从源ListObject和StringObject映射。

SourceObject具有相同的类:

List<SourceObject1>
List<SourceObject2>
List<SourceOject3>

映射要求是:

Object1 have to map with SourceObject1
Object2 have to map with SourceObject2
Object3 have to map with SourceObject3

我使用的是1.2.0.Final版本并且能够映射 Object1必须与SourceObject1映射

//List to List mapping
@Mapping(target = "Object1", source = "SourceObject1")
Target from(Source);
@Mapping(target = "Object1[0].Object2", source = "SourceObject2") 
Mapping(target = "Object1[0].Object3", source = "SourceObject3") 
//not casting and not mapping working.Also Need to map String fields under 
list Object.

1 个答案:

答案 0 :(得分:1)

这非常简单,您只需要让mapstruct发挥作用即可! 一个简单的例子:

目标类别:

public class Object1 {
    private List<Object2> list;

    public List<Object2> getList() {
        return list;
    }

    public void setList(List<Object2> list) {
        this.list = list;
    }
}

public class Object2 {
    private List<Object3> list;

    public List<Object3> getList() {
        return list;
    }

    public void setList(List<Object3> list) {
        this.list = list;
    }
}

public class Object3 {
    private List<Object4> list;

    public List<Object4> getList() {
        return list;
    }

    public void setList(List<Object4> list) {
        this.list = list;
    }
}

public class Object4 {
    private String field;

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }
}

源类:

public class SourceObject1 {
    private List<SourceObject2> list;

    public List<SourceObject2> getList() {
        return list;
    }

    public void setList(List<SourceObject2> list) {
        this.list = list;
    }
}

public class SourceObject2 {
    private List<SourceObject3> list;

    public List<SourceObject3> getList() {
        return list;
    }

    public void setList(List<SourceObject3> list) {
        this.list = list;
    }       
}

public class SourceObject3 {
    private List<SourceObject4> list;

    public List<SourceObject4> getList() {
        return list;
    }

    public void setList(List<SourceObject4> list) {
        this.list = list;
    }
}

public class SourceObject4 {
    private String field;

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }
}

映射器的定义:

@Mapper
public interface  MapperExample {

    Object1 map(SourceObject1 source);

    Object2 map(SourceObject2 source);

    Object3 map(SourceObject3 source);

    Object4 map(SourceObject4 source);
}

生成的代码:

@Override
public Object1 map(SourceObject1 source) {
    if ( source == null ) {
        return null;
    }

    Object1 object1 = new Object1();

    object1.setList( sourceObject2ListToObject2List( source.getList() ) );

    return object1;
}

@Override
public Object2 map(SourceObject2 source) {
    if ( source == null ) {
        return null;
    }

    Object2 object2 = new Object2();

    object2.setList( sourceObject3ListToObject3List( source.getList() ) );

    return object2;
}

@Override
public Object3 map(SourceObject3 source) {
    if ( source == null ) {
        return null;
    }

    Object3 object3 = new Object3();

    object3.setList( sourceObject4ListToObject4List( source.getList() ) );

    return object3;
}

@Override
public Object4 map(SourceObject4 source) {
    if ( source == null ) {
        return null;
    }

    Object4 object4 = new Object4();

    object4.setField( source.getField() );

    return object4;
}

protected List<Object2> sourceObject2ListToObject2List(List<SourceObject2> list) {
    if ( list == null ) {
        return null;
    }

    List<Object2> list1 = new ArrayList<Object2>( list.size() );
    for ( SourceObject2 sourceObject2 : list ) {
        list1.add( map( sourceObject2 ) );
    }

    return list1;
}

protected List<Object3> sourceObject3ListToObject3List(List<SourceObject3> list) {
    if ( list == null ) {
        return null;
    }

    List<Object3> list1 = new ArrayList<Object3>( list.size() );
    for ( SourceObject3 sourceObject3 : list ) {
        list1.add( map( sourceObject3 ) );
    }

    return list1;
}

protected List<Object4> sourceObject4ListToObject4List(List<SourceObject4> list) {
    if ( list == null ) {
        return null;
    }

    List<Object4> list1 = new ArrayList<Object4>( list.size() );
    for ( SourceObject4 sourceObject4 : list ) {
        list1.add( map( sourceObject4 ) );
    }

    return list1;
}