栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用MyBatis 嵌套查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用MyBatis 嵌套查询

MyBatis嵌套查询

​ 在进行查询操作时有时会涉及多个表之间互查,可以根据查询出的某一个结果集里的一些字段作为条件去查询其他表格从而进行多表查询。简化了一些逻辑代码,省去了在service层里进行for循环遍历查询。

业务场景

​ 商城项目的订单列表查询

订单表:goods_order

​ 字段(部分): order_id(主键) , goods_id(商品id),uid (用户id), config_id (商品配置参数id)

商品表:goods_details

​ 字段(部分):goods_id (商品id主键), goods_name(商品名称) , goods_introduction (商品简介)

商品配置表:goods_config

​ 字段:config_id(主键),goods_id(商品id) , goods_config(配置详情)

商品外观: goods_suface

​ 字段:suface_id(主键),goods_id(商品id),suface(外观详情)

商品图片:goods_img

​ 字段:goods_id(商品id) , goods_img(图片地址)

商品价格表:goods_price

​ 字段:price_id(价格) , goods_id (商品id) ,config_id(配置id) ,suface_id(外观id),invetory(库存),goods_price(价格)

在订单模块中查询订单表得出一个订单列表,把订单中**商品价格id(price_id)**传入商品模块,让商品模块去根据订单的id以及price主键去查询商品订单所需要显示的一些信息

订单模块:

List goods_orderList = orderMapper.orderList(uid);

for (Goods_order order:goods_orderList
    ) {
    //这里通过服务地址调用接口
    OrderDetails orderDetails = restTemplate.getForObject(webRequest + "priceTable/" + order.getConfig_id(), OrderDetails.class);
    order.setOrderDetails(orderDetails);
}

商品模块:

​ GoodsMapper.xml



    
      
    
     
     
    
    
    
    
    
    
    
    
    
    
    




    select * from goods_price where goods_id = #{goods_id}
    
        and config_id = #{config_id}
    
    
        and suface_id = #{suface_id}
    


 
    select goods_config from goods_config where config_id = #{config_id}



    select goods_img img from goods_img where goods_id = #{goods_id} limit 1




运行项目测试:

这里实际上还是对数据库进行了多次查询,只是遍历查询的工作交给了myabtis去做,不用在service层写for循环遍历了。有利有弊就看自己怎么选择了!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/888610.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号