[JdbcTemplate] RowMapper์ ๋ํด
by rlaehddnd0422jdbctemplate์์ select๋ query()์ queryForObject() ๋ผ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- query() ๋ ์ฟผ๋ฆฌ๋ฌธ ์คํ์ ํตํด ๊ฒฐ๊ณผ row๊ฐ ํ๋์ผ ๊ฒฝ์ฐ ์ฌ์ฉํฉ๋๋ค.
- queryForObject() ๋ ์ฟผ๋ฆฌ๋ฌธ ์คํ์ ํตํด ๊ฒฐ๊ณผ row๊ฐ ํ๋ ์ด์์ผ ๋ ์ฌ์ฉํฉ๋๋ค.
query(), queryForObject() ๋ฉ์๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํ๋ผ๋ฏธํฐ๋ก ์๋ ์ธ ๊ฐ์ง๋ฅผ ๋ฐ์ต๋๋ค.
1. sql : ์ํํ SQL ์ฟผ๋ฆฌ๋ฌธ
2. newRowMapper() : RowMapper<Type> ๊ฐ์ฒด๋ฅผ ๋ฃ์ด์ค๋๋ค.
3. new Object[]{} : SQL ์ฟผ๋ฆฌ๋ฌธ์ ๋ฌผ์ํ ๊ฐ์ ์ธํ ํ ๊ฐ
3๋ฒ์ ๊ฒฝ์ฐ ์ ๋๋ฆญ ํ์ ์ Type๊ณผ ๋ง๋ ์ต๋ช ํด๋์ค ๋ก์ง ๋ฐฉ์์ผ๋ก ์์ฑํด์ค๋๋ค.
RowMapper<T> Interface
@FunctionalInterface
public interface RowMapper<T> {
/**
* Implementations must implement this method to map each row of data
* in the ResultSet. This method should not call {@code next()} on
* the ResultSet; it is only supposed to map values of the current row.
* @param rs the ResultSet to map (pre-initialized for the current row)
* @param rowNum the number of the current row
* @return the result object for the current row (may be {@code null})
* @throws SQLException if an SQLException is encountered getting
* column values (that is, there's no need to catch SQLException)
*/
@Nullable
T mapRow(ResultSet rs, int rowNum) throws SQLException;
}
- mapRow() ๋ฉ์๋๋ SQL ์คํ ๊ฒฐ๊ณผ๋ก ๊ตฌํ ResultSet์์ ํ ํ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ ์๋ฐ ๊ฐ์ฒด๋ก ๋ณํํ๋ ๋งคํผ ๊ธฐ๋ฅ์ ๊ตฌํํฉ๋๋ค.
- RowMapper ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ฅผ ์์ฑํ ์๋ ์์ง๋ง, ์์ ํด๋์ค๋ ๋๋ค์์ผ๋ก RowMapper์ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ query() ๋ฉ์๋์ ์ ๋ฌํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
์ต๋ช ํด๋์ค๋ก ๊ตฌํํ๊ธฐ
@Override
public Optional<Item> findById(Long id)
{
String sql = "select id, item_name, price, quantity from item where id=?";
try
{
Item item = template.queryForObject(sql,
new RowMapper<Item>() {
@Override
public Item mapRow(ResultSet rs, int rowNum) throws SQLException {
Item item = new Item();
item.setId(rs.getLong("id"));
item.setItemName(rs.getString("item_name"));
item.setPrice(rs.getInt("price"));
item.setQuantity(rs.getInt("quantity"));
return item;
}
}, id);
return Optional.of(item);
}catch(EmptyResultDataAccessException e)
{
return Optional.empty();
}
}
- findById() ๋ฉ์๋๋ jdbcTemplate์ query() ๋ฉ์๋๋ฅผ ์ด์ฉํด์ ์ฟผ๋ฆฌ๋ฅผ ์คํํฉ๋๋ค.
- ์ด ์ฟผ๋ฆฌ๋ ์ธ๋ฑ์ค ํ๋ผ๋ฏธํฐ(?)๋ฅผ ํฌํจํ๊ณ ์์ต๋๋ค. ์ธ๋ฑ์ค ํ๋ผ๋ฏธํฐ ๋ฌผ์ํ๊ฐ ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ ๋ง์ฝ ์ด query() ๋ฉ์๋์ ์ธ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ์ธ ๊ฐ๋ณ์ธ์๊ฐ ๊ฐ์ด ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ, ๊ฐ ๊ฐ์ ์ฝค๋ง๋ก ๊ตฌ๋ถํ๋ฉด ๋ฉ๋๋ค. ( id, price, .. )
- mapRow ๋ฉ์๋๋ ResultSet์ผ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ Item ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฆฌํดํ๋๋ก ๊ตฌํํ์ต๋๋ค.
๋๋ค์์ผ๋ก ๊ตฌํ
@Override
public Optional<Item> findById(Long id) {
String sql = "select id, item_name, price, quantity from item where id=?";
try
{
Item item = template.queryForObject(sql, itemRowMapper(), id);
return Optional.of(item);
}catch(EmptyResultDataAccessException e)
{
return Optional.empty();
}
}
private RowMapper<Item> itemRowMapper() {
return ( (rs, rowNum) -> {
Item item = new Item();
item.setId(rs.getLong("id"));
item.setItemName(rs.getString("item_name"));
item.setPrice(rs.getInt("price"));
item.setQuantity(rs.getInt("quantity"));
return item;
});
}
- ๋ง์ฐฌ๊ฐ์ง๋ก Item๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ rs(ResultSet)์ผ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ setter ์ฃผ์ ํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋๋ก ๊ตฌํํ์ต๋๋ค.
queryForObject() Logic
1. sql๊ณผ ๊ฐ๋ณ์ธ์(?)๋ก ๋ฐ์ ๊ฐ์ ํตํด SQL ์ฟผ๋ฆฌ๋ฅผ ์์ฑ ํ ์คํํ์ฌ ์ฟผ๋ฆฌ ์คํ ๊ฒฐ๊ณผ ResultSet์ ์ ์ฅ.
2. itemRowMapper์์ ResultSet์ ํตํด ์์ฑ๋ ๊ฐ์ฒด(Item) ๋ฆฌํด.
(+ query()์ ๊ฒฝ์ฐ์๋ ResultSet์ row๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ List<T> ๋ก ๋ฆฌํด)
<์ฐธ๊ณ ์๋ฃ>
'๐ Backend > DB Access' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TestCode์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์๋ฒ ์ฐ๋ (1) | 2023.04.15 |
---|---|
[JdbcTemplate] JdbcTemplate ์ ์ฉ (0) | 2023.04.12 |
[JdbcTemplate] JDBC Template ์ด๋? (0) | 2023.04.07 |
Spring์ด ์ ๊ณตํ๋ ์์ธ ์ถ์ํ, ์์ธ ๋ณํ๊ธฐ ์ฌ์ฉ (0) | 2023.04.07 |
ErrorCode๋ฅผ ์ด์ฉํ ํน์ ์์ธ ๋ณต๊ตฌ (0) | 2023.04.07 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
Study Repository
rlaehddnd0422