refactor: Idgen 구현 완료
This commit is contained in:
parent
d45a7a9f60
commit
4f81cb0447
41
src/main/java/com/itn/mjonApi/cmn/context/ContextIdgen.java
Normal file
41
src/main/java/com/itn/mjonApi/cmn/context/ContextIdgen.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package com.itn.mjonApi.cmn.context;
|
||||||
|
|
||||||
|
import com.itn.mjonApi.cmn.idgen.service.impl.IdgenServiceImpl;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* packageName : com.itn.mjonApi.cmn.context
|
||||||
|
* fileName : ContextIdgen
|
||||||
|
* author : hylee
|
||||||
|
* date : 2023-04-24
|
||||||
|
* description :
|
||||||
|
* ===========================================================
|
||||||
|
* DATE AUTHOR NOTE
|
||||||
|
* -----------------------------------------------------------
|
||||||
|
* 2023-04-24 hylee 최초 생성
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* ==사용 방법==
|
||||||
|
* - 사용할 class에 아래(예시)와 같이 추가하여 testApi.getNextStringId(); 와 같이 호출하면 됨
|
||||||
|
* @Resource(name = "testApi")
|
||||||
|
* private IdgenService testApi;
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class ContextIdgen {
|
||||||
|
/*
|
||||||
|
* 예시 testApi
|
||||||
|
* */
|
||||||
|
@Bean(name="testApi")
|
||||||
|
public IdgenServiceImpl testApi(){
|
||||||
|
IdgenServiceImpl idgenServiceImpl = new IdgenServiceImpl();
|
||||||
|
idgenServiceImpl.setCipers(11); // cipers: prefix를 제외한 아이디의 길이 지정
|
||||||
|
idgenServiceImpl.setFillChar('0'); // fillChar: 0을 대신하여 표현되는 문자
|
||||||
|
idgenServiceImpl.setPrefix("TESTAPI_ID_"); // prefix: 아이디의 앞에 고정적으로 붙이고자 하는 설정값 지정
|
||||||
|
idgenServiceImpl.setTableName("TESTAPI"); // tableName - dataSoure 에 설정된 DB에 SEQ 테이블에 tableName 컬럼에 참조할 값
|
||||||
|
return idgenServiceImpl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
package com.itn.mjonApi.cmn.idgen.mapper;
|
package com.itn.mjonApi.cmn.idgen.mapper;
|
||||||
|
|
||||||
import com.itn.mjonApi.cmn.idgen.mapper.domain.IdgenVO;
|
import com.itn.mjonApi.cmn.idgen.mapper.domain.IdgenVO;
|
||||||
|
import org.apache.ibatis.annotations.Insert;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
@ -9,6 +10,9 @@ import java.util.List;
|
|||||||
@Mapper
|
@Mapper
|
||||||
public interface IdgenMapper {
|
public interface IdgenMapper {
|
||||||
|
|
||||||
// @Select("select * from ids")
|
@Select("select * from ids where TABLE_NAME = #{tableName}")
|
||||||
List<IdgenVO> findByTableName(String tableName);
|
IdgenVO findByTableName(String tableName);
|
||||||
|
|
||||||
|
@Insert("INSERT INTO IDS VALUES (#{tableName}, #{nextId}) ON DUPLICATE KEY UPDATE NEXT_ID=#{nextId}")
|
||||||
|
void save(IdgenVO idgenVO);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,16 +15,8 @@ import java.io.Serializable;
|
|||||||
public class IdgenVO implements Serializable {
|
public class IdgenVO implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -7865729705175845268L;
|
private static final long serialVersionUID = -7865729705175845268L;
|
||||||
private Integer msgId; /*auto_increment comment '문자 고유아이디' primary key*/
|
|
||||||
private String mberId; /*comment '회원 아이디'*/
|
private String tableName;
|
||||||
private String esntlId; /*null comment '회원고유 아이디'*/
|
private int nextId;
|
||||||
private String subject; /*null comment '문자 제목'*/
|
|
||||||
private String smsTxt; /*null comment '문자 내용'*/
|
|
||||||
private String smsLen; /*null comment '문자 길이'*/
|
|
||||||
private String atchFileId1; /*null comment '첨부파일번호'*/
|
|
||||||
private String atchFileId2; /*null comment '첨부파일번호'*/
|
|
||||||
private String atchFileId3; /*null comment '첨부파일번호'*/
|
|
||||||
private String regdate; /*null comment '등록일자'*/
|
|
||||||
private String msgType; /*default 'S' null comment '문자종류'*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,16 @@
|
|||||||
|
package com.itn.mjonApi.cmn.idgen.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* packageName : com.itn.mjonApi.cmn.idgen.service
|
||||||
|
* fileName : IdgenService
|
||||||
|
* author : hylee
|
||||||
|
* date : 2023-04-24
|
||||||
|
* description :
|
||||||
|
* ===========================================================
|
||||||
|
* DATE AUTHOR NOTE
|
||||||
|
* -----------------------------------------------------------
|
||||||
|
* 2023-04-24 hylee 최초 생성
|
||||||
|
*/
|
||||||
|
public interface IdgenService {
|
||||||
|
public String getNextStringId();
|
||||||
|
}
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
package com.itn.mjonApi.cmn.idgen.service.impl;
|
||||||
|
|
||||||
|
import com.itn.mjonApi.cmn.idgen.mapper.IdgenMapper;
|
||||||
|
import com.itn.mjonApi.cmn.idgen.mapper.domain.IdgenVO;
|
||||||
|
import com.itn.mjonApi.cmn.idgen.service.IdgenService;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* packageName : com.itn.mjonApi.cmn.context
|
||||||
|
* fileName : ContextIdgen
|
||||||
|
* author : hylee
|
||||||
|
* date : 2023-04-24
|
||||||
|
* description : 전자정부 IDS 구현
|
||||||
|
* ===========================================================
|
||||||
|
* DATE AUTHOR NOTE
|
||||||
|
* -----------------------------------------------------------
|
||||||
|
* 2023-04-24 hylee 최초 생성
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Setter
|
||||||
|
public class IdgenServiceImpl implements IdgenService {
|
||||||
|
|
||||||
|
private String prefix;
|
||||||
|
private int cipers;
|
||||||
|
private char fillChar;
|
||||||
|
private String tableName;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IdgenMapper idgenMapper;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNextStringId() {
|
||||||
|
|
||||||
|
String prefixTemp = prefix;
|
||||||
|
|
||||||
|
// 기존에 있는 tableName인지 확인
|
||||||
|
IdgenVO idgenVO = idgenMapper.findByTableName(tableName);
|
||||||
|
|
||||||
|
// select 한 idgen이 없으면 초기 셋팅
|
||||||
|
if(idgenVO == null){
|
||||||
|
idgenVO = new IdgenVO();
|
||||||
|
idgenVO.setNextId(1);
|
||||||
|
idgenVO.setTableName(tableName);
|
||||||
|
}else{
|
||||||
|
// select 한 idgen이 있으면 +1
|
||||||
|
idgenVO.setNextId(idgenVO.getNextId()+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// nextId를 제외한 String 만들기
|
||||||
|
prefixTemp = this.getMakeNextIdFirstString(prefixTemp, idgenVO);
|
||||||
|
|
||||||
|
// upsert 쿼리 실행
|
||||||
|
idgenMapper.save(idgenVO);
|
||||||
|
|
||||||
|
// nextId 값 만들기
|
||||||
|
String nextId = prefixTemp + String.valueOf(idgenVO.getNextId());
|
||||||
|
|
||||||
|
log.info(" userId : [{}]", nextId);
|
||||||
|
return nextId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param prefixTemp
|
||||||
|
* @param idgenVO
|
||||||
|
* @author hylee
|
||||||
|
* @discription nextId를 제외한 String 만들기
|
||||||
|
* @date 2023-04-24
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getMakeNextIdFirstString(String prefixTemp, IdgenVO idgenVO) {
|
||||||
|
// fillChar 값이 들어갈 자리값 계산
|
||||||
|
int fillCharSize = cipers - (int)(Math.log10(idgenVO.getNextId())+1);
|
||||||
|
// prefix와 NextId를 제외한 자리에 fillchar값 넣기
|
||||||
|
for(int i=0; i<fillCharSize; i++){
|
||||||
|
prefixTemp = prefixTemp +fillChar;
|
||||||
|
}
|
||||||
|
return prefixTemp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package com.itn.mjonApi.mjon.member.service.impl;
|
package com.itn.mjonApi.mjon.member.service.impl;
|
||||||
|
|
||||||
|
import com.itn.mjonApi.cmn.idgen.service.IdgenService;
|
||||||
import com.itn.mjonApi.cmn.msg.RestResponse;
|
import com.itn.mjonApi.cmn.msg.RestResponse;
|
||||||
import com.itn.mjonApi.mjon.member.mapper.MyMsgMapper;
|
import com.itn.mjonApi.mjon.member.mapper.MyMsgMapper;
|
||||||
import com.itn.mjonApi.cmn.idgen.mapper.domain.IdgenVO;
|
import com.itn.mjonApi.cmn.idgen.mapper.domain.IdgenVO;
|
||||||
@ -8,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -17,7 +19,6 @@ public class MemberServiceImpl implements MemberService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
MyMsgMapper myMsgMapper;
|
MyMsgMapper myMsgMapper;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RestResponse findAll() {
|
public RestResponse findAll() {
|
||||||
List<IdgenVO> MyMsgListVO = myMsgMapper.findAll();
|
List<IdgenVO> MyMsgListVO = myMsgMapper.findAll();
|
||||||
|
|||||||
@ -14,3 +14,7 @@ logging.level.jdbc.sqltiming=info
|
|||||||
logging.level.jdbc.audit=off
|
logging.level.jdbc.audit=off
|
||||||
logging.level.jdbc.resultset=off
|
logging.level.jdbc.resultset=off
|
||||||
logging.level.jdbc.resultsettable=off
|
logging.level.jdbc.resultsettable=off
|
||||||
|
|
||||||
|
|
||||||
|
spring.devtools.restart.enabled=true
|
||||||
|
spring.devtools.livereload.enabled=true
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user