728x90
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.5'
ModelMapper 빈 등록
@Configuration
public class AppConfig {
@Bean
public ModelMapper modelMapper(){
return new ModelMapper();
}
}
ModelMapper 자동 매핑
private final BoardRepository boardRepository;
public List<BoardDto> getBoardList(){
return boardRepository.findAll()
.stream()
.map(board->modelMapper.map(board,BoardDto.class))
.collect(Collectors.toList());
}
DTO 선언부
package com.example.boardservice.dto;
import com.example.boardservice.Entity.Board;
import lombok.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class BoardDto {
private Long id;
private String writer;
private String title;
private String content;
private LocalDateTime createdDate;
private LocalDateTime modifiedDate;
public Board toEntity(){
Board build=Board.builder()
.id(id)
.writer(writer)
.title(title)
.content(content)
.build();
return build;
}
}
728x90
'JPA' 카테고리의 다른 글
API 개발 기본 (0) | 2023.01.17 |
---|---|
Mysql 과 JpaData를 활용하여 만든 BoardService (0) | 2023.01.17 |
Entity 올바른 수정은 무엇인가 ? (1) | 2023.01.12 |
JPA 올바른 Entity 생성 (0) | 2023.01.09 |
JPA 영속성 컨텍스트란? (0) | 2023.01.08 |