✏️ ddl-auto : create 일경우 사용법

📍 방법 1 - @PostConstruct

package com.huh.BaekJoonSupporter.board.initDB;

import com.huh.BaekJoonSupporter.board.BoardService;
import com.huh.BaekJoonSupporter.member.Member;
import com.huh.BaekJoonSupporter.member.MemberService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequiredArgsConstructor
public class InitDB {

    private final InitService initService;

		//-- running method zone --//
    @PostConstruct
    public void init() {
        initService.dbInit1();
    }

    @Component
    @Transactional
    @RequiredArgsConstructor
    static class InitService {

				//-- DI --//
        private final BoardService boardService;
        private final MemberService memberService;

				//-- init data method zone --//

        public void dbInit1() {
            memberService.create("init 글쓴이");

            for (int i = 1; i < 30; i++) {
                Member member = memberService.getMember("init 글쓴이");
                boardService.create("제목" + i, "내용" + i, member);
            }

        }
    }
}

📍 방법 2 - @Configuration

  • 이 방법이 첫번째 방법보다 더 간단해서 적용해봤지만 이 방식은 아무때나 적용이 불기능하다.
    • 단방향 연관관계의 data 를 저장하거나, 조회하는 로직만 수행 가능하다.
package com.ll.gramgram.base.initData;

import com.ll.gramgram.boundedContext.instaMember.service.InstaMemberService;
import com.ll.gramgram.boundedContext.likeablePerson.service.LikeablePersonService;
import com.ll.gramgram.boundedContext.member.entity.Member;
import com.ll.gramgram.boundedContext.member.service.MemberService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile({"dev", "test"})
public class NotProd {

    @Bean
    CommandLineRunner initData(
            //-- DI zone --//
            MemberService memberService,
            InstaMemberService instaMemberService,
            LikeablePersonService likeablePersonService
    ) {
        return args -> {
            //-- Init data zone --//
            Member memberAdmin = memberService.join("admin", "1234").getData();
            Member memberUser1 = memberService.join("user1", "1234").getData();
            Member memberUser2 = memberService.join("user2", "1234").getData();
            Member memberUser3 = memberService.join("user3", "1234").getData();
            Member memberUser4 = memberService.join("user4", "1234").getData();

            Member memberUser5ByKakao = memberService.whenSocialLogin("KAKAO", "KAKAO__2731659195").getData();

            instaMemberService.connect(memberUser2, "insta_user2", "M");
            instaMemberService.connect(memberUser3, "insta_user3", "W");
            instaMemberService.connect(memberUser4, "insta_user4", "M");

            likeablePersonService.like(memberUser3, "insta_user4", 1);
            likeablePersonService.like(memberUser3, "insta_user100", 2);
        };
    }
}
profile
잘못된 내용 PR 환영

0개의 댓글