Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.stereotype.Repository;

@Repository
public interface CompletedCoursesDao extends JpaRepository<CompletedCoursesDomain, Long> {
public interface CompletedCoursesDao
extends JpaRepository<CompletedCoursesDomain, Long>, CustomCompletedCoursesDao {

List<CompletedCoursesDomain> findByUserDomain(UserDomain userDomain);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.gimmegonghakauth.dao;

import com.example.gimmegonghakauth.domain.CompletedCoursesDomain;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;

public interface CustomCompletedCoursesDao {

@Transactional
void saveAll(List<CompletedCoursesDomain> completedCourses);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.gimmegonghakauth.dao;

import com.example.gimmegonghakauth.domain.CompletedCoursesDomain;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

@RequiredArgsConstructor
public class CustomCompletedCoursesDaoImpl implements CustomCompletedCoursesDao {

private final JdbcTemplate jdbcTemplate;

@Override
public void saveAll(List<CompletedCoursesDomain> completedCourses) {
String sql = "insert into completed_course (course_id, semester, user_id, year) values (?,?,?,?)";

jdbcTemplate.batchUpdate(
sql,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
CompletedCoursesDomain course = completedCourses.get(i);
ps.setLong(1, course.getCoursesDomain().getCourseId());
ps.setString(2, course.getSemester());
ps.setLong(3, course.getUserDomain().getId());
ps.setInt(4, course.getYear());
}

@Override
public int getBatchSize() {
return completedCourses.size();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import com.example.gimmegonghakauth.domain.CoursesDomain;
import com.example.gimmegonghakauth.domain.UserDomain;
import com.example.gimmegonghakauth.exception.FileException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
Expand All @@ -19,11 +23,8 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CompletedCoursesService {

private final CompletedCoursesDao completedCoursesDao;
Expand Down Expand Up @@ -70,34 +71,34 @@ public List<CompletedCoursesDomain> getExcelList(UserDetails userDetails) {

@Transactional
public void extractData(Sheet worksheet, DataFormatter dataFormatter, UserDomain userDomain) {
List<CompletedCoursesDomain> completedCoursesList = new ArrayList<>(); // 저장할 엔티티 리스트 생성

for (int i = FIRST_ROW; i < worksheet.getPhysicalNumberOfRows(); i++) { //데이터 추출
Row row = worksheet.getRow(i);

String yearAsString = dataFormatter.formatCellValue(row.getCell(1));
int year = Integer.parseInt(yearAsString)%100; //년도
int year = Integer.parseInt(yearAsString) % 100; //년도

String semester = dataFormatter.formatCellValue(row.getCell(2)); //학기

String courseIdAsString = dataFormatter.formatCellValue(row.getCell(3));
Long courseId = courseIdToLong(courseIdAsString); //학수번호

CoursesDomain coursesDomain = coursesDao.findByCourseId(
courseId);// 학수번호를 기반으로 Courses 테이블 검색
CoursesDomain coursesDomain = coursesDao.findByCourseId(courseId);// 학수번호를 기반으로 Courses 테이블 검색
if (coursesDomain == null) {
continue;
}
CompletedCoursesDomain data = CompletedCoursesDomain.builder().userDomain(userDomain)
.coursesDomain(coursesDomain)
.year(year)
.semester(semester)
.build();

completedCoursesDao.save(data);
CompletedCoursesDomain data = CompletedCoursesDomain.builder()
.userDomain(userDomain)
.coursesDomain(coursesDomain)
.year(year)
.semester(semester)
.build();
completedCoursesList.add(data); // 엔티티를 리스트에 추가
}
completedCoursesDao.saveAll(completedCoursesList); // 한 번에 전체 엔티티 저장
}


//업로드 파일 검증
private void validateExcelFile(MultipartFile file, String extension) throws FileException {
if (file.isEmpty()) { // 파일이 비어있으면
Expand Down Expand Up @@ -152,6 +153,4 @@ private Long courseIdToLong(String courseIdAsString) {
}
return Long.parseLong(courseIdAsString);
}


}
2 changes: 1 addition & 1 deletion src/main/resources/submodule-properties
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

운영 환경에서만 batch insert 옵션을 추가해도 괜찮은가요??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 질문의 의도를 잘 이해하지 못했습니다 😢
현재 dev와 prod에 해당 옵션을 추가한 상태인데 그 외에 다른 곳에는 적용하지 않아도되냐는 의미일까요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 질문할때 prod에만 해당 옵션이 추가된 것으로 잘못 보았네요..! test에는 해당 로직이 사용되지 않으니 dev와 prod에만 추가하는게 맞네요 확인했습니다👍