Skip to content

Instantly share code, notes, and snippets.

@evrentan
Created June 13, 2021 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrentan/1b84cd0310401242a29c9cdb8c9d7c1b to your computer and use it in GitHub Desktop.
Save evrentan/1b84cd0310401242a29c9cdb8c9d7c1b to your computer and use it in GitHub Desktop.
StudentMapper
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