码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis学习第20节 -- 嵌套结果

时间:2019-03-17 13:37:55      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:bat   column   session   result   sql   test   get   mybatis   image   

执行流程

  1. 先执行关联查询,一次性将所有数据都查询出来
  2. 再将所有查询出来的列组织成嵌套的结果对象
其实是与嵌套查询的区别在于, 嵌套结果是将每次查询出来的结果集进行组装, 嵌套查询是依照结果集中的某个column比如说id来去进行另一个查询
 

接口

Shop getShopByIdNestedResult(Integer id);

映射文件

在这里的学习, 我自己总结了一个不高明的诀窍, 给左右表起别名后, 对左表字段起别名去除"表名限定", 对右表字段增加"单字符表别名" , 这样很好去编辑
<resultMap id="nestedResultMap" type="Shop">
<id column="shop_id" property="id" ></id>
<result column="owner_id" property="ownerId" ></result>
<result column="shop_category_id" property="categoryId" ></result>
<result column="shop_name" property="name"></result>
<result column="shop_desc" property="desc"></result>
<result column="shop_addr" property="addr"></result>
<result column="phone" property="phone"></result>
<result column="shop_img" property="image"></result>
<result column="priority" property="priority"></result>
<result column="create_time" property="createTime"></result>
<result column="last_edit_time" property="lastEditTime"></result>
<result column="enable_status" property="enableStatus"></result>
<association property="area" column="area_id" javaType="Area">
<id column="a_area_id" property="id" />
<result column="a_area_name" property="name"/>
<result column="a_priority" property="priority"/>
<result column="a_create_time" property="createTime"/>
<result column="a_last_edit_time" property="lastEditTime"/>
</association>
<collection property="productList" column="shop_id"
select="io.github.coinsjack.dao.ProductMapper.getProductListByShopID" javaType="ArrayList" ofType="Product" >
</collection>
</resultMap>
<select id="getShopByIdNestedResult" parameterType="int" resultMap="nestedResultMap" >
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 = #{id};
</select>

测试

@Test
public void testGetShopByIdNestedResult() {
SqlSession session = MyBatisUtil.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
System.out.println(mapper.getShopByIdNestedResult(29));
session.close();
}

结果

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
可以很清楚的看到,在执行Shop对象查找的时候, 对于其内部的area对象,并没有像先前的嵌套查询一样, 进行两次查询, 而日志中的第二次查询时对Shop中的另一个成员Product进行查找的.

Mybatis学习第20节 -- 嵌套结果

标签:bat   column   session   result   sql   test   get   mybatis   image   

原文地址:https://www.cnblogs.com/litran/p/10546246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!