MyBatis
JdbcTemplate๊ณผ ๋น๊ตํด์ MyBatis์ ๊ฐ์ฅ ๋งค๋ ฅ์ ์ธ ์ ์
SQL์ XML์ ํธ๋ฆฌํ๊ฒ ์์ฑํ ์ ์๊ณ ๋ ๋์ ์ฟผ๋ฆฌ๋ฅผ ๋งค์ฐ ํธ๋ฆฌํ๊ฒ ์์ฑํ ์ ์๋ค๋ ์ ์ด๋ค.
JdbcTemplate
String sql = "update item " +
"set item_name=:itemName, price=:price, quantity=:quantity " +
"where id=:id";
์ฝ๋ ๋ผ์ธ ๋ณ๊ฒฝ์ ๋์ด์ฐ๊ธฐ๋ฅผ ์กฐ์ฌํด์ผ ํ๋ค.
MyBatis
<update id="update">
update item
set item_name=#{itemName},
price=#{price},
quantity=#{quantity}
where id = #{id}
</update>
MyBatis๋ XML์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ๋ผ์ธ์ด ๊ธธ์ด์ ธ๋ ๋ฌธ์ ๋ํ๊ธฐ์ ๋ํ ๋ถํธํจ์ด ์๋ค.
Mapper Interface
@Mapper
public interface ItemMapper {
void save(Item item);
void update(@Param("id") Long id, @Param("updateParam") ItemUpdateDto updateParam);
Optional<Item> findById(Long id);
List<Item> findAll(ItemSearchCond itemSearch);
}
๋ง์ด๋ฐํฐ์ค ๋งคํ XML์ ํธ์ถํด์ฃผ๋ ๋งคํผ ์ธํฐํ์ด์ค์ด๋ค.
@Mapper ์ ๋ ธํ ์ด์ ์ ๋ถ์ฌ์ฃผ์ด์ผ ํ๋ค. ๊ทธ๋์ผ MyBatis์์ ์ธ์ํ ์ ์๋ค.
์ด ์ธํฐํ์ด์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋ค์์ ๋ณด์ด๋ xml ์ ํด๋น SQL์ ์คํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋๋ ค์ค๋ค.
ItemMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="hello.itemservice.repository.mybatis.ItemMapper">
๊ฐ์ ์์น์ ์คํํ SQL์ด ์๋ XML ๋งคํ ํ์ผ์ ๋ง๋ค์ด์ฃผ๋ฉด ๋๋ค.
์ฐธ๊ณ ๋ก ์๋ฐ ์ฝ๋๊ฐ ์๋๊ธฐ ๋๋ฌธ์ src/main/resources ํ์์ ๋ง๋ค๋, ํจํค์ง ์์น๋ ๋ง์ถ์ด ์ฃผ์ด์ผํ๋ค.
namespace : ์์ ๋ง๋ ๋งคํผ ์ธํฐํ์ด์ค๋ฅผ ์ง์ ํ๋ฉด ๋๋ค.
insert - save
void save(Item item);
<insert id="save" useGeneratedKeys="true" keyProperty="id">
insert into item (item_name, price, quantity)
values (#{itemName}, #{price}, #{quantity})
</insert>
id ์๋ ๋งคํผ ์ธํฐํ์ด์ค์ ์ค์ ํ ๋ฉ์๋ ์ด๋ฆ์ ์ง์
ํ๋ผ๋ฏธํฐ๋ #{} ๋ฌธ๋ฒ์ ์ฌ์ฉ , ๋งคํผ์์ ๋๊ธด ๊ฐ์ฒด์ ํ๋กํผํฐ ์ด๋ฆ
update - update
void update(@Param("id") Long id, @Param("updateParam") ItemUpdateDto
updateParam);
<update id="update">
update item
set item_name=#{updateParam.itemName},
price=#{updateParam.price},
quantity=#{updateParam.quantity}
where id = #{id}
</update>
์ฌ๊ธฐ์๋ ํ๋ผ๋ฏธํฐ๊ฐ Long id , ItemUpdateDto updateParam ์ผ๋ก 2๊ฐ
ํ๋ผ๋ฏธํฐ๊ฐ 1๊ฐ๋ง ์์ผ๋ฉด @Param ์ ์ง์ ํ์ง ์์๋ ๋์ง๋ง,
ํ๋ผ๋ฏธํฐ๊ฐ 2๊ฐ ์ด์์ด๋ฉด @Param ์ผ๋ก ์ด๋ฆ์ ์ง์ ํด์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ตฌ๋ถํด์ผ ํ๋ค.
select - findById
Optional<Item> findById(Long id);
<select id="findById" resultType="Item">
select id, item_name, price, quantity
from item
where id = #{id}
</select>
resultType ์ ๋ฐํ ํ์ ์ ๋ช ์
์์ application.properties ์ ์์ฑ์ ์ง์ ํ ๋๋ถ์ ๋ชจ๋ ํจํค์ง ๋ช ์ ๋ค ์ ์ง๋ ์์๋ ๋๋ค.
๊ทธ๋ ์ง ์์ผ๋ฉด ๋ชจ๋ ํจํค์ง ๋ช ์ ๋ค ์ ์ด์ผ ํ๋ค.
select - findAll
List<Item> findAll(ItemSearchCond itemSearch);
<select id="findAll" resultType="Item">
select id, item_name, price, quantity
from item
<where>
<if test="itemName != null and itemName != ''">
and item_name like concat('%',#{itemName},'%')
</if>
<if test="maxPrice != null">
and price <= #{maxPrice}
</if>
</where>
</select>
<if> ๋ ํด๋น ์กฐ๊ฑด์ด ๋ง์กฑํ๋ฉด ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ค.
<where> ์ ์ ์ ํ๊ฒ where ๋ฌธ์ฅ์ ๋ง๋ค์ด์ค๋ค.
์์ ์์ <if> ๊ฐ ๋ชจ๋ ์คํจํ๊ฒ ๋๋ฉด SQL where ๋ฅผ ๋ง๋ค์ง ์๋๋ค.
์์ ์์ <if> ๊ฐ ํ๋๋ผ๋ ์ฑ๊ณตํ๋ฉด ์ฒ์ ๋ํ๋๋ and ๋ฅผ where ๋ก ๋ณํํด์ค๋ค.
and price <= #{maxPrice}
<=๋ฅผ ์ฌ์ฉํ์ง ์๊ณ <= ๋ฅผ ์ฌ์ฉ
XML์์๋ ๋ฐ์ดํฐ ์์ญ์ < , > ๊ฐ์ ํน์ ๋ฌธ์๋ฅผ ์ฌ์ฉํ ์ ์๊ธฐ ๋๋ฌธ(ํ๊ทธ ์์,์ข ๋ฃ)
XML ํน์๋ฌธ์
< : <
> : >
& : &
XML CDATA
<![CDATA[
and price <= #{maxPrice}
]]>
์ด ๊ตฌ๋ฌธ ์์์๋ ํน์๋ฌธ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋์ ์ด ๊ตฌ๋ฌธ ์์์๋ XML TAG๊ฐ ๋จ์ ๋ฌธ์๋ก ์ธ์๋๊ธฐ ๋๋ฌธ์ <if>, <where> ๋ฑ์ด ์ ์ฉ๋์ง ์๋๋ค.
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ํธ] - 3. JdbcTemplate - ์ด๋ฆ ์ง์ ํ๋ผ๋ฏธํฐ (0) | 2023.04.08 |
[์คํ๋ง DB 2ํธ] - 2. JdbcTemplate - ๋์ ์ฟผ๋ฆฌ ๋ฌธ์ (0) | 2023.04.07 |
[์คํ๋ง DB 2ํธ] - 1. JdbcTemplate (0) | 2023.04.06 |