SpringFramework/Spring

ModelMapper 사용 법

lovineff 2020. 11. 16. 13:35

의존성 추가(Gradle)

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 ModelMapper modelMapper;

public List<MemberDto> getAllMembers(){
	return memberRepository.findAll()
    		.stream()
        	.map(member -> modelMapper.map(member, MemberDto.class))
        	.collect(Collectors.toList());
}

 

DTO 선언부

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MemberDto {
    private Long id;
    private String name;
    private int age;
}

'SpringFramework > Spring' 카테고리의 다른 글

Enum 객체 활용 방안  (0) 2021.03.09
Redis 연동  (0) 2020.11.17
Swagger2 설정 및 사용  (0) 2020.11.13
class 파일내 DataSource 설정  (0) 2020.11.11
REST API  (0) 2020.06.10