Published 2022. 8. 9. 18:25

@Builder 빌더패턴 사용하지않은 예

TestDto.java


package jpa.board.dto;

import lombok.Data;

/**
 * packageName    : jpa.board.dto
 * fileName       : TestDto
 * author         : 김재성
 * date           : 2022-08-09
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-08-09        김재성       최초 생성
 */

@Data
public class TestDto {

    private Long id;
    private String name;
    private String telNo;
    private int age;

    public TestDto(){

    }

    public TestDto(Long id){
        this.id = id;
    }

    public TestDto(Long id, String name){
        this.id = id;
        this.name = name;
    }

    public TestDto(Long id, String name, String telNo){
        this.id = id;
        this.name = name;
        this.telNo = telNo;
    }

    public TestDto(Long id, String name, String telNo, int age){
        this.id = id;
        this.name = name;
        this.telNo = telNo;
        this.age = age;
    }
}
- 생성자에 값을 넣을때 파라미터 값이 많아지면 코드가 길어지는 문제가 있습니다.

 

Test.java

package jpa.board.dto;

import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.*;

/**
 * packageName    : jpa.board.dto
 * fileName       : TestDtoTest
 * author         : 김재성
 * date           : 2022-08-09
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-08-09        김재성       최초 생성
 */
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class TestDtoTest {

    TestDto testDto = new TestDto(1L);
    TestDto testDto1 = new TestDto(1L, "이름");
    TestDto testDto2 = new TestDto(1L, "이름", "번호");
    TestDto testDto3 = new TestDto(1L, "이름", "번호", 20);


}

 

@Builder 빌더패턴 사용예

package jpa.board.dto;

import lombok.Builder;
import lombok.Data;

/**
 * packageName    : jpa.board.dto
 * fileName       : TestDto
 * author         : 김재성
 * date           : 2022-08-09
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-08-09        김재성       최초 생성
 */

@Data
public class TestDto1 {

    private Long id;
    private String name;
    private String telNo;
    private int age;

    public TestDto1(){

    }

    @Builder
    public TestDto1(Long id, String name, String telNo, int age){
        this.id = id;
        this.name = name;
        this.telNo = telNo;
        this.age = age;
    }
}

 

Test.java

package jpa.board.dto;

import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.*;

/**
 * packageName    : jpa.board.dto
 * fileName       : TestDtoTest
 * author         : 김재성
 * date           : 2022-08-09
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-08-09        김재성       최초 생성
 */
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class TestDtoTest {

    TestDto testDto = new TestDto(1L);
    TestDto testDto1 = new TestDto(1L, "이름");
    TestDto testDto2 = new TestDto(1L, "이름", "번호");
    TestDto testDto3 = new TestDto(1L, "이름", "번호", 20);

    TestDto1 testDtoo = TestDto1.builder()
                        .name("이름")
                        .age(20)
                        .build();

}

 

- 빌더패턴을 사용하면 파라미터 하나하나 생성자를 만들필요가 없습니다. 필요한 항목만 해당 코드처럼 설정을 따로 해줄수 있습니다.

 

@Builder 빌더패턴을 사용해서 dto를 entity로 변경예

DTO

package jpa.board.dto;

import jpa.board.entity.BoardFile;
import jpa.board.entity.Test;
import lombok.Builder;
import lombok.Data;

/**
 * packageName    : jpa.board.dto
 * fileName       : TestDto
 * author         : 김재성
 * date           : 2022-08-09
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-08-09        김재성       최초 생성
 */

@Data
public class TestDto1 {

    private Long id;
    private String name;
    private String telNo;
    private int age;

    public TestDto1(){

    }

    @Builder
    public TestDto1(Long id, String name, String telNo, int age){
        this.id = id;
        this.name = name;
        this.telNo = telNo;
        this.age = age;
    }

    public Test toEntity(){
        return Test.builder()
                .name(name)
                .telNo(telNo)
                .age(age)
                .build();
    }

}

 

 

Entity

package jpa.board.entity;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;

/**
 * packageName    : jpa.board.entity
 * fileName       : Test
 * author         : 김재성
 * date           : 2022-08-09
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-08-09        김재성       최초 생성
 */

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EntityListeners(AuditingEntityListener.class)
public class Test {

    private Long id;
    private String name;
    private String telNo;
    private int age;

    @Builder
    public Test(Long id, String name, String telNo, int age){
        this.id = id;
        this.name = name;
        this.telNo = telNo;
        this.age = age;
    }

}

 

Test

package jpa.board.dto;

import jpa.board.entity.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.*;

/**
 * packageName    : jpa.board.dto
 * fileName       : TestDtoTest
 * author         : 김재성
 * date           : 2022-08-09
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-08-09        김재성       최초 생성
 */
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class TestDtoTest {

    TestDto testDto = new TestDto(1L);
    TestDto testDto1 = new TestDto(1L, "이름");
    TestDto testDto2 = new TestDto(1L, "이름", "번호");
    TestDto testDto3 = new TestDto(1L, "이름", "번호", 20);

    TestDto1 testDtoo = TestDto1.builder()
                        .name("이름")
                        .age(20)
                        .build();

    Test test = testDtoo.toEntity();

}
빌더패턴을 이용해서 dto에 값을 넣고 entity로 바꿀수 있습니다.
복사했습니다!