260310_01_ENH: 배치 insert를 JPA 기반에서 JDBC TEMPLATE 방식으로 교체

This commit is contained in:
2026-03-10 15:05:48 +09:00
parent 00dbcec1dc
commit 9ace29400f
11 changed files with 341 additions and 20 deletions

View File

@@ -0,0 +1,144 @@
package com.kri.notihub.server.persistence.jdbc;
import com.kri.notihub.server.persistence.entity.BizMsgEntity;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class BizMsgBulkJdbcWriter {
private final JdbcTemplate jdbcTemplate;
private static final String INSERT_BIZ_MSG_SQL = """
insert into `BIZ_MSG` (
`CMID`,
`UMID`,
`MSG_TYPE`,
`STATUS`,
`CALL_STATUS`,
`REQUEST_TIME`,
`SEND_TIME`,
`REPORT_TIME`,
`DEST_PHONE`,
`DEST_NAME`,
`SEND_PHONE`,
`SEND_NAME`,
`SUBJECT`,
`MSG_BODY`,
`NATION_CODE`,
`SENDER_KEY`,
`TEMPLATE_CODE`,
`RESPONSE_METHOD`,
`TIMEOUT`,
`RE_TYPE`,
`RE_BODY`,
`RE_PART`,
`COVER_FLAG`,
`SMS_FLAG`,
`REPLY_FLAG`,
`RETRY_CNT`,
`ATTACHED_FILE`,
`VXML_FILE`,
`USE_PAGE`,
`USE_TIME`,
`SN_RESULT`,
`TEL_INFO`,
`CINFO`,
`USER_KEY`,
`AD_FLAG`
) values (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?
)
""";
public void bulkInsert(List<BizMsgEntity> entityList) {
if (entityList == null || entityList.isEmpty()) {
return;
}
jdbcTemplate.batchUpdate(INSERT_BIZ_MSG_SQL, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int index) throws SQLException {
BizMsgEntity e = entityList.get(index);
int p = 1;
ps.setString(p++, e.getCmid());
setNullableString(ps, p++, e.getUmid());
setNullableInteger(ps, p++, e.getMsgType());
setNullableInteger(ps, p++, e.getStatus());
setNullableString(ps, p++, e.getCallStatus());
setNullableDateTime(ps, p++, e.getRequestTime());
setNullableDateTime(ps, p++, e.getSendTime());
setNullableDateTime(ps, p++, e.getReportTime());
ps.setString(p++, e.getDestPhone());
setNullableString(ps, p++, e.getDestName());
ps.setString(p++, e.getSendPhone());
setNullableString(ps, p++, e.getSendName());
setNullableString(ps, p++, e.getSubject());
ps.setString(p++, e.getMsgBody());
setNullableString(ps, p++, e.getNationCode());
setNullableString(ps, p++, e.getSenderKey());
setNullableString(ps, p++, e.getTemplateCode());
setNullableString(ps, p++, e.getResponseMethod());
setNullableString(ps, p++, e.getTimeout());
setNullableString(ps, p++, e.getReType());
setNullableString(ps, p++, e.getReBody());
setNullableString(ps, p++, e.getRePart());
setNullableInteger(ps, p++, e.getCoverFlag());
setNullableInteger(ps, p++, e.getSmsFlag());
setNullableInteger(ps, p++, e.getReplyFlag());
setNullableInteger(ps, p++, e.getRetryCnt());
setNullableString(ps, p++, e.getAttachedFile());
setNullableString(ps, p++, e.getVxmlFile());
setNullableInteger(ps, p++, e.getUsePage());
setNullableInteger(ps, p++, e.getUseTime());
setNullableInteger(ps, p++, e.getSnResult());
setNullableString(ps, p++, e.getTelInfo());
setNullableString(ps, p++, e.getCinfo());
setNullableString(ps, p++, e.getUserKey());
setNullableString(ps, p++, e.getAdFalg());
}
@Override
public int getBatchSize() {
return entityList.size();
}
});
}
private void setNullableString(PreparedStatement ps, int index, String value) throws SQLException {
if (value == null) {
ps.setNull(index, Types.VARCHAR);
} else {
ps.setString(index, value);
}
}
private void setNullableInteger(PreparedStatement ps, int index, Integer value) throws SQLException {
if (value == null) {
ps.setNull(index, Types.INTEGER);
} else {
ps.setInt(index, value);
}
}
private void setNullableDateTime(PreparedStatement ps, int index, LocalDateTime value) throws SQLException {
if (value == null) {
ps.setNull(index, Types.TIMESTAMP);
} else {
ps.setTimestamp(index, Timestamp.valueOf(value));
}
}
}

