From b57b9922cd3b147d44dcdefe3ef22f551e1f1362 Mon Sep 17 00:00:00 2001 From: mgucma Date: Mon, 12 Aug 2024 10:56:11 +0200 Subject: [PATCH 1/2] Spring Boot Mapstruct --- .../mapstruct/config/MapperConfig.java | 11 +++++++ .../academy/mapstruct/mapper/GroupMapper.java | 9 ++++++ .../mapstruct/mapper/StudentMapper.java | 30 +++++++++++++++++++ .../mapstruct/mapper/SubjectMapper.java | 4 +++ .../service/student/StudentServiceImpl.java | 2 +- src/main/resources/application.properties | 4 +-- 6 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/mate/academy/mapstruct/config/MapperConfig.java diff --git a/src/main/java/mate/academy/mapstruct/config/MapperConfig.java b/src/main/java/mate/academy/mapstruct/config/MapperConfig.java new file mode 100644 index 0000000..9e97a43 --- /dev/null +++ b/src/main/java/mate/academy/mapstruct/config/MapperConfig.java @@ -0,0 +1,11 @@ +package mate.academy.mapstruct.config; + +import org.mapstruct.InjectionStrategy; +import org.mapstruct.NullValueCheckStrategy; + +@org.mapstruct.MapperConfig(componentModel = "spring", + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + implementationPackage = "mate.academy.mapstruct.mapper.impl") +public class MapperConfig { +} diff --git a/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java b/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java index d60c9df..493bdb9 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java @@ -1,11 +1,20 @@ package mate.academy.mapstruct.mapper; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.group.CreateGroupRequestDto; import mate.academy.mapstruct.dto.group.GroupDto; import mate.academy.mapstruct.model.Group; +import org.mapstruct.Mapper; +import org.mapstruct.Named; +@Mapper(config = MapperConfig.class) public interface GroupMapper { GroupDto toDto(Group group); Group toModel(CreateGroupRequestDto requestDto); + + @Named("groupById") + default Group groupById(Long id) { + return new Group(id); + } } diff --git a/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java b/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java index 853e6b1..ad2dad8 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java @@ -1,14 +1,44 @@ package mate.academy.mapstruct.mapper; +import java.util.List; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.student.CreateStudentRequestDto; import mate.academy.mapstruct.dto.student.StudentDto; import mate.academy.mapstruct.dto.student.StudentWithoutSubjectsDto; import mate.academy.mapstruct.model.Student; +import mate.academy.mapstruct.model.Subject; +import org.mapstruct.AfterMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; +@Mapper(config = MapperConfig.class, + uses = {GroupMapper.class}) public interface StudentMapper { + @Mapping(source = "group.id", target = "groupId") + @Mapping(target = "subjectIds", ignore = true) StudentDto toDto(Student student); + @AfterMapping + default void setSubjectId(@MappingTarget StudentDto studentDto, Student student) { + List subjectIds = student.getSubjects().stream() + .map(Subject::getId) + .toList(); + studentDto.setSubjectIds(subjectIds); + } + + @Mapping(source = "group.id", target = "groupId") StudentWithoutSubjectsDto toStudentWithoutSubjectsDto(Student student); + @Mapping(source = "groupId", target = "group", qualifiedByName = "groupById") + @Mapping(target = "subjects", ignore = true) Student toModel(CreateStudentRequestDto requestDto); + + @AfterMapping + default void setSubjects(@MappingTarget Student student, CreateStudentRequestDto requestDto) { + List subjects = requestDto.subjects().stream() + .map(Subject::new) + .toList(); + student.setSubjects(subjects); + } } diff --git a/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java b/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java index 2906e78..74a383f 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java @@ -1,11 +1,15 @@ package mate.academy.mapstruct.mapper; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.subject.CreateSubjectRequestDto; import mate.academy.mapstruct.dto.subject.SubjectDto; import mate.academy.mapstruct.model.Subject; +import org.mapstruct.Mapper; +@Mapper(config = MapperConfig.class) public interface SubjectMapper { SubjectDto toDto(Subject subject); Subject toModel(CreateSubjectRequestDto requestDto); + } diff --git a/src/main/java/mate/academy/mapstruct/service/student/StudentServiceImpl.java b/src/main/java/mate/academy/mapstruct/service/student/StudentServiceImpl.java index 420ffcf..d8aa1e4 100644 --- a/src/main/java/mate/academy/mapstruct/service/student/StudentServiceImpl.java +++ b/src/main/java/mate/academy/mapstruct/service/student/StudentServiceImpl.java @@ -35,7 +35,7 @@ public List findAll() { @Override public StudentDto findById(Long id) { Student student = studentRepository.findById(id).orElseThrow( - () -> new EntityNotFoundException("Can't find employee by id" + id) + () -> new EntityNotFoundException("Can't find student by id" + id) ); return studentMapper.toDto(student); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9b8695a..7b6bfec 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/mapstruct?createDatabaseIfNotExist=true -spring.datasource.username= -spring.datasource.password= +spring.datasource.username=root +spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true From c1531a760dcb601a751a74a326c8e12845bdfdf1 Mon Sep 17 00:00:00 2001 From: mgucma Date: Mon, 12 Aug 2024 10:56:11 +0200 Subject: [PATCH 2/2] Spring Boot Mapstruct --- .../mapstruct/config/MapperConfig.java | 11 +++++++ .../academy/mapstruct/mapper/GroupMapper.java | 9 ++++++ .../mapstruct/mapper/StudentMapper.java | 30 +++++++++++++++++++ .../mapstruct/mapper/SubjectMapper.java | 4 +++ .../service/student/StudentServiceImpl.java | 2 +- src/main/resources/application.properties | 4 +-- 6 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/mate/academy/mapstruct/config/MapperConfig.java diff --git a/src/main/java/mate/academy/mapstruct/config/MapperConfig.java b/src/main/java/mate/academy/mapstruct/config/MapperConfig.java new file mode 100644 index 0000000..9e97a43 --- /dev/null +++ b/src/main/java/mate/academy/mapstruct/config/MapperConfig.java @@ -0,0 +1,11 @@ +package mate.academy.mapstruct.config; + +import org.mapstruct.InjectionStrategy; +import org.mapstruct.NullValueCheckStrategy; + +@org.mapstruct.MapperConfig(componentModel = "spring", + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + implementationPackage = "mate.academy.mapstruct.mapper.impl") +public class MapperConfig { +} diff --git a/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java b/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java index d60c9df..493bdb9 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java @@ -1,11 +1,20 @@ package mate.academy.mapstruct.mapper; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.group.CreateGroupRequestDto; import mate.academy.mapstruct.dto.group.GroupDto; import mate.academy.mapstruct.model.Group; +import org.mapstruct.Mapper; +import org.mapstruct.Named; +@Mapper(config = MapperConfig.class) public interface GroupMapper { GroupDto toDto(Group group); Group toModel(CreateGroupRequestDto requestDto); + + @Named("groupById") + default Group groupById(Long id) { + return new Group(id); + } } diff --git a/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java b/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java index 853e6b1..ad2dad8 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java @@ -1,14 +1,44 @@ package mate.academy.mapstruct.mapper; +import java.util.List; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.student.CreateStudentRequestDto; import mate.academy.mapstruct.dto.student.StudentDto; import mate.academy.mapstruct.dto.student.StudentWithoutSubjectsDto; import mate.academy.mapstruct.model.Student; +import mate.academy.mapstruct.model.Subject; +import org.mapstruct.AfterMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; +@Mapper(config = MapperConfig.class, + uses = {GroupMapper.class}) public interface StudentMapper { + @Mapping(source = "group.id", target = "groupId") + @Mapping(target = "subjectIds", ignore = true) StudentDto toDto(Student student); + @AfterMapping + default void setSubjectId(@MappingTarget StudentDto studentDto, Student student) { + List subjectIds = student.getSubjects().stream() + .map(Subject::getId) + .toList(); + studentDto.setSubjectIds(subjectIds); + } + + @Mapping(source = "group.id", target = "groupId") StudentWithoutSubjectsDto toStudentWithoutSubjectsDto(Student student); + @Mapping(source = "groupId", target = "group", qualifiedByName = "groupById") + @Mapping(target = "subjects", ignore = true) Student toModel(CreateStudentRequestDto requestDto); + + @AfterMapping + default void setSubjects(@MappingTarget Student student, CreateStudentRequestDto requestDto) { + List subjects = requestDto.subjects().stream() + .map(Subject::new) + .toList(); + student.setSubjects(subjects); + } } diff --git a/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java b/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java index 2906e78..74a383f 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java @@ -1,11 +1,15 @@ package mate.academy.mapstruct.mapper; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.subject.CreateSubjectRequestDto; import mate.academy.mapstruct.dto.subject.SubjectDto; import mate.academy.mapstruct.model.Subject; +import org.mapstruct.Mapper; +@Mapper(config = MapperConfig.class) public interface SubjectMapper { SubjectDto toDto(Subject subject); Subject toModel(CreateSubjectRequestDto requestDto); + } diff --git a/src/main/java/mate/academy/mapstruct/service/student/StudentServiceImpl.java b/src/main/java/mate/academy/mapstruct/service/student/StudentServiceImpl.java index 420ffcf..d8aa1e4 100644 --- a/src/main/java/mate/academy/mapstruct/service/student/StudentServiceImpl.java +++ b/src/main/java/mate/academy/mapstruct/service/student/StudentServiceImpl.java @@ -35,7 +35,7 @@ public List findAll() { @Override public StudentDto findById(Long id) { Student student = studentRepository.findById(id).orElseThrow( - () -> new EntityNotFoundException("Can't find employee by id" + id) + () -> new EntityNotFoundException("Can't find student by id" + id) ); return studentMapper.toDto(student); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9b8695a..7b6bfec 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/mapstruct?createDatabaseIfNotExist=true -spring.datasource.username= -spring.datasource.password= +spring.datasource.username=root +spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true