Mapping Dto, Entity, and Vo using MapStruct

Mapping Dto, Entity, and Vo using MapStruct

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…

  1. MapStruct?create implementations by reading of Annotation
  2. when Compiling (ModelMapper?has a reflection when mapping occurs)
  3. MapStruct can?check Errors?when Compiling
  4. MapStruct processing speed is?overwhelmingly faster?than ModelMapper

Because of these advantages, I will user?MapStruct

Let’s take a look at basic function and different mapping strategies

Dependency Settings

No alt text provided for this image

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

No alt text provided for this image

If DTO and Entity variable names are the same,

It’ll work

have to make Builder in UserEntity

No alt text provided for this image

DTO and Entity have the same variables

Now use the one you made before

No alt text provided for this image

Finally, let’s look at some mapping strategies

It’s on?UserMapper

Mapping Different Properties

No alt text provided for this image

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)

No alt text provided for this image

Use?ignore

Gideon Ojo

Java Software Engineer | Full Stack Developer | Passionate about SOAs/Microservices.

2 年

used this

回复
Amer Hijazi

Integration Head | Software Engineer Expert | API Management, Java, Spring boot , Apigee

2 年

Thanks you Omar

回复

要查看或添加评论,请登录

Omar Ismail的更多文章

社区洞察

其他会员也浏览了