
@Result ์ด๋
ธํ
์ด์
์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋ฐ ๊ฐ์ฒด ๊ฐ์ ๋งคํ์ ์ฝ๊ฒ ํ ์ ์๋๋ก ๋์์ค๋๋ค.
DB์ ์ปฌ๋ผ ๊ฐ์ ์๋ฐ ๊ฐ์ฒด์ ํ๋์ ๋งคํํ ์ ์์ต๋๋ค.
ProductMapper.java
@Select("""
SELECT p.id,
p.title,
p.category,
p.start_price,
p.start_time,
p.end_time,
p.content,
p.status
FROM product p
ORDER BY p.status DESC, p.end_time
""")
@Results(id = "productList", value = {
@Result(property = "id", column = "id"),
@Result(property = "productFileList", column = "id", many = @Many(select = "selectFileByProductId"))
})
List<Product> selectAll();
@Result(property = "id", column = "id"): id ํ๋๋ product ํ
์ด๋ธ์ id ์ปฌ๋ผ๊ณผ ๋งคํ๋ฉ๋๋ค.@Result(property = "productFileList", column = "id", many = @Many(select = "selectFileByProductId")): productFileList ํ๋๋ product_file ํ
์ด๋ธ์์ product_id์ file_name์ ์กฐํํ์ฌ ProductFile ๊ฐ์ฒด์ ๋ฆฌ์คํธ๋ก ๋งคํ๋ฉ๋๋ค. ์ด ๊ฒฝ์ฐ selectFileByProductId ๋ฉ์๋๊ฐ @Many ์ด๋
ธํ
์ด์
๊ณผ ํจ๊ป ์ฌ์ฉ๋์ด ํ์ ์ฟผ๋ฆฌ๋ก ์คํ๋ฉ๋๋ค.property: ์๋ฐ ๊ฐ์ฒด์ ํ๋๋ช
์ ์ง์ ํฉ๋๋ค.column: ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ปฌ๋ผ๋ช
์ ์ง์ ํฉ๋๋ค.many: ๊ด๊ณํ ๋งคํ์์ ์ผ๋๋ค ๊ด๊ณ๋ฅผ ์ ์ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.์ด์ ๊ฐ์ด @Result ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋ฐ ๊ฐ์ฒด ๊ฐ์ ์ ๊ตํ ๋งคํ์ ์ํํ ์ ์์ต๋๋ค. ์ด๋ MyBatis๊ฐ ์ ๊ณตํ๋ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ ์ค ํ๋์
๋๋ค.