Mybatis學習第20節 -- 嵌套結果
阿新 • • 發佈:2019-03-17
eat main 文件 system Owner sep body 嵌套結果 pan
數據庫檢索日誌
可以很清楚的看到,在執行Shop對象查找的時候, 對於其內部的area對象,並沒有像先前的嵌套查詢一樣, 進行兩次查詢, 而日誌中的第二次查詢時對Shop中的另一個成員Product進行查找的.
執行流程
- 先執行關聯查詢,一次性將所有數據都查詢出來
- 再將所有查詢出來的列組織成嵌套的結果對象
接口
Shop getShopByIdNestedResult(Integer id);
|
映射文件
在這裏的學習, 我自己總結了一個不高明的訣竅, 給左右表起別名後, 對左表字段起別名去除"表名限定", 對右表字段增加"單字符表別名" , 這樣很好去編輯
<resultMap id="nestedResultMap" type="Shop">
<select id="getShopByIdNestedResult" parameterType="int" resultMap="nestedResultMap" >
|
測試
@Test
|
結果
Shop{id=29, ownerId=1, area=Area{id=3, name=‘長治學院‘, priority=2, createTime=null, lastEditTime=null}, categoryId=null, name=‘暴漫奶茶店‘, desc=‘過來喝喝就知道啦,你是我的奶茶‘, addr=‘西苑1號‘, phone=‘1211334565‘, image=‘/upload/images/item/shop/29/2017092601054939287.jpg‘, priority=40, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘, productList=[Product{id=1, name=‘大黃人‘, desc=‘我是大黃人‘, imgAddr=‘upload/images/item/shop/29/2017092601204036435.jpg‘, normalPrice=‘2‘, promotionPrice=‘1‘, priority=100, createTime=Tue Sep 26 09:20:40 CST 2017, lastEditTime=Tue Sep 26 09:20:40 CST 2017, enableStatus=1, CategoryID=3, shopID=29} , Product{id=2, name=‘小黃人‘, desc=‘我是小黃人‘, imgAddr=‘upload/images/item/shop/29/2017092601212211185.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=90, createTime=Tue Sep 26 09:21:22 CST 2017, lastEditTime=Tue Sep 26 09:21:22 CST 2017, enableStatus=1, CategoryID=2, shopID=29} , Product{id=3, name=‘暴漫人‘, desc=‘開心了‘, imgAddr=‘upload/images/item/shop/29/2017092601220059819.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=80, createTime=Tue Sep 26 09:22:00 CST 2017, lastEditTime=Tue Sep 26 09:22:00 CST 2017, enableStatus=1, CategoryID=3, shopID=29} , Product{id=4, name=‘宇宙第一‘, desc=‘宇宙無敵‘, imgAddr=‘upload/images/item/shop/29/2017092601224389939.jpg‘, normalPrice=‘5‘, promotionPrice=‘2‘, priority=70, createTime=Tue Sep 26 09:22:43 CST 2017, lastEditTime=Tue Sep 26 09:22:43 CST 2017, enableStatus=1, CategoryID=3, shopID=29} , Product{id=5, name=‘眼凸凸‘, desc=‘宇宙無敵‘, imgAddr=‘upload/images/item/shop/29/2017092601231570458.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=60, createTime=Tue Sep 26 09:23:15 CST 2017, lastEditTime=Tue Sep 26 09:23:15 CST 2017, enableStatus=1, CategoryID=3, shopID=29} , Product{id=6, name=‘笑瞇瞇‘, desc=‘笑瞇瞇 甜蜜蜜‘, imgAddr=‘upload/images/item/shop/29/2017092601234922140.jpg‘, normalPrice=‘2‘, promotionPrice=‘2‘, priority=50, createTime=Tue Sep 26 09:23:49 CST 2017, lastEditTime=Tue Sep 26 09:23:49 CST 2017, enableStatus=1, CategoryID=3, shopID=29} ]} |
2018-12-29 11:08:23,842 [main] DEBUG [io.github.coinsjack.dao.ShopMapper] - Cache Hit Ratio [io.github.coinsjack.dao.ShopMapper]: 0.0 2018-12-29 11:08:24,213 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopByIdNestedResult] - ==> Preparing: SELECT s.`shop_id` as `shop_id`, s.`owner_id` as `owner_id`, s.`area_id` as `area_id`, s.`shop_category_id` as `shop_category`, s.`shop_name` as `shop_name`, s.`shop_desc` as `shop_desc`, s.`shop_addr` as `shop_addr`, s.`phone` as `phone`, s.`shop_img` as `shop_img`, s.`priority` as `priority`, s.`create_time` as `create_time`, s.`last_edit_time` as `last_edit_time`, s.`enable_status` as `enable_status`, s.`advice` as `advice`, a.`area_id` as `a_area_id`, a.`area_name` as `a_area_name`, a.`priority` as `a_priority`, a.`create_time` as `a_create_time`, a.`last_edit_time` as `a_last_edit_time` FROM `tb_shop` s LEFT JOIN `tb_area` a ON s.`area_id` = a.`area_id` WHERE s.shop_id = ?; 2018-12-29 11:08:24,291 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopByIdNestedResult] - ==> Parameters: 29(Integer) 2018-12-29 11:08:24,363 [main] DEBUG [io.github.coinsjack.dao.ProductMapper] - Cache Hit Ratio [io.github.coinsjack.dao.ProductMapper]: 0.0 2018-12-29 11:08:24,363 [main] DEBUG [io.github.coinsjack.dao.ProductMapper.getProductListByShopID] - ====> Preparing: select * from tb_product WHERE `shop_id` = ?; 2018-12-29 11:08:24,364 [main] DEBUG [io.github.coinsjack.dao.ProductMapper.getProductListByShopID] - ====> Parameters: 29(Integer) 2018-12-29 11:08:24,376 [main] DEBUG [io.github.coinsjack.dao.ProductMapper.getProductListByShopID] - <==== Total: 6 2018-12-29 11:08:24,377 [main] DEBUG [io.github.coinsjack.dao.ShopMapper.getShopByIdNestedResult] - <== Total: 1 |
Mybatis學習第20節 -- 嵌套結果