빈 칸
2024. 6. 20. 18:59
데이터 로더(Data Loader)란?
스프링 부트 시작 시 자동으로 실행
기본 데이터를 DB에 불러오는 데(삽입하는 데) 사용하는 클래스
보통 'commandLineRunner' 인터페이스를 구현
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.example.demo.entity.TouristSpot;
import com.example.demo.repository.TouristSpotRepository;
@Component
public class DataLoader implements CommandLineRunner {
@Autowired
private TouristSpotRepository touristSpotRepository;
@Override
public void run(String... args) throws Exception {
// 기본 데이터 추가
TouristSpot spot1 = new TouristSpot();
spot1.setName("Gyeongbokgung Palace");
spot1.setDescription("Historic palace in Seoul");
touristSpotRepository.save(spot1);
TouristSpot spot2 = new TouristSpot();
spot2.setName("Jeju Island");
spot2.setDescription("Beautiful island in South Korea");
touristSpotRepository.save(spot2);
}
}