티스토리 뷰
의존성 추가
implementation 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
Entity와 Dto 구현
public abstract class TodoDto {
@Getter
@AllArgsConstructor
@Builder
public static class CreateRequest{
private String title;
private String description;
private List<String> tags=new ArrayList<>();
}
}
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class Todo{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer todoId;
private String title;
private String description;
private boolean isCompleted;
private LocalDate createdAt;
@ElementCollection
private List<String> tags=new ArrayList<>();
private LocalDate updatedAt;
@Builder
public Todo(String title, String description){
this.title=title;
this.description=description;
}
}
Mapper 인터페이스 구현
@Mapper(componentModel = "spring")
public interface TodoMapper {
@Mapping(target = "title", source = "createRequest.title")
@Mapping(target="description", source="createRequest.description")
@Mapping(target="tags", source="createRequest.tags")
@Mapping(target="createdAt", expression="java(java.time.LocalDate.now())")
@Mapping(target="updatedAt", expression="java(java.time.LocalDate.now())")
Todo toEntity(TodoDto.CreateRequest createRequest);
}
source->target이라고 생각하면 된다.
지금은 dto->entity이므로 source에는 dto의 필드명을, target에는 entity의 필드명을 적으면 된다.
Id는 @GeneratedValue(strategy = GenerationType.IDENTITY)이므로 따로 매핑을 안해줘도 된다.
그리고 bool값인 isCompleted는 매핑을 안해주면 false로 초기값이 설정된다.
'Spring > JPA' 카테고리의 다른 글
[JPA] MultipleBagFetchException와 N+1문제 동시에 해결하기 (1) | 2023.09.24 |
---|---|
[JPA] entity에서 생성시간, 수정시간 반영하는 법 (1) | 2023.01.28 |
[JPA] Querydsl에서 ElementCollection Projection 할 때 에러-org.hibernate.QueryException: not an entity (0) | 2023.01.27 |
[JPA] 연관관계 매핑-양방향 연관관계 (0) | 2022.11.11 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- jpa
- FacadePattern
- idtoken
- 빌더 패턴
- 책임연쇄패턴
- restapi
- 책임체인패턴
- GithubActions
- 메멘토 패턴
- 프로토타입 패턴
- 반복자 패턴
- 양방향연관관계
- 컴포지트패턴
- 구글로그인
- n+1
- UML 필요성
- java문법
- 플라이웨이트패턴
- 상태 패턴
- 퍼싸드패턴
- Iterator Pattern
- 프록시패턴
- CompositePattern
- Flutter
- dfs
- 복합체 패턴
- ArrayDeque
- Chain of Responsibility
- springsecurity
- 브리지 패턴
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
글 보관함