ํ๋ผ๋ฏธํฐ ๋ฐ์ธ๋ฉ
String sql = "update item set item_name=?, price=?, quantity=? where id=?";
template.update(sql,
itemName,
price,
quantity,
itemId);
JdbcTemplate์ ๊ธฐ๋ณธ์ผ๋ก ํ๋ผ๋ฏธํฐ๋ฅผ ์์๋๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
์์๋ง ์ ์งํค๋ฉด ๋ฌธ์ ๊ฐ ๋ ๊ฒ์ ์๋ค. ๊ทธ๋ฐ๋ฐ ๋ฌธ์ ๋ ๋ณ๊ฒฝ์์ ์ ๋ฐ์ํ๋ค.
์์๊ฐ ๋ง์ง ์์์ ๋ฒ๊ทธ๊ฐ ๋ฐ์ํ ์๋ ์์ผ๋ฏ๋ก ์ฃผ์ํด์ ์ฌ์ฉํด์ผ ํ๋ค.
์ด๋ฆ ์ง์ ๋ฐ์ธ๋ฉ(NamedParameterJdbcTemplate)
private final NamedParameterJdbcTemplate template;
JdbcTemplate์ ์ด๋ฐ ๋ฌธ์ ๋ฅผ ๋ณด์ํ๊ธฐ ์ํด
NamedParameterJdbcTemplate ๋ผ๋ ์ด๋ฆ์ ์ง์ ํด์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์ธ๋ฉ ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
save()
// ์ด์ ์ฝ๋
String sql = "insert into item(item_name, price, quantity) values(?,?,?)";
//NamedParameterJdbcTemplate ์ฌ์ฉ
String sql = "insert into item(item_name, price, quantity) " +
"values(:itemName, :price, :quantity)";
SQL์์ ๋ค์๊ณผ ๊ฐ์ด ? ๋์ ์ :ํ๋ผ๋ฏธํฐ์ด๋ฆ ์ ๋ฐ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
ํ๋ผ๋ฏธํฐ ์์ฑ/์ ๋ฌ
ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌํ๋ ค๋ฉด Map ์ฒ๋ผ key , value ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค์ด์ ์ ๋ฌํด์ผ ํ๋ค.
์ฌ๊ธฐ์ key ๋ :ํ๋ฆฌ์ดํฐ์ด๋ฆ ์ผ๋ก ์ง์ ํ, ํ๋ผ๋ฏธํฐ์ ์ด๋ฆ์ด๊ณ ,
value ๋ ํด๋น ํ๋ผ๋ฏธํฐ์ ๊ฐ์ด ๋๋ค.
๋ค์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์ด๋ ๊ฒ ๋ง๋ ํ๋ผ๋ฏธํฐ( param )๋ฅผ ์ ๋ฌํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
template.update(sql, param, keyHolder);
์ด๋ฆ ์ง์ ๋ฐ์ธ๋ฉ์์ ์์ฃผ ์ฌ์ฉํ๋ ํ๋ผ๋ฏธํฐ์ ์ข ๋ฅ๋ ํฌ๊ฒ 3๊ฐ์ง๊ฐ ์๋ค.
1. Map
- SqlParameterSource
2. MapSqlParameterSource
3. BeanPropertySqlParameterSource
Map
๋จ์ํ Map ์ ์ฌ์ฉํ๋ค.
Map<String, Object> param = Map.of("id", id);
MapSqlParameterSource
SqlParameterSource param = new MapSqlParameterSource()
.addValue("itemName", updateParam.getItemName())
.addValue("price", updateParam.getPrice())
.addValue("quantity", updateParam.getQuantity())
.addValue("id", itemId);
BeanPropertySqlParameterSource
์๋ฐ๋น ํ๋กํผํฐ ๊ท์ฝ์ ํตํด์ ์๋์ผ๋ก ํ๋ผ๋ฏธํฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
์) ( getXxx() -> xxx, getItemName() -> itemName )
SqlParameterSource param = new BeanPropertySqlParameterSource(item);
BeanPropertyRowMapper
// ์ด์ ์ฝ๋
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;
}; }
//BeanPropertyRowMapper
private RowMapper<Item> itemRowMapper() {
return BeanPropertyRowMapper.newInstance(Item.class); //camel ๋ณํ ์ง์
}
BeanPropertyRowMapper ๋ ResultSet ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์์ ์๋ฐ๋น ๊ท์ฝ์ ๋ง์ถ์ด ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๋ค.
snake_case ๋ camel ๋ณํ์ ์ง์ํ๋ค.
์ปฌ๋ผ ์ด๋ฆ๊ณผ ๊ฐ์ฒด ์ด๋ฆ์ด ์์ ํ ๋ค๋ฅธ ๊ฒฝ์ฐ์๋ ์กฐํ SQL์์ ๋ณ์นญ(as)์ ์ฌ์ฉํ๋ฉด ๋๋ค.
select member_name as username
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-db-2/dashboard
์คํ๋ง DB 2ํธ - ๋ฐ์ดํฐ ์ ๊ทผ ํ์ฉ ๊ธฐ์ - ์ธํ๋ฐ | ๊ฐ์
๋ฐฑ์๋ ๊ฐ๋ฐ์ ํ์ํ DB ๋ฐ์ดํฐ ์ ๊ทผ ๊ธฐ์ ์ ํ์ฉํ๊ณ , ์์ฑํ ์ ์์ต๋๋ค. ์คํ๋ง DB ์ ๊ทผ ๊ธฐ์ ์ ์๋ฆฌ์ ๊ตฌ์กฐ๋ฅผ ์ดํดํ๊ณ , ๋ ๊น์ด์๋ ๋ฐฑ์๋ ๊ฐ๋ฐ์๋ก ์ฑ์ฅํ ์ ์์ต๋๋ค., - ๊ฐ์ ์๊ฐ | ์ธ
www.inflearn.com
'Spring > Spring DB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์คํ๋ง DB 2ํธ] - 5. ๋ฐ์ดํฐ ์ ๊ทผ ๊ธฐ์ - ํ ์คํธ (0) | 2023.04.08 |
---|---|
[์คํ๋ง DB 2ํธ] - 4. JdbcTemplate - SimpleJdbcInsert (0) | 2023.04.08 |
[์คํ๋ง DB 2ํธ] - 2. JdbcTemplate - ๋์ ์ฟผ๋ฆฌ ๋ฌธ์ (0) | 2023.04.07 |
[์คํ๋ง DB 2ํธ] - 1. JdbcTemplate (0) | 2023.04.06 |
[์คํ๋ง DB 2ํธ] - 0. DTO / DAO (0) | 2023.04.05 |