From 151ecbac635e6d74b4b51cfe25108f2b0cb6f41a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=A7=80=ED=98=84?= Date: Mon, 20 Nov 2023 12:07:11 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D/=20=EC=88=98=EC=A0=95=EC=8B=9C=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=EB=A5=BC=20batch=20insert=20=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95=ED=95=A8=20(#528)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 이미지 저장시 batch insert 하도록 수정 * feat: 게시글 수정시에도 이미지 정보를 batch update를 사용하도록 수정함 --- .../PostImageInfoCustomRepository.java | 10 ++++++ .../PostImageInfoCustomRepositoryImpl.java | 36 +++++++++++++++++++ .../repository/PostImageInfoRepository.java | 2 +- .../backend/post/application/PostService.java | 5 ++- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoCustomRepository.java create mode 100644 backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoCustomRepositoryImpl.java diff --git a/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoCustomRepository.java b/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoCustomRepository.java new file mode 100644 index 00000000..9b098633 --- /dev/null +++ b/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoCustomRepository.java @@ -0,0 +1,10 @@ +package edonymyeon.backend.image.postimage.repository; + +import edonymyeon.backend.image.postimage.domain.PostImageInfo; + +import java.util.List; + +public interface PostImageInfoCustomRepository { + + void batchSave(final List imageInfos, final Long postId); +} diff --git a/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoCustomRepositoryImpl.java b/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoCustomRepositoryImpl.java new file mode 100644 index 00000000..d25948c0 --- /dev/null +++ b/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoCustomRepositoryImpl.java @@ -0,0 +1,36 @@ +package edonymyeon.backend.image.postimage.repository; + +import edonymyeon.backend.image.postimage.domain.PostImageInfo; +import lombok.RequiredArgsConstructor; +import org.springframework.jdbc.core.BatchPreparedStatementSetter; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.util.List; + +@RequiredArgsConstructor +public class PostImageInfoCustomRepositoryImpl implements PostImageInfoCustomRepository { + + private final JdbcTemplate jdbcTemplate; + + @Override + public void batchSave(final List imageInfos, final Long postId) { + final LocalDateTime now = LocalDateTime.now(); + jdbcTemplate.batchUpdate("INSERT INTO post_image_info (created_at, post_id, store_name) values (?, ?, ?)", + new BatchPreparedStatementSetter() { + @Override + public void setValues(final PreparedStatement ps, final int i) throws SQLException { + ps.setObject(1, now); + ps.setLong(2, postId); + ps.setString(3, imageInfos.get(i).getStoreName()); + } + + @Override + public int getBatchSize() { + return imageInfos.size(); + } + }); + } +} diff --git a/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoRepository.java b/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoRepository.java index d99ac480..a3857f49 100644 --- a/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoRepository.java +++ b/backend/src/main/java/edonymyeon/backend/image/postimage/repository/PostImageInfoRepository.java @@ -8,7 +8,7 @@ import java.util.List; -public interface PostImageInfoRepository extends JpaRepository { +public interface PostImageInfoRepository extends JpaRepository, PostImageInfoCustomRepository { List findAllByPostId(final Long postId); diff --git a/backend/src/main/java/edonymyeon/backend/post/application/PostService.java b/backend/src/main/java/edonymyeon/backend/post/application/PostService.java index 9a35d90e..dd49380a 100644 --- a/backend/src/main/java/edonymyeon/backend/post/application/PostService.java +++ b/backend/src/main/java/edonymyeon/backend/post/application/PostService.java @@ -62,7 +62,7 @@ public PostIdResponse createPost(final MemberId memberId, final PostRequest post final PostImageInfos postImageInfos = PostImageInfos.of(post, imageService.saveAll(postRequest.newImages(), ImageType.POST)); - postImageInfoRepository.saveAll(postImageInfos.getPostImageInfos()); + postImageInfoRepository.batchSave(postImageInfos.getPostImageInfos(), post.getId()); return new PostIdResponse(post.getId()); } @@ -133,8 +133,7 @@ public PostIdResponse updatePost( final List imageIdsToDelete = post.getImageIdsToDeleteBy(remainedImageNames, imagesToAdd); postImageInfoRepository.deleteAllByIds(imageIdsToDelete); //이때 기존 이미지중 삭제되는 것들은 softDelete - - postImageInfoRepository.saveAll(imagesToAdd.getPostImageInfos()); // //새로 추가된 이미지들을 DB에 저장 + postImageInfoRepository.batchSave(imagesToAdd.getPostImageInfos(), post.getId()); //새로 추가된 이미지들을 DB에 저장 return new PostIdResponse(postId); }