🎅 오너먼트 프로젝트

람다식 BeanPropertyRowMapper로 코드 줄여보기

보배 진 2026. 2. 7. 17:30

 

 

기존 코드 (람다 RowMapper)

// 아이디로 PK 조회 (주문, 장바구니, 주소 등 FK 연결용)
else if ("SELECT_ACCOUNT_PK_BY_ACCOUNT_ID".equals(accountDTO.getCondition())) {
    System.out.println("[로그] AccountRepository의 SELECT_ACCOUNT_PK_BY_ACCOUNT_ID");

    return jdbcTemplate.queryForObject(
        SELECT_ACCOUNT_PK_BY_ACCOUNT_ID,
        (rs, rowNum) -> {
            AccountDTO dto = new AccountDTO();
            dto.setAccountPk(rs.getInt("accountPk"));
            return dto;
        },
        accountDTO.getAccountId()
    );
}

 

 

 

BeanPropertyRowMapper로 줄인 버전

return jdbcTemplate.queryForObject(
    SELECT_ACCOUNT_PK_BY_ACCOUNT_ID,
    new BeanPropertyRowMapper<>(AccountDTO.class),
    accountDTO.getAccountId()
);

 

 

⚠️ 주의할 점 딱 하나

  • 컬럼 AS 이름 = DTO 필드명
  • 대소문자 상관 없음
  • 스네이크 → 카멜 자동 변환도 해줌