[내배캠/TIL(7/29)]Spring lombok을 이용해 Builder 사용

손홍서·2022년 7월 31일
0

Spring

목록 보기
24/24

day69 TIL

스프링에서 빌더 사용하는 방법은 아주 간단하다!
바로 lombok annotation을 사용하면 된다.
@Builder만 생성자에 붙여준다면 손쉽게 만들 수 있다.
바로 아래 코드처럼 말이다.

    @Builder
    public Community(String name, String introduce, String communityImg, Integer headCount, Category category, List<String> days, String time, Type type) {
        this.name = name;
        this.introduce = introduce;
        this.communityImg = communityImg;
        this.headCount = headCount;
        this.category = category;
        this.days = days;
        this.time = time;
        this.type = type;
    }

이는 객체를 생성하는 코드를 더 깔끔하게 보이도록 만든다.

community = Community.builder()
                    .name(reqDto.getName())
                    .introduce(reqDto.getIntroduce())
                    .communityImg(imgUrl)
                    .headCount(reqDto.getHeadCount())
                    .category(reqDto.getCategory())
                    .days(reqDto.getDay())
                    .time(reqDto.getTime())
                    .type(Type.mapPost)
                    .build();
profile
Hello World!!

0개의 댓글