728x90
이전 글에서 엑셀 데이터에 대한 처리는 끝났다. 이후는 개발자가 원하는대로 데이터를 사용하면 된다.
방장은 Database를 update하는 작업을 수행했고 그에 대한 내용을 작성했다.
[SPRING] EXCEL 업로드하여 데이터 처리하기 #1
엑셀 데이터를 파싱하여 Database를 업데이트할 수 있도록 해달라는 요청사항에 따라 개발을 수행하게 되었다. 까먹지 않기 위해 포스팅을 작성해본다. 시나리오 사이트의 장기 미사용자에 대해
many.tistory.com
Service
ExcelUtil
getUpdateMemberList method에서 cell의 값을 가져온 ExcelUtil이다.
cell의 타입을 가져와서 switch-case 문을 통해 맞는 type으로 데이터를 변환하여 반환한다.
★ cell이 숫자일 때, 날짜인 경우 시/분/초 형식으로 가져오게끔 분기하여 작성했다.
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExcelUtil {
public static String getValue(Cell cell) {
String value = "";
if(cell == null) {
return value;
}
switch(cell.getCellType()) {
case FORMULA:
value = cell.getNumericCellValue() + "";
break;
case NUMERIC:
// [★]
if(DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
value = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
// 지수변환 방지
BigDecimal bd = new BigDecimal(cell.getNumericCellValue());
value = bd.toString();
if(value.indexOf('.') > 0){
value = bd.setScale(4, BigDecimal.ROUND_HALF_UP) + "";
}
}
break;
case STRING:
value = cell.getStringCellValue();
break;
case BOOLEAN:
value = cell.getBooleanCellValue() + "";
break;
case BLANK:
value = "";
break;
case ERROR:
value = cell.getErrorCellValue() + "";
break;
default:
value = cell.getStringCellValue();
}
return value;
}
}
updateAccountDatetime
getUpdateMemberList에서 list를 반환받아 updateAccountDatetime으로 전달하여 Database를 업데이트 한다.
id를 통해 계정이 존재하는지 확인 후 존재한다면 전달받은 시간으로 update한다.
public int updateAccountDatetime(ArrayList<String[]> list) {
int count = 0;
for(String[] arr : list) {
String id = arr[0];
String name = arr[1];
String updateDatetime = arr[2] + " " + arr[3];
// id 존재 여부
if(accountRepository.selectAccountIdExistByAccountId(id) > 0) {
count += accountRepository.updateAccountDatetimeByAccountId(id, updateDatetime);
}
}
return count;
}
REPOSITORY
interface와 연결된 mapper를 통해 select, update를 수행하고 update된 개수를 전달한다.
AccountRepository
import com.doomole.stockproject.model.Account;
import org.springframework.stereotype.Repository;
@Repository
public interface AccountRepository {
public int selectAccountIdExistByAccountId(String accountId);
public int updateAccountDatetime(String accountId, String updateDatetime);
}
Account.xml
<mapper>
<!-- 아이디로 사용자 존재여부 확인 -->
<select id="selectAccountIdExistByAccountId" resultType="int">
/* AccountRepository - selectAccountIdExistByAccountId */
SELECT EXISTS (
SELECT
ac.account_idx
FROM
tb_account ac
WHERE
ac.account_id = #{accountId}
) AS selectExist
</select>
<update id="updateAccountDatetime">
/* AccountRepository - updateAccountDatetime */
UPDATE tb_account
SET
updateDatetime = #{updateDatetime}
<where>
account_id = #{accountId}
</where>
</update>
</mapper>
* 질문이나 문의사항, 피드백은 댓글로 남겨주세요.
'프로그래밍 언어 > JAVA, SPRING' 카테고리의 다른 글
SPRING 파헤치기 #1 - SPRING FRAMEWORK (0) | 2023.09.22 |
---|---|
어노테이션 @Valid와 @Validated (3) | 2023.09.11 |
[SPRING] EXCEL 업로드하여 데이터 처리하기 #1 (0) | 2023.08.22 |
[DI] @Autowird vs @RequiredArgsConstructor (4) | 2023.08.16 |
DI(의존성 주입) 란? (4) | 2023.08.16 |