MyBatis的⾼级映射及延迟加载

MyBatis的⾼级映射及延迟加载

  • 一、多对一
    • 1.方式一:级联属性映射
    • 2.方式二:association
    • 3.方式三:分步查询
  • 二、一对多
    • 1.方式一:collection
    • 2.方式二:分步查询
  • 三、延迟加载(懒加载)
    • 1.分步查询的优点
    • 2.延迟加载(懒加载)


  • 开始写代码前先了解数据库表的结构。
    在这里插入图片描述

一、多对一

  • 以ArticleDetail表(主键)为多,Article表为一。

1.方式一:级联属性映射

  • 编写pojo实体类

    package com.gdb.mybatis.advancedMapping.pojo;
    
    public class ArticleDetail {
        private Integer id;
        private String content;
        private Article article; //多对一,多的为主表时,在主表中添加⼀个属性:Article article; 
       	//有参构造、无参构造、toString、set和get方法...
    }
    
  • 编写映射文件Article

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.gdb.mybatis.advancedMapping.mapper.ArticleDetailMapper">
        <resultMap id="ArticleDetailResultMap" type="ArticleDetail" >
            <id property="id" column="id"/>
            <result property="content" column="content"/>
            <result property="article.id" column="Aid"/>
            <result property="article.userId" column="user_id"/>
            <result property="article.title" column="title"/>
            <result property="article.summary" column="summary"/>
            <result property="article.readCount" column="read_count"/>
            <result property="article.createTime" column="create_time"/>
            <result property="article.updateTime" column="update_time"/>
        </resultMap>
        <select id="selectArticleDetailForId" resultMap="ArticleDetailResultMap">
            select
                d.*, a.id Aid, a.user_id, a.summary, a.title, a.read_count, a.create_time, a.update_time
            from
                article_detail d left join article a on d.article_id = a.id
            where
                d.id = #{id}
        </select>
    </mapper>
    
  • 测试程序

    @org.junit.Test
    public void TestSelectArticleDetailForId(){
        SqlSession sqlSession = SqlSessionUtil.openSqlsession();
        ArticleDetailMapper mapper = sqlSession.getMapper(ArticleDetailMapper.class);
        ArticleDetail articleDetail = mapper.selectArticleDetailForId(1);
    
        System.out.println(articleDetail);
        System.out.println(articleDetail.getArticle());
    
        sqlSession.close();
    }
    
  • 结果展示
    在这里插入图片描述

2.方式二:association

  • 其他位置都不需要修改,只需要修改resultMap中的配置:association即可。
<resultMap id="ArticleDetailResultMap" type="ArticleDetail" >
    <id property="id" column="id"/>
    <result property="content" column="content"/>
    <association property="article" javaType="Article">
        <id property="id" column="id"/>
        <result property="id" column="Aid"/>
        <result property="userId" column="user_id"/>
        <result property="title" column="title"/>
        <result property="summary" column="summary"/>
        <result property="readCount" column="read_count"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </association>
</resultMap>

3.方式三:分步查询

  • 其他位置不需要修改,只需要修改以及添加以下三处:

    • 第一处:在ArticleMapper接⼝中添加⽅法。
    package com.gdb.mybatis.advancedMapping.mapper;
    
    import com.gdb.mybatis.advancedMapping.pojo.Article;
    
    public interface ArticleMapper {
        Article selectArticleForId(Integer id);
    }
    
    • 第二处:在ArticleMapper.xml⽂件中进⾏配置
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.gdb.mybatis.advancedMapping.mapper.ArticleMapper">
        <select id="selectArticleForId" resultType="Article">
            select * from article where id = #{id};
        </select>
    </mapper>
    
    • 第三处:在ArticleDetailMapper.xml文件中的association标签中select位置填写sqlId。sqlId=namespace+id。其中column属性作为这条⼦sql语句的条件。
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.gdb.mybatis.advancedMapping.mapper.ArticleDetailMapper">
        <resultMap id="ArticleDetailResultMap" type="ArticleDetail" >
            <id property="id" column="id"/>
            <result property="content" column="content"/>
            <association property="article" select="com.gdb.mybatis.advancedMapping.mapper.ArticleMapper.selectArticleForId" column="article_id"/>
        </resultMap>
        <select id="selectArticleDetailForId" resultMap="ArticleDetailResultMap">
            select * from article_detail where id = #{id};
        </select>
    </mapper>
    

二、一对多

  • ⼀对多的实现,通常是在⼀的⼀⽅中有List集合属性。

    package com.gdb.mybatis.advancedMapping.pojo;
    
    import java.util.Date;
    import java.util.List;
    
    public class Article {
        private Integer id;
        private Integer userId;
        private String title;
        private String summary;
        private Integer readCount;
        private Date createTime;
        private Date updateTime;
        private List<ArticleDetail> articleDetailList;
    	//有参构造、无参构造、toString、set和get方法...
    }
    

1.方式一:collection

  • ArticleMapper中添加方法

    public interface ArticleMapper {
        Article selectArticleForId(Integer id);
    }
    
  • 编写ArticleMapper.xml映射文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.gdb.mybatis.advancedMapping.mapper.ArticleMapper">
        <resultMap id="ArticleResultMap" type="Article">
            <id property="id" column="id"/>
            <result property="userId" column="user_id"/>
            <result property="title" column="title"/>
            <result property="summary" column="summary"/>
            <result property="readCount" column="read_count"/>
            <result property="createTime" column="create_time"/>
            <result property="updateTime" column="update_time"/>
            <collection property="articleDetailList" ofType="ArticleDetail">
                <id property="id" column="Did"/>
                <result property="content" column="content"/>
            </collection>
        </resultMap>
        <select id="selectArticleForId" resultMap="ArticleResultMap">
            select
                a.*, d.id Did, d.content
            from
                article a left join article_detail d on a.id = d.article_id
            where a.id = #{id}
        </select>
    </mapper>
    
    • 注意是ofType,表示“集合中的类型”。
  • 编写测试程序

    @org.junit.Test
    public void TestSelectArticleForId(){
        SqlSession sqlSession = SqlSessionUtil.openSqlsession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        Article article = mapper.selectArticleForId(1);
    
        System.out.println(article);
        System.out.println(article.getArticleDetailList());
    
        sqlSession.close();
    }
    
  • 查询结果
    在这里插入图片描述

2.方式二:分步查询

  • 和多对一的分布查询方式一样,只需要修改三处的代码即可。

  • 第一处:修改映射文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.gdb.mybatis.advancedMapping.mapper.ArticleMapper">
        <resultMap id="ArticleResultMap" type="Article">
            <id property="id" column="id"/>
            <result property="userId" column="user_id"/>
            <result property="title" column="title"/>
            <result property="summary" column="summary"/>
            <result property="readCount" column="read_count"/>
            <result property="createTime" column="create_time"/>
            <result property="updateTime" column="update_time"/>
            <collection property="articleDetailList" select="com.gdb.mybatis.advancedMapping.mapper.ArticleDetailMapper.selectArticleDetailByArticleId" column="id"/>
        </resultMap>
        <select id="selectArticleForId" resultMap="ArticleResultMap">
            select * from article where id = #{id};
        </select>
    </mapper>
    
  • 第二处:在ArticeDetailMapper中添加方法

    /**
     * 通过文章的编号 id,查询文章下所有的评论
     * @param ArticleId 文章id
     * @return 返回对应文章下的所有评论
     */
    List<ArticleDetail> selectArticleDetailByArticleId(Integer ArticleId);
    
  • 第三处:编写ArticleDetailMapper.xml文件中的sql语句。

    <select id="selectArticleDetailByArticleId" resultType="ArticleDetail">
        select * from article_detail where article_id = #{articleId};
    </select>
    

三、延迟加载(懒加载)

1.分步查询的优点

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

