【MyBatis】三、ResultMap与多表查询的处理

ResultMap与多表查询的处理

当字段名与实类名不一致时

使用别名进行处理

字段名:emp_name

实体类名:empName

映射文件中写法:

    <select id="getAllEmp" resultType="Emp">select eid, emp_name empName, age, sex, email, did from t_emp</select>

使用全局配置将下划线命名映射为驼峰

在mybatis-config.xml文件的properties标签和typeAlias标签之间添加settings标签如下,可以将下划线式命名映射为驼峰:

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

使用resultMap创建自定义的映射关系

在mapper.xml文件中进行定义:

  • 定义resultMap:

        <!-- 就算是自定义映射关系,也需要相对应的实体类 --><resultMap id="empResultMap" type="Emp"><!-- id用来声明主键,property用来表示实体类中的属性名、column用来标识数据库表中的字段名 --><id property="eid" column="eid"></id><!-- result用来声明普通字段 --><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result></resultMap>
    
  • 传入resultMap的id以用来使用自定义映射

        <select id="getAllEmp" resultMap="empResultMap">select * from t_emp</select>
    

注意:resultMap一般用于处理多对一、一对多的关系

一对多情况的处理

对于多对一的情况(一个员工只会在一个部门中)

只需要在员工实体类中添加一个部门属性:

    private Integer eid;private String empName;private Integer age;private String sex;private String email;private Integer did;private Dept dept;

通过级联属性赋值resultMap解决多对一问题

在mapper.xml文件中创建resultMap:

    <resultMap id="empAndDeptResultMap" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><result property="dept.did" column="did"></result><result property="dept.deptName" column="dept_name"></result></resultMap>

再将这个传入语句进行调用

<!--    Emp getEmpAndDept(@Param("eid") Integer eid);--><select id="getEmpAndDept" resultMap="empAndDeptResultMap">select *from t_emp left join t_dept on t_emp.did = t_dept.didwhere eid=#{eid}</select>

级联属性赋值的方式一般不使用

使用association解决多对一问题

在mapper.xml文件中创建resultMap:

    <resultMap id="empAndDeptResultMapAsscoiation" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><association property="dept" javaType="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result></association></resultMap>

再将之传入即可

通过分步查询解决多对一问题

在mapper.xml建立如下:

<!--    注意在分步查询的过程中,association中的column代表传入的条件--><resultMap id="empAndDeptByStepResultMap" type="emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><association property="dept"select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"></association></resultMap>

Dept的mapper如下:

<!--    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);--><select id="getEmpAndDeptByStepTwo" resultType="Dept">select * from t_dept where did = #{did}</select>

调用:

    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">select * from t_emp where eid = #{eid}</select>

分布查询的优点:延迟加载

当mybatis的查询语句由多步查询构成时,我们可以开启mybatis的懒加载,此时若我们只需要第一步的某个属性,mybatis就不会去调用第二步的sql语句,这样在多种场景下最大限度的保证了性能。

  • 在全局配置(mybatis-config.xml)中添加如下配置

    <!--    全局配置:自动将下划线转为驼峰,懒加载--><settings><setting name="mapUnderscoreToCamelCase" value="true"/><setting name="lazyLoadingEnabled" value="true"></setting></settings>
    

同时,我们也可以在association中使用fetchType标签来使延迟加载变得可控,eager代表立即加载、lazy代表延迟加载

<!--    注意在分步查询的过程中,association中的column代表传入的条件--><resultMap id="empAndDeptByStepResultMap" type="emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><association property="dept"select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"fetchType="eager"></association></resultMap>

多对一情况的处理

在dept实体类中添加多的的那个List:

    private List<Emp> empList;

使用collection标签进行一口气的处理

在deptMapper下进行如下操作:

    <resultMap id="deptAndEmpResultMap" type="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result>
<!--        这里的ofType代表传入的List的泛型--><collection property="empList" ofType="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result></collection></resultMap><select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">select *from t_emp left join t_dept on t_emp.did = t_dept.didwhere t_dept.did=#{did}</select>

通过分步查询进行处理

