Mapping Dto, Entity, and Vo using MapStruct
Omar Ismail
Senior Software Engineer @ Digitinary | Java & Spring Expert ? | AWS & Microservices Architect ? | FinTech & Open Banking Innovator ?? | Digital Payments Expert ?? | Top 200 IT Content Creator in Jordan ?? | 40K+ ??
Thanks TO : https://medium.com/@Seonggil/mapping-dto-entity-and-vo-using-mapstruct-5b6a9f73f17d
ModelMapper vs MapStruct
Once both functions are used for type conversion
Used to convert one object to another type of object. Both two
Explain the differences based on MapStructure…
Because of these advantages, I will user?MapStruct
Let’s take a look at basic function and different mapping strategies
Dependency Settings
If you use Lombok, Add “lombok-mapstruct-bunding”
Now, Create Mapper
UserMapper.java
@Mapper
public interface UserMapper{
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
UserEntity userDTOToEntity(UserDTO userDTO);
UserDTO userEntityToDTO(UserEntity userEntity);
}
Declare it as an interface,
The implant is automatically created in the?target
If DTO and Entity variable names are the same,
It’ll work
have to make Builder in UserEntity
DTO and Entity have the same variables
Now use the one you made before
Finally, let’s look at some mapping strategies
It’s on?UserMapper
Mapping Different Properties
Mapping?nick in DTO to nickname in Entity
Ignore properties
When you change DTO to an entity, if the value of the entity is not present in the DTO
DTO
String nick;
String name;
Entity
String nickname;
String name;
String Age;
In this case, the null value is added to Entity.Age (DTO → Entity)
Use?ignore
Java Software Engineer | Full Stack Developer | Passionate about SOAs/Microservices.
2 年used this
Integration Head | Software Engineer Expert | API Management, Java, Spring boot , Apigee
2 年Thanks you Omar