View File

@@ -0,0 +1,145 @@
package com.kri.notihub.server.persistence.jdbc;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kri.notihub.server.persistence.entity.TSendEntity;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@RequiredArgsConstructor
@Component
public class SendBulkJdbcWriter {
private final JdbcTemplate jdbcTemplate;
private final ObjectMapper objectMapper;
private static final String INSERT_T_SEND_SQL = """
insert into t_send (
cmid,
bulk_send_key,
send_ty,
is_resve,
send_corp,
send_status,
result_code,
itrnt_prjct_cd,
reqst_svc,
prjct_cd,
tmplat_idx,
sender_key,
kko_tmplat_cd,
nation_cd,
atch_fl,
send_eml,
send_tel,
send_nm,
send_dt,
sid,
msg_title,
msg_body,
msg_param,
dest_eml,
dest_tel,
dest_nm,
report_dt,
reg_cmpn,
reg_user,
reg_dt
) values (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
""";
public void bulkInsert(List<TSendEntity> entityList) {
if (entityList == null || entityList.isEmpty()) {
return;
}
jdbcTemplate.batchUpdate(INSERT_T_SEND_SQL, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int index) throws SQLException {
TSendEntity e = entityList.get(index);
int p = 1;
ps.setString(p++, e.getCmid());
setNullableString(ps, p++, e.getBulkSendKey());
ps.setShort(p++, e.getSendTy());
ps.setBoolean(p++, e.isResve());
ps.setShort(p++, e.getSendCorp());
ps.setShort(p++, e.getSendStatus());
ps.setShort(p++, e.getResultCode());
ps.setString(p++, e.getItrntPrjctCd());
ps.setString(p++, e.getReqstSvc());
ps.setString(p++, e.getPrjctCd());
setNullableLong(ps, p++, e.getTmplatIdx());
setNullableString(ps, p++, e.getSenderKey());
setNullableString(ps, p++, e.getKkoTmplatCd());
setNullableString(ps, p++, e.getNationCd());
setNullableString(ps, p++, e.getAtchFl());
setNullableString(ps, p++, e.getSendEml());
setNullableString(ps, p++, e.getSendTel());
setNullableString(ps, p++, e.getSendNm());
setNullableDateTime(ps, p++, e.getSendDt());
ps.setString(p++, e.getSid());
setNullableString(ps, p++, e.getMsgTitle());
ps.setString(p++, e.getMsgBody());
ps.setString(p++, toJson(e.getMsgParam()));
setNullableString(ps, p++, e.getDestEml());
setNullableString(ps, p++, e.getDestTel());
setNullableString(ps, p++, e.getDestNm());
setNullableDateTime(ps, p++, e.getReportDt());
ps.setString(p++, e.getRegCmpn());
ps.setString(p++, e.getRegUser());
setNullableDateTime(ps, p++, e.getRegDt());
}
@Override
public int getBatchSize() {
return entityList.size();
}
});
}
private String toJson(Map<String, String> value) throws SQLException {
try {
return objectMapper.writeValueAsString(value != null ? value : Map.of());
} catch (JsonProcessingException e) {
throw new SQLException("msg_param JSON 직렬화 실패", e);
}
}
private void setNullableString(PreparedStatement ps, int index, String value) throws SQLException {
if (value == null) {
ps.setNull(index, Types.VARCHAR);
} else {
ps.setString(index, value);
}
}
private void setNullableLong(PreparedStatement ps, int index, Long value) throws SQLException {
if (value == null) {
ps.setNull(index, Types.BIGINT);
} else {
ps.setLong(index, value);
}
}
private void setNullableDateTime(PreparedStatement ps, int index, LocalDateTime value) throws SQLException {
if (value == null) {
ps.setNull(index, Types.TIMESTAMP);
} else {
ps.setTimestamp(index, Timestamp.valueOf(value));
}
}
}