当前位置: 首页 > news >正文

一对多映射处理

8.3.1 、collection

/**
* 根据部门id查新部门以及部门中的员工信息
* @param did
* @return
*/
Dept getDeptEmpByDid(@Param("did") int did);
<resultMap id="deptEmpMap" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<!--
ofType:设置collection标签所处理的集合属性中存储数据的类型
-->
<collection property="emps"ofType="Emp">
<id property="eid" column="eid"></id>
<result property="ename" column="ename"></result>
<result property="age"column="age"></result>
<result property="sex"column="sex"></result>
</collection>
</resultMap>
<!--Dept getDeptEmpByDid(@Param("did") int did);-->
<select id="getDeptEmpByDid" resultMap="deptEmpMap">
select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did =
emp.did where dept.did = #{did}
</select>

实验测试:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
测试类:
在这里插入图片描述
测试结果:
在这里插入图片描述

8.3.2 、分步查询
①查询部门信息

/**
* 分步查询部门和部门中的员工
* @param did
* @return
*/
Dept getDeptByStep(@Param("did") int did);
<resultMap id="deptEmpStep" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<collection property="emps" fetchType="eager"
select="com.softeem.MyBatis.mapper.EmpMapper.getEmpListByDid" column="did">
</collection>
</resultMap>
<!--Dept getDeptByStep(@Param("did") int did);-->
<select id="getDeptByStep" resultMap="deptEmpStep">
select * from t_dept where did = #{did}
</select>

②根据部门id查询部门中的所有员工

/**
* 根据部门id查询员工信息
* @param did
* @return
*/
List<Emp> getEmpListByDid(@Param("did") int did);
<!--List<Emp> getEmpListByDid(@Param("did") int did);-->
<select id="getEmpListByDid" resultType="Emp">
select * from t_emp where did = #{did}
</select>

实验成测试:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
测试结果:
在这里插入图片描述

总结:
分步查询的优点:可以实现延迟加载
但是必须在核心配置文件中设置全局配置信息:
lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属
性会按需加载
此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和
collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType=“lazy(延迟加
载)|eager(立即加载)”

http://www.lryc.cn/news/170331.html

相关文章:

  • 关于IDEA没有显示日志输出?IDEA控制台没有显示Tomcat Localhost Log和Catalina Log 怎么办?
  • 蛇形填数 rust解法
  • 一文探索SD-WAN技术进阶后与MPLS的区别
  • RocketMq(四)消息分类
  • ip地址怎么改网速快
  • 植物大战僵尸各种僵尸攻略(四)
  • main函数中两个参数的作用
  • 华为OD机试 - 连续字母长度 - 字符串(Java 2023 B卷 100分)
  • 想要精通算法和SQL的成长之路 - 填充书架
  • 【ROS入门】ROS的核心概念
  • Python爬虫从端到端抓取网页
  • 这10款类似Stable Diffusion的ai绘图软件,你了解多少?
  • 部署ik分词器
  • 基于STM32+华为云IOT设计的智能垃圾桶
  • 板子接线图
  • Python练习之选择与循环
  • MySQL5.7开启通用日志功能
  • WPF控件模板
  • vue移动端页面适配
  • Ei Scopus 双检索 |第三届信息与通信工程国际会议国际会议(JCICE 2024)
  • ChatGPT实战-Embeddings打造定制化AI智能客服
  • C语言指针,深度长文全面讲解
  • 云桌面打开部署在linux的服务特别卡 怎么解决
  • day5ARM
  • 旋转链表-双指针思想-LeetCode61
  • 使用自定义XML配置文件在.NET桌面程序中保存设置
  • 1787_函数指针的使用
  • 解决nomachine扫描不出ip问题
  • Web 3.0 发展到什么水平了?
  • 大模型:如何利用旧的tokenizer训练出一个新的来?