Member.java
@Entity
@Data
public class Member {
private String memberId;
private String name;
private int age;
private String role;
}
MemberDto.java
@Data
public class MemeberDto {
private String memberId;
private String name;
private int age;
private String role;
}
MemberDto.java
@Data
public class MemeberDto {
private String memberId;
private String name;
private int age;
private String role;
public MemberDto(Member member){
this.memberId = member.getMemberId();
this.name = member.getName();
this.age = member.getAge();
this.role = member.getRole();
}
}
MemberDto.java
@Data
public class MemeberDto {
private String memberId;
private String name;
private int age;
private String role;
//builder 방식
public Member toEntity1(){
return Member.builder()
.memberId(memberId)
.name(name)
.role(role)
.build();
}
//기본 생성자와 setter이용
public Member toEntity2(){
Member member = new Member();
member.setMemberId(this.memberId);
member.setName(this.name);
member.setAge(this.age);
member.setRole(this.role);
return member;
}
}
MemberService.java
@RequiredArgsConstructor
@Service
public class MemberService {
//생성자 주입
private final MemberRepository memberRepository;
//DTO로 받아서 Entity로 변환 후 저장
@Transactional
public void insertMemeber(MemberDto memberDto){
// 두 가지 방법 존재
memberRepository.save(memberDto.toEntity1());
//memberRepository.save(memberDto.toEntity2());
}
}
maven 이용 시 의존성 추가
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.4.0</version>
</dependency>
gradle 이용 시 의존성 추가
implementation group: 'org.modelmapper', name: 'modelmapper', version: '2.4.0'
ApplicationConfig.java
@Configuration
public class ApplicationConfig {
@Bean
ModelMapper modelMapper() {
return new ModelMapper();
}
}
MemberService.java
@RequiredArgsConstructor
@Service
public class MemberService {
//생성자 주입
private final MemberRepository memberRepository;
private final ModelMapper modelMapper;
//DTO로 받아서 Entity로 변환 후 저장
@Transactional
public void insertMemeber(MemberDto memberDto){
Member member = modelMapper.map(memberDto, Member.class);
memberRepository.save(member);
}
//DB로부터 Entity 객체를 받아 DTO로 변환 후 전달
@Transactional(readOnly=true)
public MemberDto getMember(int memberId){
Member member = memberRepository.findById(memberId).orElseGet(Member::new);
MemberDto memberDto = modelMapper.map(member, MemberDto.class);
return memberDto;
}
//DB로부터 객체 리스트 받아 DTO 리스트로 변환 후 전달
@Transactional(readOnly=true)
public List<MemberDto> getMemberList(){
List<Member> memberList =memberRepository.findAll();
List<MemberDto> memberDtoList = memberList.stream().map(member -> modelMapper.map(member, MemberDto.class)).collect(Collectors.toList());
return memberDtoList;
}
}
maven 이용 시 의존성 추가
<properties>
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.24</org.projectlombok.version>
</properties>
....
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
</plugin>
- Lombok annotation processor가 getter, builder를 만들기 전에 mapStruct annotation processor가 동작하여 매핑할 방법을 찾지 못해 오류가 발생한다고 한다.
- MapStruct에서 제시해주는 lombok과 함께 의존성 추가 예시
gradle 이용 시 의존성 추가
implementation "org.mapstruct:mapstruct:1.4.2.Final"
annotationProcessor "org.mapstruct:mapstruct-processor:1.4.2.Final"
MemberMapper.java
@Mapper
public interface MemberMapper {
MemberMapper INSTANCE = Mappers.getMapper(MemberMapper.class);
//@Mapping(source = "member.필드명", target = "바뀐 필드명")
MemberDto toDto(Member member); //Entity to DTO
Member toEntity(MemberDto memberDto); //DTO to Entity
}
MemberService.java
@RequiredArgsConstructor
@Service
public class MemberService {
//생성자 주입
private final MemberRepository memberRepository;
//Mapper 변수 객체 생성
MemberMapper memberMapper = MemberMapper.INSTANCE;
//DTO로 받아서 Entity로 변환 후 저장
@Transactional
public void insertMemeber(MemberDto memberDto){
Member member = memberMapper.toEntity(memberDto);
memberRepository.save(member);
}
//DB로부터 Entity 객체를 받아 DTO로 변환 후 전달
@Transactional(readOnly=true)
public MemberDto getMember(int memberId){
Member member = memberRepository.findById(memberId).orElseGet(Member::new);
MemberDto memberDto =memberMapper.toDto(member);
return memberDto;
}
//DB로부터 객체 리스트 받아 DTO 리스트로 변환 후 전달
@Transactional(readOnly=true)
public List<MemberDto> getMemberList(){
List<Member> memberList =memberRepository.findAll();
List<MemberDto> memberDtoList = memberList.stream().map(member -> memberMapper.toDto(member)).collect(Collectors.toList());
return memberDtoList;
}
}
Reference: