MapStruct 映射多个对象

我们也可以添加映射多个对象。 例如,我们想要使用 Student 和 Address 对象获取 DeliveryAddress 对象。

现在创建一个映射器接口,它可以将两个对象映射为一个。

@Mapper
public interface DeliveryAddressMapper {
   @Mapping(source = "student.name", target = "name")
   @Mapping(source = "address.houseNo", target = "houseNumber")
   DeliveryAddress getDeliveryAddress(StudentEntity student, AddressEntity address);   
}

示例

打开自定义映射章节中更新的项目映射。

使用以下代码创建 DeliveryAddress.java

DeliveryAddress.java

package com.jiyik.model;

public class DeliveryAddress {
    private String name;
    private int houseNumber;
    private String city;
    private String state;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getHouseNumber() {
        return houseNumber;
    }
    public void setHouseNumber(int houseNumber) {
        this.houseNumber = houseNumber;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}

使用以下代码创建 AddressEntity.java

AddressEntity.java

package com.jiyik.entity;

public class AddressEntity {
    private int houseNo;
    private String city;
    private String state;
    public int getHouseNo() {
        return houseNo;
    }
    public void setHouseNo(int houseNo) {
        this.houseNo = houseNo;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}

使用以下代码创建 DeliveryAddressMapper.java

DeliveryAddressMapper.java

package com.jiyik.mapper;

import com.jiyik.entity.AddressEntity;
import com.jiyik.entity.StudentEntity;
import com.jiyik.model.DeliveryAddress;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper
public interface DeliveryAddressMapper {
    @Mapping(source = "student.name", target = "name")
    @Mapping(source = "address.houseNo", target = "houseNumber")
    DeliveryAddress getDeliveryAddress(StudentEntity student, AddressEntity address);
}

使用以下代码创建 DeliveryAddressMapperTest.java

DeliveryAddressMapperTest.java

import com.jiyik.entity.AddressEntity;
import com.jiyik.entity.StudentEntity;
import com.jiyik.mapper.DeliveryAddressMapper;
import com.jiyik.model.DeliveryAddress;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DeliveryAddressMapperTest {
    private DeliveryAddressMapper deliveryAddressMapper = Mappers.getMapper(DeliveryAddressMapper.class);

    @Test
    public void testEntityToModel() {
        StudentEntity student = new StudentEntity();
        student.setClassVal("X");
        student.setName("John");
        student.setId(1);

        AddressEntity address = new AddressEntity();
        address.setCity("Y");
        address.setState("Z");
        address.setHouseNo(1);

        DeliveryAddress deliveryAddress = deliveryAddressMapper.getDeliveryAddress(student, address);

        assertEquals(deliveryAddress.getName(), student.getName());
        assertEquals(deliveryAddress.getCity(), address.getCity());
        assertEquals(deliveryAddress.getState(), address.getState());
        assertEquals(deliveryAddress.getHouseNumber(), address.getHouseNo());
    }
}

运行以下命令来测试映射。

$ mvn clean test

执行结果如下

mapstruct 映射多对象

查看笔记

扫码一下
查看教程更方便