在DeptMapper中建立第一步的接口:

    /*** 通过分步查询部门以及部门中的员工信息*/Dept getDeptAndEmpByStepOne(@Param("did") Integer did);

在EmpMapper中建立第二步的接口:

    /*** 分步查询第二步,根据did查询员工信息*/List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

在EmpMapper.xml文件中定义xml:

<!--    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);--><select id="getDeptAndEmpByStepTwo" resultType="Emp">select * from t_emp</select>

在DeptMapper.xml文件中定义xml:

    <resultMap id="deptAndEmpByStepResultMap" type="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result><collection property="empList"select="com.qinghe.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"column="did"></collection></resultMap><select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">select * from t_dept where did = #{did}</select>

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

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

相关文章

210. 课程表 II(leetcode210,ArrayList类型的数组创建,拓扑排序)-------------------Java实现

210. 课程表 II&#xff08;leetcode210&#xff0c;ArrayList类型的数组创建&#xff0c;拓扑排序&#xff09;-------------------Java实现 题目表述 现在你总共有 numCourses 门课需要选&#xff0c;记为 0 到 numCourses - 1。给你一个数组 prerequisites &#xff0c;其…

【React】React获取URL参数,根据URL参数隐藏页面元素

React获取URL参数&#xff0c;根据URL参数隐藏页面元素 AI推荐方法 如果您想使用React获取URL参数并相应地隐藏页面元素&#xff0c;可以按照以下步骤进行操作&#xff1a; 导入React和React DOM&#xff1a; import React from react; import ReactDOM from react-dom;创建…

react 中 antd 的 样式和 tailwind 样式冲突

问题原因&#xff1a;在使用 tailwindcss 时&#xff0c;会导入大量的 tailwindcss 默认属性&#xff0c;而默认样式中 button, [typebutton] 包含了 background-color: transparent; 从而导致 antd Button 按钮背景色变成透明。解决办法&#xff1a;禁止 tailwindcss 的默认属…

单例模式(Singleton Pattern)

