- MapStruct - Home
- MapStruct - Overview
- MapStruct - Environment Setup
- MapStruct - Mapping
- MapStruct - Basic Mapping
- MapStruct - Custom Mapping
- MapStruct - Mapping Multiple
- MapStruct - Mapping Nested Bean
- MapStruct - Mapping Direct Field
- MapStruct - Builder
- MapStruct - Data Type Conversions
- MapStruct - Implicit Type Conversion
- MapStruct - Using numberFormat
- MapStruct - Using dateFormat
- MapStruct - Using expression
- MapStruct - Using constant
- MapStruct - Using defaultValue
- MapStruct - Using defaultExpression
- MapStruct - Mapping Collections
- MapStruct - Mapping List
- MapStruct - Mapping Map
- Mapstruct - Miscellaneous
- MapStruct - Mapping Streams
- MapStruct - Mapping Enum
- MapStruct - Throwing Exceptions
- MapStruct - Customizing Mapper
- MapStruct - Useful Resources
- MapStruct - Quick Guide
- MapStruct - Useful Resources
- MapStruct - Discussion
MapStruct - Mapping Multiple Objects
We can add map multiple objects as well. For Example, we want to get a DeliveryAddress Object using Student and Address object.
Now create a mapper interface which can map two objects into one.
@Mapper
public interface DeliveryAddressMapper {
@Mapping(source = "student.name", target = "name")
@Mapping(source = "address.houseNo", target = "houseNumber")
DeliveryAddress getDeliveryAddress(StudentEntity student, AddressEntity address);
}
Example - Mapping Multiple objects
Open project mapping as updated in Custom Mapping chapter in Eclipse.
Create DeliveryAddress.java with following code −
DeliveryAddress.java
package com.tutorialspoint.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;
}
}
Create AddressEntity.java with following code −
AddressEntity.java
package com.tutorialspoint.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;
}
}
Create DeliveryAddressMapper.java with following code −
DeliveryAddressMapper.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.DeliveryAddress;
@Mapper
public interface DeliveryAddressMapper {
@Mapping(source = "student.name", target = "name")
@Mapping(source = "address.houseNo", target = "houseNumber")
DeliveryAddress getDeliveryAddress(StudentEntity student, AddressEntity address);
}
Create DeliveryAddressMapperTest.java with following code −
DeliveryAddressMapperTest.java
package com.tutorialspoint.test;
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.DeliveryAddressMapper;
import com.tutorialspoint.model.DeliveryAddress;
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());
}
}
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] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 s -- in com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m [INFO] Running com.tutorialspoint.test.[1mStudentMapperTest[m [INFO] [1;32mTests run: [0;1;32m2[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 s -- in com.tutorialspoint.test.[1mStudentMapperTest[m [INFO] [INFO] Results: [INFO] [INFO] [1;32mTests run: 3, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 3.279 s [INFO] Finished at: 2025-09-13T15:26:57+05:30 [INFO] [1m------------------------------------------------------------------------[m
Advertisements