-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 채팅 저장을 jdbc template을 이용한 bulk insert 변경
- Loading branch information
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...rc/main/java/com/programmers/lime/domains/chat/repository/ChatJdbcTemplateRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.programmers.lime.domains.chat.repository; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.sql.Timestamp; | ||
import java.util.List; | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.programmers.lime.domains.chat.domain.Chat; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ChatJdbcTemplateRepository { | ||
|
||
private static final String INSERT_SQL = "INSERT INTO chats (chat_room_id, member_id, message, send_at, chat_type) VALUES (?, ?, ?, ?, ?)"; | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
@Transactional | ||
public void bulkInsertChats(final List<Chat> chats) { | ||
jdbcTemplate.batchUpdate(INSERT_SQL, chats, 50, this::prepareStatement); | ||
} | ||
|
||
private void prepareStatement(final PreparedStatement ps, final Chat chat) throws SQLException { | ||
ps.setLong(1, chat.getChatRoomId()); | ||
ps.setLong(2, chat.getMemberId()); | ||
ps.setString(3, chat.getMessage()); | ||
ps.setTimestamp(4, Timestamp.valueOf(chat.getSendAt())); | ||
ps.setString(5, chat.getChatType().toString()); | ||
} | ||
} |