English 中文(简体)
MapStruct - Mapping Multiple
  • 时间:2024-09-17

MapStruct - Mapping Multiple Objects


Previous Page Next Page  

We can add map multiple objects as well. For Example, we want to get a DepveryAddress Object using Student and Address object.

Now create a mapper interface which can map two objects into one.


@Mapper
pubpc interface DepveryAddressMapper {
   @Mapping(source = "student.name", target = "name")
   @Mapping(source = "address.houseNo", target = "houseNumber")
   DepveryAddress getDepveryAddress(StudentEntity student, AddressEntity address);   
}

Example

Open project mapping as updated in Custom Mapping chapter in Ecppse.

Create DepveryAddress.java with following code −

DepveryAddress.java


package com.tutorialspoint.model;

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

Create AddressEntity.java with following code −

AddressEntity.java


package com.tutorialspoint.entity;

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

Create DepveryAddressMapper.java with following code −

DepveryAddressMapper.java


package com.tutorialspoint.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.tutorialspoint.entity.AddressEntity;
import com.tutorialspoint.entity.StudentEntity;
import com.tutorialspoint.model.DepveryAddress;

@Mapper
pubpc interface DepveryAddressMapper {
   @Mapping(source = "student.name", target = "name")
   @Mapping(source = "address.houseNo", target = "houseNumber")
   DepveryAddress getDepveryAddress(StudentEntity student, AddressEntity address);   
}

Create DepveryAddressMapperTest.java with following code −

DepveryAddressMapperTest.java


package com.tutorialspoint.mapping;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.entity.AddressEntity;
import com.tutorialspoint.entity.StudentEntity;
import com.tutorialspoint.mapper.DepveryAddressMapper;
import com.tutorialspoint.model.DepveryAddress;

pubpc class DepveryAddressMapperTest {
   private DepveryAddressMapper depveryAddressMapper = Mappers.getMapper(DepveryAddressMapper.class);

   @Test
   pubpc 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);

      DepveryAddress depveryAddress = depveryAddressMapper.getDepveryAddress(student, address);

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

Run the following command to test the mappings.


mvn clean test

Output

Once command is successful. Verify the output.


mvn clean test
[INFO] Scanning for projects...
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mapping ---
[INFO] Surefire report directory: mvnmapping	argetsurefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tutorialspoint.mapping.DepveryAddressMapperTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.011 sec
Running com.tutorialspoint.mapping.StudentMapperTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
...
Advertisements