2.延迟加载(懒加载)

  • 延迟加载的核心原理是:用的时候再执行查询语句。不用的时候不查询。
  • 作用:提高性能。尽可能的不查,或者说尽可能的少查。来提高效率。
  • 在mybatis当中怎么开启延迟加载:
    • association 标签中添加fetchType = “lazy”
    • 注意:默认情况下是没有开启延迟加载的。需要设置:fetchType = “lazy”
    • 这种在association标签中配置fetchType=“lazy”,是局部设置,只对当前的association关联的sql语句起作用。
  • 在实际的开发中,大部分都是需要使用延迟加载的,所以建议开启全部的延迟加载机制。
    • 在 mybatis 核心配置文件中添加全局配置:lazyLoadingEnabled=true
  • 实际开发中的模式:
    • 把全局的延迟加载开启。
    • 如果某一步不需要使用延迟加载,设置:fetchType=“eager”。
      在这里插入图片描述
      在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【C++】类和对象之拷贝构造函数篇

个人主页 &#xff1a; zxctscl 文章封面来自&#xff1a;艺术家–贤海林 如有转载请先通知 文章目录 1. 前言2. 传值传参和传引用传参3. 概念4. 特征 1. 前言 在前面学习了6个默认成员函数中的构造函数和析构函数 【C】构造函数和析构函数详解&#xff0c;接下来继续往后看拷…

uvloop,一个强大的 Python 异步IO编程库!

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站零基础入门的AI学习网站~。 目录 ​编辑 前言 什么是uvloop库&#xff1f; 安装uvloop库 使用uvloop库 uvloop库的功能特性 1. 更…

nodejs+vue+ElementUi废品废弃资源回收系统

系统主要是以后台管理员管理为主。管理员需要先登录系统然后才可以使用本系统&#xff0c;管理员可以对系统用户管理、用户信息管理、回收站点管理、站点分类管理、站点分类管理、留言板管理、系统管理进行添加、查询、修改、删除&#xff0c;以保障废弃资源回收系统系统的正常…

【Linux】部署单机项目(自动化启动)

目录 一.jdk安装 二.tomcat安装 三.MySQL安装 四.部署项目 一.jdk安装 1.上传jdk安装包 jdk-8u151-linux-x64.tar.gz 进入opt目录&#xff0c;将安装包拖进去 2.解压安装包 防止后面单个系列解压操作&#xff0c;我这边就直接将所有的要用的全部给解压&#xff0c;如下图注…

2024 高级前端面试题之 计算机通识(基础) 「精选篇」

该内容主要整理关于 计算机通识&#xff08;基础&#xff09; 的相关面试题&#xff0c;其他内容面试题请移步至 「最新最全的前端面试题集锦」 查看。 计算机基础精选篇 一、网络1.1 UDP1.2 TCP1.3 HTTP1.4 DNS 二、数据结构2.1 栈2.2 队列2.3 链表2.4 树2.5 堆 三、算法3.1 时…

计算机毕业设计-SSM课程题库管理系统 18655(赠送源码数据库)JAVA、PHP,node.js,C++、python,大屏数据可视化等

毕业设计&#xff08;论文&#xff09; SSM课程题库管理系统 学 院 专 业 班 级 学 号 学生姓名 指导教师 完成日期…

vSphere高可用架构---HA简介

1.高可用性 2.不同级别的高可用&#xff1a; 1&#xff09;应用程序级别&#xff0c;2&#xff09;操作系统级别&#xff0c;3&#xff09;虚拟化级别&#xff0c;4&#xff09;物理层级别 不同级别的高可用举例&#xff1a; 应用程序级别的高可用性。例如&#xff1a;Oracl…

使用C++和SFML库创建2D游戏

FML&#xff08;Simple and Fast Multimedia Library&#xff09;是一个跨平台的C库&#xff0c;用于开发2D游戏和多媒体应用程序。它提供了许多功能&#xff0c;包括图形、声音、网络、窗口管理和事件处理等。 ———————————————不怎么完美的分割线——————…

Spring Boot 手写starter!!!

原因&#xff1a;为什么要手写starter&#xff1f;&#xff1f;&#xff1f; 原因&#xff1a;简化功能。 实例&#xff1a;以分页为例&#xff1a;写一个starter。 1.首先定义一个PageX注解。 Target({ElementType.METHOD}) Retention(RetentionPolicy.RUNTIME) Documented p…

