From 58ce3e21d52dbc316a23bfcaea87b812810023c4 Mon Sep 17 00:00:00 2001 From: Song-EunJu Date: Thu, 17 Nov 2022 22:43:27 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20csv=20=ED=8C=8C=EC=9D=BC=20DB=EC=97=90?= =?UTF-8?q?=20=ED=91=B8=EC=8B=9C=ED=95=98=EB=8A=94=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?batch=20=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goorm/FileItemReaderJobConfig.java | 189 +++++++++--------- .../com/example/goorm/oreum/CsvReader.java | 20 -- .../com/example/goorm/oreum/CsvWriter.java | 23 --- .../example/goorm/oreum/OreumController.java | 11 +- .../com/example/goorm/oreum/OreumService.java | 59 +----- .../java/com/example/goorm/oreum/Test.java | 39 ---- .../{ => oreum/config}/WebMvcConfig.java | 2 +- .../oreum/{ => domain}/EnumMapperType.java | 2 +- .../goorm/oreum/{ => domain}/MyOreum.java | 2 +- .../goorm/oreum/{ => domain}/Oreum.java | 3 +- .../goorm/oreum/{ => domain}/OreumType.java | 2 +- ...thDayRequest.java => BirthdayRequest.java} | 2 +- .../example/goorm/oreum/dto/CsvReaderDto.java | 12 ++ .../goorm/oreum/dto/OreumResponse.java | 8 +- .../example/goorm/oreum/file/CsvReader.java | 25 +++ .../example/goorm/oreum/file/CsvWriter.java | 22 ++ .../oreum/repository/MyOreumRepository.java | 2 +- .../oreum/repository/OreumRepository.java | 2 +- .../oreum/repository/TestRepository.java | 8 - 19 files changed, 170 insertions(+), 263 deletions(-) delete mode 100644 src/main/java/com/example/goorm/oreum/CsvReader.java delete mode 100644 src/main/java/com/example/goorm/oreum/CsvWriter.java delete mode 100644 src/main/java/com/example/goorm/oreum/Test.java rename src/main/java/com/example/goorm/{ => oreum/config}/WebMvcConfig.java (96%) rename src/main/java/com/example/goorm/oreum/{ => domain}/EnumMapperType.java (82%) rename src/main/java/com/example/goorm/oreum/{ => domain}/MyOreum.java (92%) rename src/main/java/com/example/goorm/oreum/{ => domain}/Oreum.java (92%) rename src/main/java/com/example/goorm/oreum/{ => domain}/OreumType.java (91%) rename src/main/java/com/example/goorm/oreum/dto/{BirthDayRequest.java => BirthdayRequest.java} (90%) create mode 100644 src/main/java/com/example/goorm/oreum/dto/CsvReaderDto.java create mode 100644 src/main/java/com/example/goorm/oreum/file/CsvReader.java create mode 100644 src/main/java/com/example/goorm/oreum/file/CsvWriter.java delete mode 100644 src/main/java/com/example/goorm/oreum/repository/TestRepository.java diff --git a/src/main/java/com/example/goorm/FileItemReaderJobConfig.java b/src/main/java/com/example/goorm/FileItemReaderJobConfig.java index e7c5a39..3c63797 100644 --- a/src/main/java/com/example/goorm/FileItemReaderJobConfig.java +++ b/src/main/java/com/example/goorm/FileItemReaderJobConfig.java @@ -1,96 +1,93 @@ -//package com.example.goorm; -// -//import com.example.goorm.oreum.CsvReader; -//import com.example.goorm.oreum.CsvWriter; -//import com.example.goorm.oreum.Oreum; -//import com.example.goorm.oreum.Test; -//import com.example.goorm.oreum.repository.OreumRepository; -//import com.example.goorm.oreum.repository.TestRepository; -//import lombok.RequiredArgsConstructor; -//import lombok.extern.slf4j.Slf4j; -//import org.springframework.batch.core.Job; -//import org.springframework.batch.core.Step; -//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; -//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; -//import org.springframework.batch.item.ItemProcessor; -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -// -//@Slf4j -//@Configuration -//@RequiredArgsConstructor -//public class FileItemReaderJobConfig { -// private final JobBuilderFactory jobBuilderFactory; -// private final StepBuilderFactory stepBuilderFactory; -// -// private final TestRepository testRepository; -// private final CsvReader csvReader; -// private final CsvWriter csvWriter; -// -// private static final int chunkSize = 366; -// -// private static int monthCheck = 1; -// private static int dayCheck = 1; -// -// // csvFileItemReaderJob 라는 이름의 job 생성 -// @Bean -// public Job csvFileItemReaderJob() { -// return jobBuilderFactory.get("csvFileItemReaderJob") -// .start(csvFileItemReaderStep()) -// .build(); -// } -// -// // csvFileItemReaderStep 이라는 이름의 Step 생성 -// @Bean -// public Step csvFileItemReaderStep() { -// return stepBuilderFactory.get("csvFileItemReaderStep") -// .chunk(chunkSize) -// // Reader 에서 읽어올 타입 - String, Writer에 넘겨줄 타입이 Oreum -// .reader(csvReader.csvFileItemReader()) // 일단 csv에서 String 읽기 -// .processor(csvProcessor()) // String을 가공 -// .writer(csvWriter) -// .build(); -// } -// @Bean -// public ItemProcessor csvProcessor() { -// return oreumStr -> { -// System.out.println(oreumStr); -// String[] lineArr = oreumStr.split(","); -// String name = lineArr[0]; -// String type = lineArr[1].split("/")[1]; -// String pos = lineArr[2]; -// System.out.println(name+" "+type+" "+pos); -// double x = Double.parseDouble(pos.split(" ")[0]); -// double y = Double.parseDouble(pos.split(" ")[1]); -// double z = Double.parseDouble(pos.split(" ")[2]); -// -// Test test = Test.builder() -// .name(name) -// .xPos(x) -// .yPos(y) -// .zPos(z) -// .month(monthCheck) -// .day(dayCheck) -// .build(); -// -// test.toTypeEnum(type); -// testRepository.save(test); -// if((monthCheck == 1 || monthCheck == 3 || monthCheck == 5 || monthCheck == 7 -// || monthCheck == 8 || monthCheck == 10 || monthCheck ==12) && dayCheck == 31) { -// monthCheck++; -// dayCheck = 0; -// } -// else if(monthCheck == 2 && dayCheck == 29){ -// monthCheck++; -// dayCheck = 0; -// } -// else if((monthCheck == 4 || monthCheck == 6 || monthCheck ==9 || monthCheck == 11) && dayCheck == 30){ -// monthCheck++; -// dayCheck = 0; -// } -// dayCheck++; -// return test; -// }; -// } -// -//} +package com.example.goorm; + +import com.example.goorm.oreum.file.CsvReader; +import com.example.goorm.oreum.file.CsvWriter; +import com.example.goorm.oreum.domain.Oreum; +import com.example.goorm.oreum.dto.CsvReaderDto; +import com.example.goorm.oreum.repository.OreumRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.weaver.ast.Test; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Slf4j +@Configuration +@RequiredArgsConstructor +public class FileItemReaderJobConfig { + private final JobBuilderFactory jobBuilderFactory; + private final StepBuilderFactory stepBuilderFactory; + + private final OreumRepository oreumRepository; + private final CsvReader csvReader; + private final CsvWriter csvWriter; + + private static final int chunkSize = 366; + + private static int monthCheck = 1; + private static int dayCheck = 1; + + // csvFileItemReaderJob 라는 이름의 job 생성 + @Bean + public Job csvFileItemReaderJob() { + return jobBuilderFactory.get("csvFileItemReaderJob") + .start(csvFileItemReaderStep()) + .build(); + } + + // csvFileItemReaderStep 이라는 이름의 Step 생성 + @Bean + public Step csvFileItemReaderStep() { + return stepBuilderFactory.get("csvFileItemReaderStep") + .chunk(chunkSize) + // Reader 에서 읽어올 타입 - String, Writer에 넘겨줄 타입이 Oreum + .reader(csvReader.csvFileItemReader()) // 일단 csv에서 String 읽기 + .processor(csvProcessor()) // String을 가공 + .writer(csvWriter) + .build(); + } + @Bean + public ItemProcessor csvProcessor() { + return csvReaderDto -> { + String name = csvReaderDto.getName(); + String type = csvReaderDto.getType().split("/")[1]; + String pos = csvReaderDto.getPos(); + double x = Double.parseDouble(pos.split(" ")[0]); + double y = Double.parseDouble(pos.split(" ")[1]); + double z = Double.parseDouble(pos.split(" ")[2]); + + Oreum oreum = Oreum.builder() + .name(name) + .xPos(x) + .yPos(y) + .zPos(z) + .month(monthCheck) + .day(dayCheck) + .build(); + + oreum.toTypeEnum(type); + oreumRepository.save(oreum); + if((monthCheck == 1 || monthCheck == 3 || monthCheck == 5 || monthCheck == 7 + || monthCheck == 8 || monthCheck == 10 || monthCheck ==12) && dayCheck == 31) { + monthCheck++; + dayCheck = 0; + } + else if(monthCheck == 2 && dayCheck == 29){ + monthCheck++; + dayCheck = 0; + } + else if((monthCheck == 4 || monthCheck == 6 || monthCheck ==9 || monthCheck == 11) && dayCheck == 30){ + monthCheck++; + dayCheck = 0; + } + dayCheck++; + return oreum; + }; + } + +} diff --git a/src/main/java/com/example/goorm/oreum/CsvReader.java b/src/main/java/com/example/goorm/oreum/CsvReader.java deleted file mode 100644 index bf7025e..0000000 --- a/src/main/java/com/example/goorm/oreum/CsvReader.java +++ /dev/null @@ -1,20 +0,0 @@ -//package com.example.goorm.oreum; -// -//import lombok.RequiredArgsConstructor; -//import org.springframework.batch.item.file.FlatFileItemReader; -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -//import org.springframework.core.io.ClassPathResource; -// -//@Configuration -//@RequiredArgsConstructor -//public class CsvReader { -// @Bean -// public FlatFileItemReader csvFileItemReader() { -// /* file read */ -// FlatFileItemReader flatFileItemReader = new FlatFileItemReader<>(); -// flatFileItemReader.setResource(new ClassPathResource("C:\\Users\\Windows10\\Desktop\\오름.txt")); -// flatFileItemReader.setEncoding("UTF-8"); -// return flatFileItemReader; -// } -//} \ No newline at end of file diff --git a/src/main/java/com/example/goorm/oreum/CsvWriter.java b/src/main/java/com/example/goorm/oreum/CsvWriter.java deleted file mode 100644 index cc47548..0000000 --- a/src/main/java/com/example/goorm/oreum/CsvWriter.java +++ /dev/null @@ -1,23 +0,0 @@ -//package com.example.goorm.oreum; -// -//import com.example.goorm.oreum.repository.OreumRepository; -//import com.example.goorm.oreum.repository.TestRepository; -//import lombok.RequiredArgsConstructor; -//import org.springframework.batch.item.ItemWriter; -//import org.springframework.context.annotation.Configuration; -// -//import java.util.ArrayList; -//import java.util.List; -// -//@Configuration -//@RequiredArgsConstructor -//public class CsvWriter implements ItemWriter { -//// private final OreumRepository oreumRepository; -// -// private final TestRepository testRepository; -// -// @Override -// public void write(List list) throws Exception { -// testRepository.saveAll(new ArrayList(list)); -// } -//} \ No newline at end of file diff --git a/src/main/java/com/example/goorm/oreum/OreumController.java b/src/main/java/com/example/goorm/oreum/OreumController.java index c5e9325..6a23c29 100644 --- a/src/main/java/com/example/goorm/oreum/OreumController.java +++ b/src/main/java/com/example/goorm/oreum/OreumController.java @@ -1,6 +1,6 @@ package com.example.goorm.oreum; -import com.example.goorm.oreum.dto.BirthDayRequest; +import com.example.goorm.oreum.dto.BirthdayRequest; import com.example.goorm.oreum.dto.OreumResponse; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -12,18 +12,11 @@ @CrossOrigin(origins = "*") @RequiredArgsConstructor public class OreumController { - private final OreumService oreumService; - @PostMapping("/oreums") - public ResponseEntity pushData(){ - oreumService.readCsv(); - return ResponseEntity.ok().body("DB 푸시 완료"); - } - // 생일에 따른 오름 정보 보여주기 (입력하고 디비에 저장하는 거까지) @PostMapping("/oreum") - public ResponseEntity getOreum(@RequestBody BirthDayRequest request){ + public ResponseEntity getOreum(@RequestBody BirthdayRequest request){ return ResponseEntity.ok().body(oreumService.getOreum(request)); } diff --git a/src/main/java/com/example/goorm/oreum/OreumService.java b/src/main/java/com/example/goorm/oreum/OreumService.java index 3c35794..4b426b4 100644 --- a/src/main/java/com/example/goorm/oreum/OreumService.java +++ b/src/main/java/com/example/goorm/oreum/OreumService.java @@ -1,6 +1,8 @@ package com.example.goorm.oreum; -import com.example.goorm.oreum.dto.BirthDayRequest; +import com.example.goorm.oreum.domain.MyOreum; +import com.example.goorm.oreum.domain.Oreum; +import com.example.goorm.oreum.dto.BirthdayRequest; import com.example.goorm.oreum.dto.OreumResponse; import com.example.goorm.oreum.repository.MyOreumRepository; import com.example.goorm.oreum.repository.OreumRepository; @@ -10,8 +12,6 @@ import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; -import java.io.*; - @Slf4j @Service @RequiredArgsConstructor @@ -19,7 +19,7 @@ public class OreumService { private final OreumRepository oreumRepository; private final MyOreumRepository myOreumRepository; - public OreumResponse getOreum(BirthDayRequest request){ + public OreumResponse getOreum(BirthdayRequest request){ int month = request.getMonth(); int day = request.getDay(); Oreum oreum = oreumRepository.findByBirthday(month, day); // 저장된 오름을 찾아서 @@ -44,55 +44,4 @@ public OreumResponse getMyOreum(Long myOreumId){ log.info("Oreum id: "+oreumInfo.getId()); return OreumResponse.ofOthers(oreumInfo, myOreum); } - - public void readCsv(){ - File csv = new File("C:\\Users\\Windows10\\Desktop\\오름.txt"); - BufferedReader br = null; - String line = ""; - - try { - br = new BufferedReader(new FileReader(csv)); - int monthCheck = 1; - int dayCheck = 1; - - while((line = br.readLine()) != null) { - String[] lineArr = line.split(","); - String name = lineArr[0]; - String type = lineArr[1].split("/")[1]; - String pos = lineArr[2]; - Double x = Double.parseDouble(pos.split(" ")[0]); - Double y = Double.parseDouble(pos.split(" ")[1]); - Double z = Double.parseDouble(pos.split(" ")[2]); - - Oreum oreum = Oreum.builder() - .name(name) - .xPos(x) - .yPos(y) - .zPos(z) - .month(monthCheck) - .day(dayCheck) - .build(); - - oreum.toTypeEnum(type); - oreumRepository.save(oreum); - if((monthCheck == 1 || monthCheck == 3 || monthCheck == 5 || monthCheck == 7 - || monthCheck == 8 || monthCheck == 10 || monthCheck ==12) && dayCheck == 31) { - monthCheck++; - dayCheck = 0; - } - else if(monthCheck == 2 && dayCheck == 29){ - monthCheck++; - dayCheck = 0; - } - else if((monthCheck == 4 || monthCheck == 6 || monthCheck ==9 || monthCheck == 11) && dayCheck == 30){ - monthCheck++; - dayCheck = 0; - } - dayCheck++; - } - - } catch (Exception e) { - throw new RuntimeException(e); - } - } } diff --git a/src/main/java/com/example/goorm/oreum/Test.java b/src/main/java/com/example/goorm/oreum/Test.java deleted file mode 100644 index 6e801f6..0000000 --- a/src/main/java/com/example/goorm/oreum/Test.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.example.goorm.oreum; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import javax.persistence.*; -import java.time.LocalDateTime; -import java.util.Arrays; - -@Entity -@Getter -@NoArgsConstructor -@AllArgsConstructor -@Builder -public class Test { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - private String name; - - @Enumerated(EnumType.STRING) - private OreumType type; - private double xPos; - private double yPos; - private double zPos; - private int month; - private int day; - - // 랜덤 색상값 추가해줘야 함 - public void toTypeEnum(String type) { - this.type = Arrays.stream(OreumType.values()) - .filter(o1 -> o1.getTitle().equals(type)) - .findFirst() - .get(); - } - -} - diff --git a/src/main/java/com/example/goorm/WebMvcConfig.java b/src/main/java/com/example/goorm/oreum/config/WebMvcConfig.java similarity index 96% rename from src/main/java/com/example/goorm/WebMvcConfig.java rename to src/main/java/com/example/goorm/oreum/config/WebMvcConfig.java index 3952abc..a097b34 100644 --- a/src/main/java/com/example/goorm/WebMvcConfig.java +++ b/src/main/java/com/example/goorm/oreum/config/WebMvcConfig.java @@ -1,4 +1,4 @@ -package com.example.goorm; +package com.example.goorm.oreum.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; diff --git a/src/main/java/com/example/goorm/oreum/EnumMapperType.java b/src/main/java/com/example/goorm/oreum/domain/EnumMapperType.java similarity index 82% rename from src/main/java/com/example/goorm/oreum/EnumMapperType.java rename to src/main/java/com/example/goorm/oreum/domain/EnumMapperType.java index 67c7269..4e2fc6e 100644 --- a/src/main/java/com/example/goorm/oreum/EnumMapperType.java +++ b/src/main/java/com/example/goorm/oreum/domain/EnumMapperType.java @@ -1,4 +1,4 @@ -package com.example.goorm.oreum; +package com.example.goorm.oreum.domain; public interface EnumMapperType { // 해당 Enum의 이름을 조회하는 변수 diff --git a/src/main/java/com/example/goorm/oreum/MyOreum.java b/src/main/java/com/example/goorm/oreum/domain/MyOreum.java similarity index 92% rename from src/main/java/com/example/goorm/oreum/MyOreum.java rename to src/main/java/com/example/goorm/oreum/domain/MyOreum.java index 6a95516..7edf755 100644 --- a/src/main/java/com/example/goorm/oreum/MyOreum.java +++ b/src/main/java/com/example/goorm/oreum/domain/MyOreum.java @@ -1,4 +1,4 @@ -package com.example.goorm.oreum; +package com.example.goorm.oreum.domain; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/src/main/java/com/example/goorm/oreum/Oreum.java b/src/main/java/com/example/goorm/oreum/domain/Oreum.java similarity index 92% rename from src/main/java/com/example/goorm/oreum/Oreum.java rename to src/main/java/com/example/goorm/oreum/domain/Oreum.java index 9ff6426..3e15657 100644 --- a/src/main/java/com/example/goorm/oreum/Oreum.java +++ b/src/main/java/com/example/goorm/oreum/domain/Oreum.java @@ -1,4 +1,4 @@ -package com.example.goorm.oreum; +package com.example.goorm.oreum.domain; import lombok.AllArgsConstructor; import lombok.Builder; @@ -6,7 +6,6 @@ import lombok.NoArgsConstructor; import javax.persistence.*; -import java.time.LocalDateTime; import java.util.Arrays; @Entity diff --git a/src/main/java/com/example/goorm/oreum/OreumType.java b/src/main/java/com/example/goorm/oreum/domain/OreumType.java similarity index 91% rename from src/main/java/com/example/goorm/oreum/OreumType.java rename to src/main/java/com/example/goorm/oreum/domain/OreumType.java index 3a9893f..7388d1a 100644 --- a/src/main/java/com/example/goorm/oreum/OreumType.java +++ b/src/main/java/com/example/goorm/oreum/domain/OreumType.java @@ -1,4 +1,4 @@ -package com.example.goorm.oreum; +package com.example.goorm.oreum.domain; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/com/example/goorm/oreum/dto/BirthDayRequest.java b/src/main/java/com/example/goorm/oreum/dto/BirthdayRequest.java similarity index 90% rename from src/main/java/com/example/goorm/oreum/dto/BirthDayRequest.java rename to src/main/java/com/example/goorm/oreum/dto/BirthdayRequest.java index 6317169..c295573 100644 --- a/src/main/java/com/example/goorm/oreum/dto/BirthDayRequest.java +++ b/src/main/java/com/example/goorm/oreum/dto/BirthdayRequest.java @@ -9,7 +9,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class BirthDayRequest { +public class BirthdayRequest { private String nickname; private int month; private int day; diff --git a/src/main/java/com/example/goorm/oreum/dto/CsvReaderDto.java b/src/main/java/com/example/goorm/oreum/dto/CsvReaderDto.java new file mode 100644 index 0000000..0c15206 --- /dev/null +++ b/src/main/java/com/example/goorm/oreum/dto/CsvReaderDto.java @@ -0,0 +1,12 @@ +package com.example.goorm.oreum.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CsvReaderDto { + private String name; + private String type; + private String pos; +} diff --git a/src/main/java/com/example/goorm/oreum/dto/OreumResponse.java b/src/main/java/com/example/goorm/oreum/dto/OreumResponse.java index 2a1da70..f7a6b8e 100644 --- a/src/main/java/com/example/goorm/oreum/dto/OreumResponse.java +++ b/src/main/java/com/example/goorm/oreum/dto/OreumResponse.java @@ -1,8 +1,8 @@ package com.example.goorm.oreum.dto; -import com.example.goorm.oreum.MyOreum; -import com.example.goorm.oreum.Oreum; -import com.example.goorm.oreum.OreumType; +import com.example.goorm.oreum.domain.MyOreum; +import com.example.goorm.oreum.domain.Oreum; +import com.example.goorm.oreum.domain.OreumType; import lombok.Builder; import lombok.Getter; import lombok.Setter; @@ -19,7 +19,7 @@ public class OreumResponse { private double zPos; private Long myOreumId; - public static OreumResponse of(Oreum oreum, MyOreum myOreum, BirthDayRequest request){ + public static OreumResponse of(Oreum oreum, MyOreum myOreum, BirthdayRequest request){ return OreumResponse.builder() .nickname(request.getNickname()) .name(oreum.getName()) diff --git a/src/main/java/com/example/goorm/oreum/file/CsvReader.java b/src/main/java/com/example/goorm/oreum/file/CsvReader.java new file mode 100644 index 0000000..47a9825 --- /dev/null +++ b/src/main/java/com/example/goorm/oreum/file/CsvReader.java @@ -0,0 +1,25 @@ +package com.example.goorm.oreum.file; + +import com.example.goorm.oreum.dto.CsvReaderDto; +import lombok.RequiredArgsConstructor; +import org.springframework.batch.item.file.FlatFileItemReader; +import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.PathResource; + +@Configuration +@RequiredArgsConstructor +public class CsvReader { + @Bean + public FlatFileItemReader csvFileItemReader() { + // FlatFileItemReader - DB가 아닌 Resource에서 데이터를 읽어오도록 구현된 구현체 + return new FlatFileItemReaderBuilder() + .name("CsvReader") + .resource(new PathResource("C:\\Users\\Windows10\\Desktop\\오름.txt")) + .delimited() // 한 라인에서 각각의 컬럼을 어떤 구분자로 구분할 건지 + .names("name", "type", "pos") + .targetType(CsvReaderDto.class) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/goorm/oreum/file/CsvWriter.java b/src/main/java/com/example/goorm/oreum/file/CsvWriter.java new file mode 100644 index 0000000..a526383 --- /dev/null +++ b/src/main/java/com/example/goorm/oreum/file/CsvWriter.java @@ -0,0 +1,22 @@ +package com.example.goorm.oreum.file; + +import com.example.goorm.oreum.domain.Oreum; +import com.example.goorm.oreum.repository.OreumRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.batch.item.ItemWriter; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +@RequiredArgsConstructor +public class CsvWriter implements ItemWriter { + + private final OreumRepository oreumRepository; + + @Override + public void write(List list) throws Exception { + oreumRepository.saveAll(new ArrayList(list)); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/goorm/oreum/repository/MyOreumRepository.java b/src/main/java/com/example/goorm/oreum/repository/MyOreumRepository.java index 49a0a99..1a29fba 100644 --- a/src/main/java/com/example/goorm/oreum/repository/MyOreumRepository.java +++ b/src/main/java/com/example/goorm/oreum/repository/MyOreumRepository.java @@ -1,6 +1,6 @@ package com.example.goorm.oreum.repository; -import com.example.goorm.oreum.MyOreum; +import com.example.goorm.oreum.domain.MyOreum; import org.springframework.data.jpa.repository.JpaRepository; import java.util.Optional; diff --git a/src/main/java/com/example/goorm/oreum/repository/OreumRepository.java b/src/main/java/com/example/goorm/oreum/repository/OreumRepository.java index 8fb1d32..5c4facb 100644 --- a/src/main/java/com/example/goorm/oreum/repository/OreumRepository.java +++ b/src/main/java/com/example/goorm/oreum/repository/OreumRepository.java @@ -1,6 +1,6 @@ package com.example.goorm.oreum.repository; -import com.example.goorm.oreum.Oreum; +import com.example.goorm.oreum.domain.Oreum; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; diff --git a/src/main/java/com/example/goorm/oreum/repository/TestRepository.java b/src/main/java/com/example/goorm/oreum/repository/TestRepository.java deleted file mode 100644 index 4b07003..0000000 --- a/src/main/java/com/example/goorm/oreum/repository/TestRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.goorm.oreum.repository; - -import com.example.goorm.oreum.Oreum; -import com.example.goorm.oreum.Test; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface TestRepository extends JpaRepository { -} \ No newline at end of file