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

MyBatis学习笔记之高级映射及延迟加载

文章目录

  • 环境搭建,数据配置
  • 多对一的映射的思路逻辑
    • 级联属性映射
    • association
    • 分布查询
  • 一对多的映射的思路逻辑
    • collection
    • 分布

环境搭建,数据配置

t_class表
在这里插入图片描述
在这里插入图片描述


t_stu表
在这里插入图片描述

多对一的映射的思路逻辑

多对一:多个学生对应一个班级

多的一方是student,
一的一方是class


怎么分主表和副表
谁在前,谁是主表

多对一和一对多其实都是一样的“叫法”,就是主宾之间的顺序,这里的区分是对于设计需求逻辑的区分

多对一:多在前,那么多就是主表
一对多:一在前,那么一就是主表


现在选取多对一,那么多的一方是学生,那么学生就是主表,班级就是副表

所以映射到JVM虚拟机中的主对象就是学生对象

为什么映射过去的是学生对象,因为学生是主表,映射过去的是主表对象


讨论内存结构

在Student对象中通过sid可以查询到学生的属性,再通过学生对象的cid可以查询到他的班级,那么就应该在Student类中加上private Class class,通过在Student类中加上对Class的引用从而完成对Class的关联
在这里插入图片描述


mybatis实现映射
多种方式,常见的包括三种

  • 第一种方式:一条SQL语句,级联属性映射
  • 第二种方式:一条SQL语句,association
  • 第三种方式:两条SQL语句,分布查询。(这种方式常用:优点一是可复用,优点二十支持懒加载。)

级联属性映射

//StudnetMapper.java
Student selectById(Integer id);
<!-- StudentMapper.xml --><resultMap id="studentResultMap" type="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/><result property="clazz.cid" column="cid"/><result property="clazz.cname" column="cname"/></resultMap><select id="selectById" resultMap="studentResultMap">selects.sid,s.sname,c.cid,c.cnamefromt_stu s left join t_class c on s.cid = c.cidwheresid = #{sid}</select>
//test文件@Testpublic void testSelectById(){SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student1 = mapper.selectById(2);System.out.println(student1);sqlSession.close();}

association

association译为关联,一个Student对象关联一个Class对象

    Student selectByIdByAssociation(Integer id);
    <resultMap id="studentResultMapByAssociation" type="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/><association property="clazz" javaType="Clazz"><id property="cid" column="cid"/><result property="cname" column="cname"/></association></resultMap><select id="selectByIdByAssociation" resultMap="studentResultMapByAssociation">selects.sid,s.sname,c.cid,c.cnamefromt_stu s left join t_class c on s.cid = c.cidwheresid = #{sid}</select>
    @Testpublic void testSelectByIdByAssociation(){SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student = mapper.selectByIdByAssociation(6);System.out.println(student);sqlSession.close();}

SQL语句都是一样的,不同的就是对于class这个“外面来的类”不同的处理方式,如何去访问到其中的数据

分布查询

显而易见的方式,不用动脑子都能想到的方法,先去查student,再利用student获取的cid去查询cname
其中最大的问题就是关于sql语句如何将cid的值赋上
并且如何再从其中拿出来再进行查询


//StudentMapper.java
Student selectByIdStep1(Integer id);
//ClassMapper.java
Clazz selectByIdStep2(Integer cid);
<!--StudentMapper.xml --><resultMap id="studentResultMapByStep" type="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/><association property="clazz"select="org.powernode.mapper.ClassMapper.selectByIdStep2"column="cid"/></resultMap><select id="selectByIdOnlyStudent" resultMap="studentResultMapByStep">selectsid,sname,cidfromt_stuwheresid = #{sid}</select>
<!--ClassMapper.xml --><select id="selectByIdStep2" resultType="Clazz">selectcid,cnamefromt_classwherecid = #{cid}</select>
//test@Testpublic void TestAll2(){SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Student student = mapper.selectByIdOnlyStudent(6);System.out.println(student);sqlSession.close();}

about 优点

  • 第一点:复用性增强,可以重复利用。(大步拆成N多个小碎步,每一个小碎步更加可以重复利用)
  • 第二点:采用这种分布查询,可以充分利用他们的延迟加载/懒加载机制。

什么是延迟加载(懒加载),有什么用?
延迟加载的核心原理就是:用的时候再执行查询语句,不用的时候不查询
作用:提供性能。


在mybatis中如何开启延迟加载?

association标签中添加fetchType=“lazy”
注意:默认情况下是没有开启延迟加载的,需要设置:fetchType=“lazy”
这种在Association标签中配置fetchType=“lazy”的是局部设置,只对当前的Association关联的sql语句起作用
在实际开发中,大部分都是需要使用延迟加载的,所以建议开启全部的延迟加载机制
可以在mybatis-config中配置setting

<settings>	<!-- 延迟加载的全局开关,默认值false不开启 --><!-- 什么意思:所有只要但凡带有分布的,都采用延迟加载 --><setting name="lazyLoadingEnabled" value="true"/>
</settings>

如果在开启全局之后有部分地方在这里插入代码片不想要开启懒加载,可以在associationfetchType中设置为eager

一对多的映射的思路逻辑

一对多,一在前,那么一就是主表,t_class就是主表,那就是在class类中加入对student的引用List<Student> stus

一对多的实现通常包括两种实现方式:

  • 第一种方式:collection
  • 第二种方式:分布查询(老伙计)

collection

//classMapper.javaClazz selectById(Integer id);
//classMapper.xml<resultMap id="clazzResultMap" type="Clazz"><id property="cid" column="cid"/><result property="cname" column="cname"/><collection property="stus" ofType="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/></collection></resultMap><select id="selectById" resultMap="clazzResultMap">selectc.cid,c.cname,s.sid,s.snamefromt_class c left join t_stu s on c.cid = s.cidwherec.cid = #{cid}</select>
//test@Testpublic void testSelectById(){SqlSession sqlSession = SqlSessionUtil.openSession();ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);Clazz clazz = mapper.selectById(1001);System.out.println(clazz);sqlSession.close();}

分布

//ClassMapper.java
Clazz selectByStep(Integer cid);
//StudentMapper.java
List<Student> selectByIdByStep(Integer cid);
<!-- ClassMapper.xml --><resultMap id="clazzResultMapStep" type="Clazz"><id property="cid" column="cid"/><result property="cname" column="cname"/><collection property="stus"select="org.powernode.mapper.StudentMapper.selectByIdByStep"column="cid"/></resultMap><select id="selectByStep" resultMap="clazzResultMapStep">selectcid,cnamefromt_classwherecid = #{cid}</select>
<!-- StudentMapper.xml --><select id="selectByIdByStep" resultType="Student">select*fromt_stuwherecid = #{cid}</select>
//test@Testpublic void testByStep(){SqlSession sqlSession = SqlSessionUtil.openSession();ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);Clazz clazz = mapper.selectByStep(1001);System.out.println(clazz);sqlSession.close();}
http://www.lryc.cn/news/95633.html

相关文章:

  • 小程序如何删除/上架/下架商品
  • Failed to load local font resource:微信小程序加载第三方字体
  • 使用fastjson错误
  • 【GitOps系列】使用Kustomize和Helm定义应用配置
  • Android kotlin高阶函数与Java lambda表达式介绍与实战
  • 自然语言处理实战项目13-基于GRU模型与NER的关键词抽取模型训练全流程
  • 7.26 Qt
  • 【MySQL】库和表的操作
  • (五)RabbitMQ-进阶 死信队列、延迟队列、防丢失机制
  • windows下面的python配置
  • vue3中 状态管理pinia得使用
  • 如何使用 After Effects 导出摄像机跟踪数据到 3ds Max
  • 【iOS】懒加载
  • 《脱离“一支笔、一双手、一道力扣”困境的秘诀》:突破LeetCode难题的五个关键步骤
  • 基于jeecg-boot的任务甘特图显示
  • docker export,import后无法运行,如java命令找不到,运行后容器内编码有问题
  • Web3教程| 什么是地址监控?如何使用地址监控追踪黑客地址?
  • flask结合mysql实现用户的添加和获取
  • 阿里云服务器配置 内存,cpu等等
  • PHP注册、登陆、6套主页-带Thinkphp目录解析-【强撸项目】
  • android Activity设置背景为半透明的时候会显示上一个activity的内容
  • Linux 网络收包流程
  • flex: 0 0 273px的意思
  • helm部署rabbitmq
  • Java版Spring Cloud+Spring Boot+Mybatis+uniapp知识付费平台讲解
  • 编程之舞:流程控制乐章
  • ChatGPT是否能够进行情感融合和语气调整?
  • C++--动态规划路径问题
  • 从实践彻底掌握MySQL的主从复制
  • 机器学习深度学习——线性回归的基本元素