유틸리티 가이드
기본 원칙
- 유틸리티 클래스는 인스턴스 생성 불가 (
private생성자) static메서드만 제공- 부작용(Side Effect) 없는 순수 함수로 작성
DateUtil
날짜/시간 관련 유틸리티.
| 메서드 | 설명 |
|---|---|
printCurrentTime() | 현재 시간을 콘솔 출력 |
getCurrentDateTime("yyyyMMddHHmmss") | 지정 포맷으로 현재 시간 반환 |
// 현재 시간 출력
DateUtil.printCurrentTime();
// 현재 시간 문자열 반환 (yyyyMMddHHmmss)
String now = DateUtil.getCurrentDateTime("yyyyMMddHHmmss");
// 예: "20260516143022"
// 날짜만
String today = DateUtil.getCurrentDateTime("yyyyMMdd");
// 예: "20260516"
MyBatisUtil
MyBatis 쿼리 내 null/빈 문자열 체크 유틸리티.
| 메서드 | 설명 |
|---|---|
isEmpty(str) | null 또는 빈 문자열이면 true |
isNotEmpty(str) | null도 아니고 빈 문자열도 아니면 true |
<!-- MyBatis XML에서 사용 -->
<if test="@com.{회사명}.{프로젝트명}.common.util.MyBatisUtil@isNotEmpty(searchKeyword)">
AND A.USER_NM LIKE '%' || #{searchKeyword} || '%'
</if>
StringUtil
문자열 처리 유틸리티.
| 메서드 | 설명 |
|---|---|
isEmpty(str) | null 또는 빈 문자열이면 true |
isNotEmpty(str) | null도 아니고 빈 문자열도 아니면 true |
// 빈 문자열 체크
if (StringUtil.isEmpty(userId)) {
throw new BusinessException("사용자 ID를 입력하세요.");
}
// 값 존재 체크
if (StringUtil.isNotEmpty(searchKeyword)) {
vo.setSearchKeyword(searchKeyword);
}
ObjectUtil
객체 타입 판별 유틸리티.
| 메서드 | 설명 |
|---|---|
isMap(obj) | Map 타입 여부 |
isList(obj) | List 타입 여부 |
isArray(obj) | Array 타입 여부 |
isBoolean(obj) | Boolean 타입 여부 |
isNumber(obj) | Number 타입 여부 |
isString(obj) | String 타입 여부 |
isInteger(obj) | Integer 타입 여부 |
isLong(obj) | Long 타입 여부 |
isDouble(obj) | Double 타입 여부 |
isVO(obj) | VO(JavaBean) 타입 여부 |
isGetter(method) | Getter 메서드 여부 |
isSetter(method) | Setter 메서드 여부 |
// 타입 확인
Object value = getValueFromSomewhere();
if (ObjectUtil.isList(value)) {
List<?> list = (List<?>) value;
// 목록 처리
} else if (ObjectUtil.isMap(value)) {
Map<?, ?> map = (Map<?, ?>) value;
// 맵 처리
} else if (ObjectUtil.isVO(value)) {
// VO 처리
}
ExcelUtil
Apache POI 기반 엑셀 유틸리티.
| 메서드 | 설명 |
|---|---|
setWidthCell(sheet, col, width) | 셀 너비 설정 |
setHeightRow(row, height) | 행 높이 설정 |
setStringValue(cell, value) | 문자열 값 설정 |
setDateValue(cell, value, format) | 날짜 값 설정 |
setNumberValue(cell, value) | 숫자 값 설정 |
setCurrencyValue(cell, value) | 통화 값 설정 |
setStyleFont(cell, bold, size) | 폰트 스타일 설정 |
setStyleAlignment(cell, hAlign, vAlign) | 정렬 설정 |
setStyleRowCellColor(cell, color) | 배경색 설정 |
mergeRowCell(sheet, row, colStart, colEnd) | 셀 병합 |
setImage(sheet, imgPath, row, col) | 이미지 삽입 |
@Service
@RequiredArgsConstructor
@Slf4j
public class UserExcelService {
@Transactional(readOnly = true)
public byte[] createUserExcel(UserVo vo) {
List<UserVo> list = userMapper.getList(vo);
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("사용자 목록");
// 헤더 행 생성
Row headerRow = sheet.createRow(0);
Cell cell0 = headerRow.createCell(0);
Cell cell1 = headerRow.createCell(1);
Cell cell2 = headerRow.createCell(2);
ExcelUtil.setStringValue(cell0, "사용자 번호");
ExcelUtil.setStringValue(cell1, "사용자명");
ExcelUtil.setStringValue(cell2, "이메일");
ExcelUtil.setStyleFont(cell0, true, 12); // 헤더 굵게
ExcelUtil.setStyleRowCellColor(cell0, IndexedColors.GREY_25_PERCENT);
ExcelUtil.setWidthCell(sheet, 0, 3000);
ExcelUtil.setWidthCell(sheet, 1, 5000);
ExcelUtil.setWidthCell(sheet, 2, 8000);
// 데이터 행 생성
int rowIdx = 1;
for (UserVo user : list) {
Row row = sheet.createRow(rowIdx++);
ExcelUtil.setNumberValue(row.createCell(0), user.getUserSn());
ExcelUtil.setStringValue(row.createCell(1), user.getUserNm());
ExcelUtil.setStringValue(row.createCell(2), user.getUserEmail());
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
return out.toByteArray();
} catch (IOException e) {
log.error("엑셀 생성 실패", e);
throw new RuntimeException("엑셀 파일 생성 중 오류가 발생했습니다.");
}
}
}
체크리스트
- [ ]
DateUtil: 날짜 포맷 필요 시 사용 - [ ]
StringUtil.isEmpty/isNotEmpty: null 체크는 StringUtil 사용 - [ ]
ExcelUtil: 엑셀 생성 시 ExcelUtil 메서드 활용 - [ ] 유틸리티 클래스 직접 인스턴스화 금지