Created
June 13, 2021 19:15
-
-
Save evrentan/1b84cd0310401242a29c9cdb8c9d7c1b to your computer and use it in GitHub Desktop.
StudentMapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tan.myhateoasexample.mapper; | |
import com.tan.myhateoasexample.dto.Student; | |
import com.tan.myhateoasexample.dto.StudentRef; | |
import com.tan.myhateoasexample.entity.StudentEntity; | |
import org.mapstruct.*; | |
import org.mapstruct.factory.Mappers; | |
import java.util.List; | |
import java.util.Objects; | |
@Mapper( | |
componentModel = "spring", | |
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE, | |
uses = {LectureMapper.class} | |
) | |
@MapperConfig(unmappedTargetPolicy = ReportingPolicy.ERROR, unmappedSourcePolicy = ReportingPolicy.ERROR) | |
public interface StudentMapper { | |
StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class); | |
@Mappings({ | |
@Mapping(target = "id", ignore = true) | |
}) | |
StudentEntity toEntity(Student student); | |
Student toDto(StudentEntity studentEntity); | |
List<StudentEntity> toEntityList(List<Student> studentList); | |
List<Student> toDtoList(List<StudentEntity> studentEntityList); | |
@BeanMapping(ignoreUnmappedSourceProperties = {"name", "lectureList"}) | |
StudentRef toDtoRef(StudentEntity studentEntity); | |
List<StudentRef> toDtoRefList(List<StudentEntity> studentEntityList); | |
@AfterMapping | |
default void setEntityId(Student student, @MappingTarget StudentEntity studentEntity) { | |
if(Objects.nonNull(student.getId())) | |
studentEntity.setId(student.getId()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment