Explanation of MapStruct Entity Conversion and List Conversion Method

  • 2021-07-06 10:55:12
  • OfStack

In development, we often need to convert PO to DTO, DTO to PO and other entities. The famous ones are BeanUtil and ModelMapper, which are simple to use, but they are not enough in slightly complex business scenarios. MapStruct this plug-in can be used to deal with domin entity class and model class attribute mapping, configurable. Only one Mapper interface needs to be defined, and MapStruct will automatically implement this mapping interface, thus avoiding complicated mapping implementation. MapStruct official website address: http://mapstruct.org/

Introducing dependency


    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-jdk8</artifactId>
      <version>1.1.0.Final</version>
    </dependency>

Demand

We assume that a student student class needs to be converted into a user user class, and the student information is stored in the user information base

At this point, the Student class is as follows:


public class Student {
  private Integer id;
  private String name;
  private Integer age;
  private String sex;
  //setters, getters, toString()  Method is omitted here, but it needs to be written in actual development  
  }

At this point, the User class is as follows:


public class User {
  private Integer id;
  private String name;
  private Integer age;
  private String sex;
  //setters, getters, toString()  Method is omitted here, but it needs to be written in actual development  
 }

Convert model

At this time, the attribute names of Student and User are the same, so the conversion interface is


import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface UserMapping {
  /**
   * Student  Convert to  User
   * @param Student
   * @return
   */
   User studentToUser(Student student);
}

mvn clean compile should be compiled before the program runs, so that mapstruct framework generates UserMappingImpl implementation class.

Transformation model property mismatch

If the attribute names of User and Student do not correspond, for example:

At this point, the Student class is as follows:


public class Student {
  private Integer id;
  private String sname;
  private Integer age;
  private String sex;
  //setters, getters, toString()  Method is omitted here, but it needs to be written in actual development  
  }

At this point, the User class is as follows:


public class User {
  private Integer id;
  private String uname;
  private Integer age;
  private String sex;
  //setters, getters, toString()  Method is omitted here, but it needs to be written in actual development  
 }

Then the conversion interface is


import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
@Mapper(componentModel = "spring")
public interface UserMapping {
  /**
   * Student  Convert to  User
   * @param Student
   * @return
   */
   @Mappings({
      @Mapping(target = "uname", source = "sname")
     //  Multiple attributes do not correspond. You can use  " , "  Write multiple @Mapping
     // ,@Mapping(target = "uname", source = "sname")
  })
   User studentToUser(Student student);
}

Conversion model attribute type mismatch

For example, the sex field type of the user class is changed to boolean

At this point, the User class is as follows:


public class User {
  private Integer id;
  private String uname;
  private Integer age;
  private boolean sex;
  //setters, getters, toString()  Method is omitted here, but it needs to be written in actual development  
 }

This conversion requirement is a bit abnormal, so we need to write a conversion class by ourselves


public class UserTransform {
  public String booleanToString(boolean value){
    if(value){
      return " Male ";
    }
    return " Female ";
  }
  public boolean strToBoolean(String str){
    if (" Male ".equals(str)) {
      return true;
    }
    return false;
  }
}

Then the conversion interface is (using the UserTransform. class class)


import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
@Mapper(componentModel = "spring",uses = UserTransform.class)
public interface UserMapping {
  /**
   * Student  Convert to  User
   * @param Student
   * @return
   */
   @Mappings({
      @Mapping(target = "uname", source = "sname")
     //  Multiple attributes do not correspond. You can use  " , "  Write multiple @Mapping
     // ,@Mapping(target = "uname", source = "sname")
  })
   User studentToUser(Student student);
}

Convert list

When user and student are both set form list, the following transformation should be performed

Transform List < > Collection must have entity transformation, because in the implementation, List transformation is called entity transformation by for loop. Therefore, when the attribute names do not correspond, the attribute name mapping configuration of @ Mappings should be performed in entity transformation, and then the transformation of list will inherit this mapping with attributes.

For example, attribute names are the same


public class Student {
  private Integer id;
  private String name;
  private Integer age;
  private String sex;
  //setters, getters, toString()  Method is omitted here, but it needs to be written in actual development  
  }
0

Attribute name is different:


public class Student {
  private Integer id;
  private String name;
  private Integer age;
  private String sex;
  //setters, getters, toString()  Method is omitted here, but it needs to be written in actual development  
  }
1

Shows an automatically generated implementation of UserMappingImpl (this class is automatically generated after executing mvn clean compile)


public class Student {
  private Integer id;
  private String name;
  private Integer age;
  private String sex;
  //setters, getters, toString()  Method is omitted here, but it needs to be written in actual development  
  }
2

Reference: https://www.ofstack.com/article/157763. htm

Summarize


Related articles: