第十三章MyBatis高级映射

news2025/7/10 18:29:21

多对一映射

创建数据表

  • student是主表
  • class_id关联class表的id

class表
在这里插入图片描述
student表
在这里插入图片描述

创建pojo

Class类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Class {

  private Long id;
  private String name;
  private List<Student> students;
}

Student类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

  private Long id;
  private String name;
  private Long classId;
  private Class clazz;
}

映射方式

1. 级联映射

StudentMapper写入如下代码

Student selectByIdResult(Long id);

StudentMapper.xml写入如下代码

<sql id="studentSql">
    s.id,
    s.name,
    s.class_id,
    c.id as classId,
    c.name as className
</sql>

<resultMap id="studentResultResultMap" type="Student">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="classId" column="classId"/>
    <result property="clazz.id" column="classId"/>
    <result property="clazz.name" column="className"/>
</resultMap>

<select id="selectByIdResult" resultMap="studentResultResultMap" >
    select
    <include refid="studentSql"/>
    from student s left join class c on s.class_id = c.id
    where s.id=#{id}
</select>

2. association映射

StudentMapper写入如下代码

Student selectByIdAssociation(Long id);

StudentMapper.xml写入如下代码

<sql id="studentSql">
    s.id,
    s.name,
    s.class_id,
    c.id as classId,
    c.name as className
</sql>
<resultMap id="studentAssociationResultMap" type="Student">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="classId" column="class_id"/>
    <association property="clazz" javaType="Class">
        <id property="id" column="classId"/>
        <result property="name" column="className"/>
    </association>
</resultMap>
<select id="selectByIdAssociation" resultMap="studentAssociationResultMap">
    select 
    <include refid="studentSql"/>
    from student s left join class c on s.class_id=c.id
    where s.id = #{id}
</select>

3. 分步查询

  • 上述两种方法都是一条sql完成
  • 分步查询需要俩条sql,可复用支持延迟加载

StudentMapper.xml写入如下代码

Student selectByIdStep(Long id);

ClassMapper写入如下代码

Class selectById(Long id);

StudentMapper.xml写入如下代码

<sql id="studentSql">
    id,
    name,
    class_id
</sql>
<resultMap id="studentStepResultMap" type="Student">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="classId" column="class_id"/>
    <association property="clazz" select="org.example.mapper.ClassMapper.selectById" column="class_id"/>
</resultMap>
<select id="selectByIdStep" resultMap="studentStepResultMap">
    select 
    <include refid="studentSql"/>
    from student where id = #{id}
</select>

ClassMapper.xml写入如下代码

<sql id="classSql">
    id,
    name
</sql>
<select id="selectById"  resultType="Class">
    select
    <include refid="classSql"/>
    from class
    where id=#{id}
</select>

上述俩种查询每次都要查询2条sql,有时我们不需要查询2条。并且联表查询会出现笛卡尔现象,性能很差

这可以是用延迟加载来提高性能,用到属性的时候查询不用就不会查询

局部设置 fetchType=“lazy” 。只会作用域这条sql

<resultMap id="studentStepResultMap" type="Student">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="classId" column="class_id"/>
    <association property="clazz" fetchType="lazy" select="org.example.mapper.ClassMapper.selectById" column="class_id"/>
</resultMap>

全局开启延迟加载

<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
</settings>

局部关闭使用 fetchType=“eager”

    <resultMap id="studentStepResultMap" type="Student">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="classId" column="class_id"/>
        <association property="clazz" fetchType="eager" select="org.example.mapper.ClassMapper.selectById" column="class_id"/>
    </resultMap>

一对多映射

创建pojo

Class类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Class {

  private Long id;
  private String name;
  private List<Student> students;
}

Student类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

  private Long id;
  private String name;
  private Long classId;
  private Class clazz;
}

映射方式

1. collection映射

ClassMapper写入如下代码

Class selectCollection(Long id);

ClassMapper.xml写入如下代码

<sql id="classSql">
    c.id,
    c.name,
    s.id as studentId,
    s.name as studentName,
    s.class_id as classId
</sql>
<resultMap id="selectCollectionResultMap" type="Class">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <collection property="students" ofType="Student">
        <id property="id" column="studentId"/>
        <result property="name" column="studentName"/>
        <result property="classId" column="classId"/>
    </collection>
</resultMap>
<select id="selectByIdCollection" resultMap="selectCollectionResultMap">
    select
    <include refid="classSql"/>
    from class c left join student s on c.id=s.class_id
    where c.id=#{id}
</select>

2. 分步查询

ClassMapper写入如下代码

Class selectByIdStep(Long id);

StudentMapper写入如下代码

Student selectById(Long classId);

ClassMapper.xml写入如下代码

<sql id="classSql">
    id,
    name
</sql>
<resultMap id="selectStepResultMap" type="Class">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <collection property="students" select="org.example.mapper.StudentMapper.selectById" column="id"/>
</resultMap>
<select id="selectByIdStep" resultMap="selectStepResultMap">
    select
    <include refid="classSql"/>
    from class where id=#{id}
</select>

StudentMapper.xml写入如下代码

<sql id="studentSql">
    id,
    name,
    class_id
</sql>
<select id="selectById" resultType="Student">
    select
    <include refid="studentSql"/>
    from student where class_id = #{classId}
</select>

同理可以设置延迟加载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/903610.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【算法系列篇】滑动窗口

文章目录 前言什么是滑动窗口1.长度最小的子数组1.1 题目要求1.2 做题思路 1.3 Java代码实现2.无重复字符的最长子串2.1 题目要求2.2 做题思路2.3 Java代码实现 3.最大连续1的个数 III3.1 题目要求3.2 做题思路3.3 Java代码实现 4.将x减到0的最小操作数4.1 题目要求4.2 做题思路…

ubuntu上使用osg3.2+osgearth2.9

一、介绍 在ubuntu上使用osgearth加载三维数字地球&#xff0c;首先要有osg和osgearth的库&#xff0c;这些可以直接使用apt-get下载安装&#xff0c;但是版本有些老&#xff0c;如果需要新版本的就需要自己编译。 #查看现有版本 sudo apt-cache madison openscenegraph #安装…

windows上ffmpeg如何录制双屏幕中的一个屏幕上的视频

首先&#xff0c;如何在window上安装ffmpeg自己查找scoop安装ffmpeg. 如题&#xff1a; 如果你有两个屏幕&#xff0c;如何让ffmpeg来录制其中的一个屏幕的视频呢。 很简单&#xff0c;首先你要查看另外一个屏幕的分辨率&#xff1a; 第一步&#xff1a;进入系统中 第二步&am…

VsCode报错:No such file or directory:‘文件名‘

1.问题&#xff1a; 昨天用VsCode直接打开py文件&#xff0c;运行后显示No such file or directory:‘directory’。但directory文件和该py文件在同一目录 2.原因&#xff1a; 直接打开py文件&#xff0c;Vscode看不到同一目录下的其他文件 3.解决方法&#xff1a; 打开文件夹…

漏洞指北-VulFocus靶场专栏-中级03

漏洞指北-VulFocus靶场专栏-初级03 中级009 &#x1f338;gxlcms-cve_2018_14685&#x1f338;step1&#xff1a;安装系统 密码rootstep2 进入后台页面 账号密码&#xff1a;admin amdin888step3 查看详细 有phpinfo() 中级010 &#x1f338;dedecms-cnvd_2018_01221&#x1f3…

EventBus3.0源码详解

详解之前要说明一下&#xff0c;LivedataBus 比EventBus更适合目前jetpack化的app&#xff0c;因为考虑到组件的生命周期处理&#xff0c;性能方面&#xff0c;EventBus还是要反射invoke的调用的&#xff0c;网上找不到有实测过的博文&#xff0c;我想来个性能对比实测&#xf…

【旅游度假】Axure酒店在线预订APP原型图 旅游度假子模块原型模板

作品概况 页面数量&#xff1a;共 10 页 兼容软件&#xff1a;Axure RP 9/10&#xff0c;不支持低版本 应用领域&#xff1a;旅游度假&#xff0c;生活服务 作品申明&#xff1a;页面内容仅用于功能演示&#xff0c;无实际功能 作品特色 本作品为「酒店在线预订」的移动端…

【淘宝】商品详情页+商品列表数据采集

作为国内最大的电商平台之一&#xff0c;淘宝数据采集具有多个维度。 有人需要采集商品信息&#xff0c;包括品类、品牌、产品名、价格、销量等字段&#xff0c;以了解商品销售状况、热门商品属性&#xff0c;进行市场扩大和重要决策&#xff1b; 有人需要采集产品评论&…

EndNote(二)【文献导入、全文检索】

在开始之前我们可以先分个组以管理自己的文件&#xff0c;如果不分组的话&#xff0c;它所有的文件都会放在unfiled这个目录下&#xff1a; 如何分组也很简单&#xff1a; 然后给个名字即可&#xff1a; 文献导入&#xff08;五种方法&#xff09; 第一种&#xff1a;pdf直接导…

STM32F4X USART串口使用

STM32F4X USART串口使用 串口概念起始位波特率数据位停止位校验位串口间接线 STM32F4串口使用步骤GPIO引脚复用函数串口初始化函数串口例程 串口概念 串口是MCU与外部通信的重要通信接口&#xff0c;也是MCU在开发过程中的调试利器。串口通信有几个重要的参数&#xff0c;分别…

基于蚁狮算法优化的BP神经网络(预测应用) - 附代码

基于蚁狮算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码 文章目录 基于蚁狮算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码1.数据介绍2.蚁狮优化BP神经网络2.1 BP神经网络参数设置2.2 蚁狮算法应用 4.测试结果&#xff1a;5.Matlab代码 摘要…

Chrome如何安装插件(文件夹)

1.下载的插件 说明&#xff1a;插件文件夹 2.打开扩展程序位置 3.点击已加载的扩展程序 说明&#xff1a;找到插件的位置 4.报错 说明&#xff1a;那还要进入文件里面。 5.插件的位置 说明&#xff1a;如果已经安装了插件&#xff0c;那么需要查看插件的位置。chrome输入 …

todesk远程连接出现闪退如何解决

官方提供方案&#xff1a;ToDesk远程桌面软件-免费安全流畅的远程连接电脑手机https://www.todesk.com/faq/88.html 图1 打开上述网址&#xff0c;输入“闪退”进行搜索 图2 超级实用解决方法 &#xff08;本机软件设置一下即可&#xff09; 图3 其实图2就已经解决问题了&…

【汇编语言】CS、IP与代码段

文章目录 两个关键的寄存器在CS和IP指示下代码的运行问答 两个关键的寄存器 CS: 代码段寄存器&#xff1b;IP: 指令指针寄存器&#xff1b;CS:IP : CPU将内存中CS&#xff1a;IP只想的内容当作指令执行&#xff1b; 在CS和IP指示下代码的运行 具体步骤描述&#xff1a; 1、第…

【rust/egui】(四)看看template的app.rs:update以及组件TopBottomPanelButton

说在前面 rust新手&#xff0c;egui没啥找到啥教程&#xff0c;这里自己记录下学习过程环境&#xff1a;windows11 22H2rust版本&#xff1a;rustc 1.71.1egui版本&#xff1a;0.22.0eframe版本&#xff1a;0.22.0上一篇&#xff1a;这里 update update实际上还是eframe::App的…

利用快慢指针,求链表的中间结点,判断链表是否是环形链表

前言 &#xff08;1&#xff09;在学习数据结构链表部分的时候&#xff0c;老师给出了几个题目。其中两个题目采用了快慢指针的技术&#xff0c;感觉有意思&#xff0c;于是写一篇博客记录一下。 快慢指针 &#xff08;1&#xff09;我们先来介绍一下快慢指针技术。这个说起来其…

Java性能分析中常用命令和工具

当涉及到 Java 性能分析时&#xff0c;有一系列强大的命令和工具可以帮助开发人员分析应用程序的性能瓶颈、内存使用情况和线程问题。以下是一些常用的 Java 性能分析命令和工具&#xff0c;以及它们的详细说明和示例。 以下是一些常用的性能分析命令和工具汇总&#xff1a; …

vscode远程调试PHP代码

目录 一、准备工作 二、ssh连接和xdebug配置 1.ssh连接 2.xdebug配置 三、xdebug调试&#xff0c;访问 一、准备工作 1.安装vscode里面的两个扩展 2.安装对应PHP版本的xdebug 去xdebug官方&#xff0c;复制自己的phpinfo源码到方框里&#xff0c;再点击Analyse Xdebug: …

【⑫MySQL | 约束(二)】外键 | 默认值 | 检查约束 — 综合案例

前言 ✨欢迎来到小K的MySQL专栏&#xff0c;本节将为大家带来MySQL外键 | 默认值 | 检查约束 以及综合案例的分享✨ 目录 前言6. 外键约束(FOREIGN KEY,FK)7. 默认值约束和检查约束8. 综合实战总结 6. 外键约束(FOREIGN KEY,FK) 前面介绍的完整性约束都是在单表中进行设置&…

VB6创建ActiveX exe简单方法

VB6创建的COM即可以是线程内的DLL&#xff0c;也可以是线程外独立的EXE&#xff0c;有些32位的旧东西做activex.exe封装后在新硬件新软件x64位上用还是可以的。其实activex DLL和activeX EXE主要功能并不需要改变。activex DLL可以是如下的样子&#xff1a; 一个主工程文件&…