개발자의 오르막
스프링부트 JPA 엑셀 데이터 파싱하기 본문
# 한국의 도시 목록을 엑셀 데이터로 파싱하기
- en.wikipedia.org/wiki/List_of_cities_in_South_Korea
List of cities in South Korea - Wikipedia
Wikimedia list article Special city Special autonomous city Metropolitan cities Specific cities Administrative cities Cities The largest cities of South Korea have an autonomous status equivalent to that of provinces. Seoul, the lar
en.wikipedia.org
- 먼저 파싱할 데이터를 선택한다.
- docs.google.com/spreadsheets/ 에서 새로운 시트를 만든다.
Google 스프레드시트 - 스프레드시트를 작성하고 수정할 수 있으며 무료입니다.
하나의 계정으로 모든 Google 서비스를 스프레드시트로 이동하려면 로그인하세요.
accounts.google.com
- 복사한 값을 값만 붙여넣기로 복사한다.
- 해당 데이터를 csv 파일로 (쉼표로 구분) 저장한다.
- 해당 파일을 프로젝트에 옳긴다.
@PostConstruct
public void initZoneData() throws IOException {
if (zoneRepository.count() == 0) {
Resource resource = new ClassPathResource("zone_kr.csv");
List<Zone> zoneList = Files.readAllLines(resource.getFile().toPath(), StandardCharsets.UTF_8).stream()
.map(line -> {
String[] split = line.split(",");
return Zone.builder().city(split[0]).localNameOfCity(split[1]).province(split[2]).build();
}).collect(Collectors.toList());
zoneRepository.saveAll(zoneList);
}
}
- 해당 빈을 등록하고, @PostConstruct 를 통해 빈이 시작될 때마다 실행되는 로직을 만들어준다.
이 로직을 통해 데이터가 없으면 zone_kr.csv 파일을 읽어 데이터를 저장해준다.
'SpringFrameWork > SpringBoot' 카테고리의 다른 글
[SpringBoot] 멀티모듈 프로젝트 생성 (0) | 2020.07.12 |
---|---|
[#SpringBoot] CascadeType.Remove 와 orphanRemoval 의 차이 (0) | 2020.05.28 |
[#SpringBoot] Jpa 개념 정리 (0) | 2020.05.16 |
[#SpringBoot ] Lombook 어노테이션 정리 (0) | 2020.05.16 |
[Spring Boot] application.yml 설정 방법 (0) | 2020.04.02 |
Comments