The meaning of DTO, VO, PO and the conversion between them

DTO (Data Transfer Object): Data transfer object. This concept comes from the design pattern of J2EE. The original purpose is to provide coarse-grained data entities for EJB distributed applications to reduce the number of distributed calls and thereby improve distributed calls. performance and reduce network load, but here I generally refer to objects used for data transfer between the presentation layer and the service layer. To put it more simply, the JSON data passed from the front end to the back end is part of the fields of the corresponding entity in the database (the purpose of this is also easy to understand. For example, if the interface you are modifying uses a complete entity, then you may only pass it in the front end. You can change what you want, but it does not rule out tampering in other situations. For example, adding other fields that cannot be modified may cause problems)
VO (View Object): View object, used for the presentation layer. Its function is to Encapsulates all data of a specified page (or component). In layman's terms, it means that after processing the back-end interface, the actual VO returned to the front-end needs the field
PO (Persistent Object) that the front-end wants: the persistence object, which is formed with the data structure of the persistence layer (usually a relational database). One-to-one mapping relationship, if the persistence layer is a relational database, then each field (or several) in the data table corresponds to one (or several) attributes of PO. This is the complete mapping object entity of the database mentioned in VO.
These three are commonly used and can be directly generated through the code generator.
Converter:
The converter is a tool used to convert PO, VO, and DTO. Directly enter the code:
 

   <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

This is a very useful tool provided by the MapStruct official website.
If the source object attribute has the same name as the target object attribute, the corresponding attribute will be automatically mapped. If it is different, you need to specify it. You can also use format to convert it to the type you want. Expressions are also supported. In this way, you can see that the nouns like id, name, and email are consistent. I did not specify source-target, but birthday-birth was specified. The conversion format birthDateFormat added dateFormat or birthExpressionFormat added expression. If you don’t want a certain attribute Mapping, you can add ignore=true
 

@Mapper
public interface PersonConverter {
    PersonConverter INSTANCE = Mappers.getMapper(PersonConverter.class);
    @Mappings({
        @Mapping(source = "birthday", target = "birth"),
        @Mapping(source = "birthday", target = "birthDateFormat", dateFormat = "yyyy-MM-dd HH:mm:ss"),
        @Mapping(target = "birthExpressionFormat", expression = "java(org.apache.commons.lang3.time.DateFormatUtils.format(person.getBirthday(),\"yyyy-MM-dd HH:mm:ss\"))"),
        @Mapping(source = "user.age", target = "age"),
        @Mapping(target = "email", ignore = true)
    })
    PersonDTO domain2dto(Person person);
    Person gerPerson(PersonDTO personDTO);
    List<PersonDTO> domain2dto(List<Person> people);
}

Then its implementation class will be automatically generated in the target
and the converter can be directly injected into the project and use the method defined by yourself.

 Person person= PersonConverter.INSTANCE.getPerson(personDTO);

The keyword @Mapper of the MapStruct annotation
will only implement this annotation on the interface. MapStruct will implement the interface
. There is a componentModel attribute in @Mapper, which mainly specifies the type of the implementation class. Generally, two
defaults are used: default, which can be passed through Mappers .getMapper(Class) method to obtain the instance object
spring: automatically add the annotation @Component on the implementation class of the interface, and inject
@Mapping through @Autowired method: attribute mapping, if the source object attribute is consistent with the target object name, the corresponding attribute will be automatically mapped
source: source attribute
target: target attribute
dateFormat: Convert String to Date date through SimpleDateFormat, the value is the date format of SimpleDateFormat
ignore: ignore this field
@Mappings: configure multiple @Mapping
@MappingTarget is used to update existing objects
@InheritConfiguration is used to inherit configuration

To view the complete functions, please refer to the official website:
 

http://mapstruct.org/documentation/stable/reference/html/

Guess you like

Origin blog.csdn.net/WXF_Sir/article/details/132497217