-
2021-06-02 11:08:09
嵌套查询的弊端:即嵌套查询的N+1问题
尽管嵌套查询大量的简化了存在关联关系的查询,但它的弊端也比较明显:即所谓的N+1问题。关联的嵌套查询显示得到一个结果集,然后根据这个结果集的每一条记录进行关联查询。
现在假设嵌套查询就一个(即resultMap
内部就一个association标签),现查询的结果集返回条数为N,那么关联查询语句将会被执行N次,加上自身返回结果集查询1次,共需要访问数据库N+1次。如果N比较大的话,这样的数据库访问消耗是非常大的!所以使用这种嵌套语句查询的使用者一定要考虑慎重考虑,确保N值不会很大。
嵌套结果查询:
嵌套语句的查询会导致数据库访问次数不定,进而有可能影响到性能。Mybatis还支持一种嵌套结果的查询:即对于一对多,多对多,多对一的情况的查询,Mybatis通过联合查询,将结果从数据库内一次性查出来,然后根据其一对多,多对一,多对多的关系和ResultMap中的配置,进行结果的转换,构建需要的对象。更多相关内容 -
mybatis嵌套查询
2022-04-21 15:19:09类中包含自己 @Data public class SubjectVo { private String id; private int sort; private String title; private List<SubjectVo> children=new ArrayList<>(); } <...类中包含自己
@Data public class SubjectVo { private String id; private int sort; private String title; private List<SubjectVo> children=new ArrayList<>(); } <resultMap id="nectList2" type="com.atguigu.guli.service.edu.entity.vo.SubjectVo"> <result column="id" jdbcType="VARCHAR" property="id" /> <result column="sort" jdbcType="INTEGER" property="sort" /> <result column="title" jdbcType="VARCHAR" property="title" /> <collection property="children" column="id" ofType="com.atguigu.guli.service.edu.entity.vo.SubjectVo" select="selectnectList2"></collection> </resultMap> <select id="selectnectList2" resultMap="nectList2"> select id,sort,title from edu_subject where parent_id=#{id} </select>
@Data public class ChapterVo implements Serializable { private static final long serialVersionUID = 1L; private String id; private String title; private Integer sort; private List<VideoVo> children = new ArrayList<>(); } <resultMap id="nectList2" type="com.atguigu.guli.service.edu.entity.vo.ChapterVo"> <result column="id" jdbcType="VARCHAR" property="id" /> <result column="sort" jdbcType="INTEGER" property="sort" /> <result column="title" jdbcType="VARCHAR" property="title" /> <collection property="children" column="id" ofType="com.atguigu.guli.service.edu.entity.vo.VideoVo" select="selectVideoList"></collection> </resultMap> <select id="getChapterBycourseId" resultMap="nectList2"> select id,title,sort from edu_chapter where course_id=#{courseid} </select>
类中不包含自己
-
【MyBatis嵌套查询】MyBatis使用‘association’标签实现嵌套查询
2022-07-14 11:26:49MyBatis使用‘association’标签实现嵌套查询返回类结构(内部类)
public class testDto{ private Integer id; private String name; private List<Child> childList; public static class Child{ private Integer id; private String name; } }
resultMap编写
<resultMap id="TestDto" type="com.x.y.testDto"> <id property="id" column="id" jdbcType="INTEGER"/> <result property="name" column="name" jdbcType="VARCHAR"/> <association property="childList" column="id" select="queryChildList" javaType="java.util.ArrayList" /> </resultMap>
‘select’为嵌套查询的SQL的ID名称
‘column’为要带入的某列的值<select id="queryTestDto" resultMap="TestDto"> SELECT id,name FROM test </select>
<select id="queryTestDto" resultMap="com.x.y.testDto$Child"> SELECT id,name FROM test WHERE parent_id=#{id} </select>
-
使用MyBatis 嵌套查询
2022-05-15 20:25:47MyBatis嵌套查询 在进行查询操作时有时会涉及多个表之间互查,可以根据查询出的某一个结果集里的一些字段作为条件去查询其他表格从而进行多表查询。简化了一些逻辑代码,省去了在service层里进行for循环遍历查询...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_order> 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
<!-- 定义一个resultmap --> <resultMap id="goodsOrderDetails" type="com.xqj.mishop.utils.OrderDetails"> <!-- 根据price_id 去查询--> <id column="price_id" jdbcType="INTEGER" property="price_id" /> <!-- 查询出来后的字段,cloumn需与表字段对应,jdbcType数据返回类型,property对应pojo类属性 --> <result column="goods_id" jdbcType="INTEGER" property="goods_id" /> <result column="config_id" jdbcType="INTEGER" property="config_id" /> <!-- 同上 --> <result column="suface_id" jdbcType="INTEGER" property="suface_id" /><!-- 同上 --> <result column="invetory" jdbcType="INTEGER" property="invetory" /><!-- 同上 --> <result column="goods_price" jdbcType="DOUBLE" property="goods_price" /><!-- 同上 --> <!-- 根据config_id去查询商品配置详情,association用于返回单结果 collection返回一个集合 --> <association property="goods_config" column="config_id" select="configValue" javaType="STRING"/> <!-- 同上,根据suface_id查询外观详情 --> <association property="surface" column="suface_id" select="sufaceValue" javaType="STRING"/> <!-- 根据goods_id查询图片地址 --> <association property="img" column="goods_id" select="goodsOrderImg" javaType="STRING"/> <!-- 根据goods_id 查询商品名 --> <association property="goods_name" column="goods_id" select="goodsOrderDetailsName" javaType="STRING"/> </resultMap> <!-- resultMap对应着上面写的resultmap的id--> <select id="goodsOrderListDetails" resultMap="goodsOrderDetails"> select * from goods_price where price_id = #{price_id} </select> <!-- 下面查询语句的id都分别对应着上面association里的 select --> <select id="goodsPrice" resultType="Goods_price"> select * from goods_price where goods_id = #{goods_id} <if test="config_id != null"> and config_id = #{config_id} </if> <if test="suface_id != null"> and suface_id = #{suface_id} </if> </select> <select id="goodsOrderListDetails" resultMap="goodsOrderDetails"> select * from goods_price where price_id = #{price_id} </select> <select id="configValue" parameterType="integer" resultType="string"> select goods_config from goods_config where config_id = #{config_id} </select> <select id="sufaceValue" parameterType="integer" resultType="string"> select surface from goods_suface where suface_id = #{suface_id} </select> <select id="goodsOrderImg" parameterType="integer" resultType="string"> select goods_img img from goods_img where goods_id = #{goods_id} limit 1 </select> <select id="goodsOrderDetailsName" parameterType="integer" resultType="string"> select goods_name from goods_details where goods_id = #{goods_id} </select>
运行项目测试:
这里实际上还是对数据库进行了多次查询,只是遍历查询的工作交给了myabtis去做,不用在service层写for循环遍历了。有利有弊就看自己怎么选择了!
-
mybatis 嵌套查询list写法
2021-02-20 08:43:52public class BeanName implements Serializable{ private String id; private String propertyName1; private String propertyName2; private String propertyName3; private String propertyName4;... -
mybatis 嵌套查询子查询column传多个参数描述
2020-08-27 09:49:39mybatis 嵌套查询子查询column传多个参数如下: 1、图解 2、代码示例 备注:注意,相同颜色的单词都是有关联的。 <resultMap id="blogResult" type="Blog"> <association property="author" column="{id=... -
Mybatis嵌套查询 一对多查询
2021-11-22 23:43:53今天在做项目的时候有一个地方需要用到嵌套查询,之前实现过类似的功能,但是在某些地方看到过类似的实现思路,自己试着实现了下发现行得通,于是就有了本文 首先是数据库,在这里我新建了两张表,一张用户表,一张... -
MyBatis嵌套查询和嵌套结果区别以及一对一、一对多、多对多的映射实现
2022-04-30 23:52:45Mybatis、嵌套查询、嵌套结果、一对一、一对多、多对多 -
mybatis嵌套查询与子查询
2021-04-21 11:00:231.按照查询嵌套处理。将多表的数据分别查出来然后用resultMap对应起来 (复杂属性的字段使用association(对应对象)或者collection(对应集合)来映射) <?xml version="1.0" encoding="UTF-8" ?> <!... -
mybatis嵌套查询的使用
2018-12-17 23:09:16当我们遇到表与表之之间存在关联的时候,就可以使用mybatis的嵌套查询 比如说 当一个对象包含了另一个对象 /** * 公交实体类中包含了司机信息和路线信息 */ public class Bus implements Serializable { private ... -
MyBatis嵌套查询解析
2020-08-23 21:01:58MyBatis嵌套查询解析 -
解决mybatis嵌套查询使用PageHelper分页不准确
2021-05-05 17:06:51springboot项目中嵌套查询使用PageHelper发现使用结果不正确,上PageHelper官网看了一下果然是有坑。。 就是下面这种映射会导致分页结果不正确,既然下面这种不行换了一种方式 <?xml version="1.0" encoding=... -
8. MyBatis嵌套查询 - 一对一 - 一对多
2021-04-27 01:05:258. MyBatis嵌套查询 - 一对一 - 一对多前言在上一篇中我们已经基本认识了 MyBatis 的多表查询了。那么 MyBatis 还有一个比较有意思的功能,就是 嵌套查询。这个功... -
MyBatis的嵌套查询解析
2020-08-30 13:55:49本篇文章主要介绍了MyBatis的嵌套查询解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 -
MyBatis = MyBatis嵌套查询和缓存
2020-05-12 20:52:10一.MyBatis 嵌套查询 嵌套查询就是将原来多表查询中的联合查询语句拆开成 多个单表的查询。 在使用mybatis的语法嵌套在一起。 将一次多表联合查询尽量使用多次单表查询来替代( 分步查询 ), 最后将多次查询的结果... -
MyBatis嵌套查询、懒加载实现优化、逆向工程的实现
2021-10-11 21:45:031、嵌套查询 嵌套查询其实算是个知识的扩充点把,就是不用写比较复杂的sql,通过xml文件把他们关联起来, 但是因为嵌套查询需要执行俩次sql语句,因此执行的效率会比执行一次的效率低 1.1 实体类 首先我们先查看... -
mybatis 嵌套查询 分页
2021-03-23 17:22:10 -
11.MyBatis的嵌套查询
2022-02-23 23:02:3911.MyBatis的嵌套查询 1.什么事嵌套查询 嵌套查询就是将原来多表查询中的联合查询语句拆成单个表的查询,再使用mybatis的语法嵌套在一 起。 例子: * 需求:查询一个订单,与此同时查询出该订单所属的用户 1. 联合... -
mybatis 嵌套查询,一对多
2016-01-15 16:20:11最近在做项目时,需要用到mybaits 嵌套查询,什么意思呢,比如个人信息,中包含复杂对象地址,并且是一对多,我想一次查询出来, 这个之前写过,但语法老师忘记,今天写个博客,记录下,好记性不如烂笔头,先简单写... -
mybatis嵌套查询返回树形结构
2018-08-22 11:00:45最近开发中遇到了很多树形结构数据的需要,利用mybatis提供嵌套查询功能,基本上可以完美解决,但是对于其中的原理并不理解,导致在使用的时候像瞎猫碰死耗子一样,照着先前成功的例子copy,后来遇到了莫名奇怪的... -
MyBatis(三) MyBatis复杂嵌套查询
2022-05-01 11:25:08在MyBatis查询过程中,有时会出现多对一、一对一的复杂嵌套查询,比如查询学生及其对应的班级、查询学生及其所在学校、查询评论及其发布用户等。对于这种查询需求,最简单的就是分多次查询,那么如何在一次查询处理... -
Mybatis多表查询嵌套查询
2020-09-07 12:28:47多表嵌套查询 延迟(懒)加载【了解】 内置缓存【了解】 一级缓存 二级缓存 第一章 Mybatis动态SQL1 什么是动态SQL先来看一个需求 把页面输入的id和username封装到User实体中,并作为条件查询数据库 这个... -
mybatis -- 嵌套查询
2022-05-23 22:36:09嵌套查询 -
Mybatis-嵌套查询
2021-03-15 19:32:44嵌套查询就是将原来多表查询中的联合查询语句拆成单个表的查询,再使用mybatis的语法嵌套在一起。 案例实现 -- 需求:查询一个订单,与此同时查询出该订单所属的用户 # 1. 联合查询 select * from orders o left ... -
Mybatis嵌套查询——一对多
2020-04-16 12:32:03mybatis的嵌套查询为两个表之间的关联提供了支持,通过嵌套查询可以轻松达到一种查询A的同时把B也关联查询出来,数据库中也不用创建外键。话不多说,直接上教程。 首先需要确定两个表的关系,我这里的例子是用户表,... -
mybatis嵌套类参数查询
2021-09-29 15:22:00xml: <select id="test" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from ds_award_point t where t.`name`=#{bandPointDO.name} <...public cl -
Mybatis 的嵌套查询与嵌套结果的区别
2021-09-26 19:27:08嵌套查询 嵌套结果 嵌套查询是在查询 SQL 后再进行一个(子)查询 嵌套结果是一个多表查询的 SQL 语句 会执行多条 SQL 语句 只有一条复杂的 SQL 语句(多表连接) SQL语句编写较为简单 SQL语句...