利用Dynamo为家具族三维截图并导入到明细表

前几天我在朋友圈发了一个小视频&#xff0c;是利用Dynamo为家具族截图&#xff0c;并将截图添加到族参数&#xff0c;以便于在图纸中显示族的样子。效果如下&#xff1a; 此处为语雀视频卡片&#xff0c;点击链接查看&#xff1a; 利用Dynamo为家具族三维截图并导入到明细表 …

TF-IDF,textRank,LSI_LDA 关键词提取

目录 任务 代码 keywordExtract.py TF_IDF.py LSI_LDA.py 结果 任务 用这三种方法提取关键词&#xff0c;代码目录如下&#xff0c; keywordExtract.py 为运行主程序 corpus.txt 为现有数据文档 其他文件&#xff0c;停用词&#xff0c;方法文件 corpus.txt 可以自己…

【统计分析数学模型】聚类分析: 系统聚类法

【统计分析数学模型】聚类分析&#xff1a; 系统聚类法 一、聚类分析1. 基本原理2. 距离的度量&#xff08;1&#xff09;变量的测量尺度&#xff08;2&#xff09;距离&#xff08;3&#xff09;R语言计算距离 三、聚类方法1. 系统聚类法2. K均值法 三、示例1. Q型聚类&#x…

udp服务器【Linux网络编程】

目录 一、UDP服务器 1、创建套接字 2、绑定套接字 3、运行 1&#xff09;读取数据 2&#xff09;发送数据 二、UDP客户端 创建套接字&#xff1a; 客户端不用手动bind 收发数据 处理消息和网络通信解耦 三、应用场景 1、服务端执行命令 2、Windows上的客户端 3…

GEE必须会教程—曾“几何”时(Geometry类型)

几何图形组成了世界万物&#xff0c;在数学史具有重要地位&#xff0c;将几何图形迁移到地理空间信息的处理上&#xff0c;我们我们得到就是研究区域的边界范围&#xff0c;因此&#xff0c;在学习矢量数据和栅格数据之前&#xff0c;我们有必要了解几何图形在GEE上的编辑。 1…

git最全总结

文章目录 Git 分布式版本控制工具内容1. 前言1.1 什么是Git1.2 使用Git能做什么 2. Git概述2.1 Git简介2.2 Git下载与安装 3. Git代码托管服务3.1 常用的Git代码托管服务3.2 码云代码托管服务3.2.1 注册码云账号3.2.2 登录码云3.2.3 创建远程仓库3.2.4 邀请其他用户成为仓库成员…

pikachu靶场-RCE

介绍&#xff1a; RCE(remote command/code execute)概述 RCE漏洞&#xff0c;可以让攻击者直接向后台服务器远程注入操作系统命令或者代码&#xff0c;从而控制后台系统。 远程系统命令执行 一般出现这种漏洞&#xff0c;是因为应用系统从设计上需要给用户提供指定的远程命…

Spring篇----第六篇

系列文章目录 文章目录 系列文章目录前言一、spring 支持集中 bean scope?二、spring bean 容器的生命周期是什么样的?三、什么是 spring 的内部 bean?前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站,这篇文章男…

基于springboot+vue的服装生产管理系统(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

2024022502-数据库绪论

数据库绪论 数据管理的三个阶段 人工管理阶段 文件系统阶段 数据库系统阶段 基本术语 数据&#xff08;Data&#xff09; 计算机用来描述事物的记录&#xff08;文字&#xff0e;图形&#xff0e;图像&#xff0e;声音&#xff09;数据的形式本身并不能完全表达其内容&am…

新付费进群源码-带分销分站源码程序(附搭建源码+安装建设教程)

付费进群源码是由程序员创建的计算机程序的基本组成部分&#xff0c;通常以函数、描述、定义、调用、方法和其他操作语句的形式编写。它被设计为人类可读的&#xff0c;并以开发人员和其他用户可以理解的方式格式化。 付费进群源码系统要求 硬件要求&#xff1a; 需要一台运行稳…