单例模式 1、掌握单例模式的应用场景。1-1、饿汉式单例1-2、懒汉式单例1-2-1、 测试类:1-2-2、 main1-2-3、 控制台结果1-2-4、 改进 加锁 `synchronized `1-2-5、 控制台输出1-2-6、 再改进,使用双检锁,懒汉式双重检查锁定(Lazy initialization with double-checked locki…

Qt应用开发(基础篇)——工具按钮类 QToolButton

一、前言 QToolButton类继承于QAbstractButton&#xff0c;该部件为命令或选项提供了一个快速访问按钮&#xff0c;通常用于QToolBar中。 按钮基类 QAbstractButton QToolButton是一个特殊的按钮&#xff0c;一般显示文本&#xff0c;只显示图标&#xff0c;结合toolBar使用。它…

【图文并茂】c++介绍之队列

1.1队列的定义 队列&#xff08;queue&#xff09;简称队&#xff0c;它也是一种操作受限的线性表&#xff0c;其限制为仅允许在表的一端进行插入操作&#xff0c;而在表的另一端进行删除操作 一些基础概念&#xff1a; 队尾&#xff08;rear&#xff09; &#xff1a;进行插…

django项目: ModuleNotFoundError: No module named ‘import_export‘

django项目&#xff1a; ModuleNotFoundError: No module named ‘import_export’ 解决方法&#xff1a; pip install django-import_export

MFC新建内部消息

提示&#xff1a;记录一下MFC新建内部消息的成功过程 文章目录 前言一、第一阶段二、第二阶段三、第三阶段总结 前言 先说一下基本情况&#xff0c;因为要在mapview上增加一个显示加载时间的功能。然后发现是要等加载完再显示时间&#xff0c;显示在主窗口。所以就是在子线程中…

DELL precision上安装nvidia A4000驱动 cuda cudnn

一、安装驱动 参考这篇文章进行安装Ubuntu安装Nvidia显卡驱动_Kevin__47的博客-CSDN博客 【出现问题】 禁用nouveau后出现黑屏&#xff0c;有几行代码&#xff0c;断线一直在闪 【解决方法】 1、参考这篇文章Ubuntu20.04安装nvidia显卡驱动并解决重启后黑屏问题_ubuntu安装…

Java常用的设计模式

单例模式&#xff08;Singleton Pattern&#xff09;: 确保一个类只有一个实例&#xff0c;并提供一个全局访问点。示例&#xff1a;应用程序中的配置管理器。 工厂模式&#xff08;Factory Pattern&#xff09;: 用于创建对象的模式&#xff0c;封装对象的创建过程。示例&…

开开心心带你学习MySQL数据库之节尾篇

Java的JDBC编程 各种数据库,MySQL, Oracle, SQL Server在开发的时候,就会提供一组编程接口(API) API ~~ Application Programming Interface ~~ 应用程序编程接口 计算机领域里面的一个非常常见的概念, 给你个软件,你能对他干啥(从代码层次上的) 基于它提供的这些功能,就可以写…

AJAX学习笔记5同步与异步理解

AJAX学习笔记4解决乱码问题_biubiubiu0706的博客-CSDN博客 示例 前端代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>演示AJAX同步和异步</title> </head> <body> <script…

深眸科技自研轻辙视觉引擎,以AI机器视觉赋能杆号牌识别与分拣

电线杆号牌作为电力行业标识的一种&#xff0c;相当于电线杆的“身份证”&#xff0c;担负着宣传电力知识、安全警示的作用&#xff0c;用于户外使用标记输电线路电压等级、线路名称、杆塔编号等&#xff0c;能够清晰地记录电力线路杆的信息&#xff0c;并为电力线路的更改以及…

ChatGPT是如何辅助高效撰写论文及使用ChatGPT注意事项

ChatGPT发布近1年&#xff0c;各大高校对它的态度也发生了极大转变&#xff0c;今年3月发布ChatGPT禁令的牛剑等世界顶级名校也在近期解除了ChatGPT禁令&#xff0c;发布了生成式人工智能使用指南。 ChatGPT一定程度上可以解放科研人员的劳动力&#xff0c;与其直接禁止不如教…

Docker笔记-概念安装简单使用

概念 docker通用词汇。 镜像&#xff1a;Build&#xff0c;创建一个镜像。 仓库&#xff1a;Ship&#xff0c;从仓库和主机上运输镜像。 容器&#xff1a;Run&#xff0c;运行的镜像就是一个容器。 安装 Windows上安装 Docker对win10有原生的支持&#xff0c;win10下的是…

thinkphp6-简简单单地开发接口

目录 1.前言TP6简介 2.项目目录3.运行项目运行命令访问规则 4.model db使用db连接配置model编写及调用调用接口 5.返回json格式 1.前言 基于上篇文章环境搭建后&#xff0c;便开始简单学习上手开发接口…记录重要的过程&#xff01; Windows-试用phpthink发现原来可这样快速搭…

C# 记事本应用程序

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System

numpy函数使用大全python

numpy 是一个功能强大的数学计算库&#xff0c;提供了众多函数和方法来处理和操作数组、矩阵和数值数据。以下是一些常用的 numpy 函数的简要介绍&#xff1a; 创建数组&#xff1a; numpy.array()&#xff1a;创建数组。numpy.zeros()&#xff1a;创建全零数组。numpy.ones(…

IDEA在创建包时如何把包分开实现自动分层

IDEA在创建包时如何把包分开实现自动分层 文章目录 IDEA在创建包时如何把包分开实现自动分层一、为什么要把包分开二、建包时如何把包自动分开三、如何编写配置文件路径&#xff1f; 一、为什么要把包分开 一开始的时候&#xff0c;我也一直以为包连在一起和分开没什么区别&am…

linux内核模块编译方法之模块编程详解

文章目录 一、模块传参二、模块依赖三、内核空间和用户空间四、执行流五、模块编程与应用编程的比较六、内核接口头文件查询总结 本期和大家主要分享的是驱动开发内核编译过程中对于模块是如何设计的&#xff0c;进行了详细的分享&#xff0c;从模块传参、模块依赖一直到内核空…