728x90
๋ฐ์ํ
๐ dto <> entity ๋ณํ ์, mapstruct ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
์ธํ :
build.gradle
dependencies {
//MapStruct
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
implementation 'org.mapstruct:mapstruct:1.4.2.Final'
}
mapper
* UserMapper๋ก ๊ตฌํํจ.
package com.org.walk.user;
import com.org.walk.util.EntityMapper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface UserMapper extends EntityMapper<UserDto, UserEntity> {
UserMapper mapper = Mappers.getMapper(UserMapper.class);
}
EntityMapper
package com.org.walk.util;
public interface EntityMapper<D, E> {
E toEntity(final D dto);
D toDto(final E entity);
}
service ๋จ์์ ์ฌ์ฉ ์, ๋ค์๊ณผ ๊ฐ์
package com.org.walk.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserServiceImpl implements UserService {
@Override
public void postUser(UserDto userDto) throws Exception {
UserEntity userEntity = UserMapper.mapper.toEntity(userDto);
userRepository.save(userEntity);
}
}
320x100
๋ฐ์ํ
'JAVA > SPRING' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] Spring / Spring boot ํน์ง ๊ฐ๋จ ์ ๋ฆฌ. (0) | 2023.01.11 |
---|---|
[NAVER maps ๊ธธ ์ฐพ๊ธฐ] springboot + webClient ๋ก API ํธ์ถ (0) | 2022.09.02 |
spring boot / H2 DB ์ธํ (0) | 2022.05.29 |
Hikari CP / Spring boot ์ค์ (0) | 2022.05.23 |
QueryDSL , Criteria , Native Query (0) | 2022.05.09 |