Java_mybatis-结果集映射-ResultTypeResultMap

Mybatis返回值接收

可以使用两种方式进行参数的接收

  • resultType
  • resultMap

这两种分别都是需要在Mapper.xml文件中去设置的

当结果是一个简单的对象或者list或者map,对象中没有嵌套对象,或者集合时,就可以直接使用resultType

反之如果需要返回的值是一个复杂对象,其中包含list或者map的时候,就需要使用resultMap去确定返回值格式

1 使用 resultType

<sql id="basicSelect">
    id,name,age,address,emp_detail
</sql>
  • 查询单个Map对象

    <select id="selectUsers" resultType="map">
      select id, username, hashedPassword
      from some_table
      where id = #{id}
    </select>
    
    Map selectUsers(Long id);
    
  • 查询具体单个对象

    <select id="selectEmpById" resultType="cn.sycoder.domain.Employee">
        select
            <include refid="basicSelect"></include>
        from employee  where id = #{id}
    
    </select>
    <!--    定义sql-->
    <sql id="basicSelect">
        id,name,age,address,emp_detail
    </sql>
    
    Employee selectEmpById(Long id);
    
  • 查询集合对象

    <select id="selectEmp" resultType="cn.sycoder.domain.Employee">
        select
            <include refid="basicSelect"></include>
        from employee
        where id = #{id}
    </select>
    
    List<Employee> selectEmp(Long id);
    
  • 查询单个值

    <select id="selectCount" resultType="java.lang.Integer">
        select count(*) from employee
    </select>
    
    Integer selectCount();
    

2 使用 resultMap

2.1 简单使用

  • 应用场景:实体类属性和数据库列名不匹配的时候(比如,数据库采用经典命名法,java 使用驼峰命名法的时候)

    <resultMap id="basicMap" type="cn.sycoder.domain.Employee">
    <!--        设置数据库id 的对应字段-->
            <id property="id" column="id"></id>
            <result property="empDetail" column="emp_detail"></result>
            <result property="name" column="name"></result>
    
        </resultMap>
    
    <select id="selectEmpById" resultMap="basicMap">
            select
                <include refid="basicSelect"></include>
            from employee  where id = #{id}
    
        </select>
    

    在这里插入图片描述

  • 解决方式2

       <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
    
  • id & result
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    
  • id & result 属性

    属性描述
    property映射到列结果的字段或属性。如果 JavaBean 有这个名字的属性(property),会先使用该属性。否则 MyBatis 将会寻找给定名称的字段(field)。无论是哪一种情形,你都可以使用常见的点式分隔形式进行复杂属性导航。 比如,你可以这样映射一些简单的东西:“username”,或者映射到一些复杂的东西上:“address.street.number”。 stu.name
    column数据库中的列名,或者是列的别名。一般情况下,这和传递给 resultSet.getString(columnName) 方法的参数一样。
    javaType一个 Java 类的全限定名,或一个类型别名(关于内置的类型别名,可以参考上面的表格)。 如果你映射到一个 JavaBean,MyBatis 通常可以推断类型。然而,如果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证行为与期望的相一致。

2.2多结果集处理前期准备

  • 新建学生和班级表

    在这里插入图片描述

    在这里插入图片描述

    create table class
    (
    	id bigint auto_increment
    		primary key,
    	name varchar(64) null
    );
    
    
    
    create table student
    (
    	id bigint auto_increment
    		primary key,
    	name varchar(64) null,
    	age int null,
    	class_id bigint null,
    	constraint student_class_id_fk
    		foreign key (class_id) references class (id)
    );
    
    insert into class values (null,'软工1班'),(null,'计科2班');
    
    insert into student (id, name, age,class_id)
    values (null,'sy',18,1),(null,'zs',19,1),(null,'zz',20,1),(null,'小明',22,2);
    
    

2.3一对多处理

  • collection :使用 collection 就可以获取到多个结果集对象

  • 一个班级对应多个学生

  • 操作

    • 第一步,新建 mapper 方法

      public interface ClassMapper {
      
          MyClass getById(Long id);
      }
      
    • 第二步,编写 xml

      <resultMap id="basicMap" type="cn.sycoder.domain.MyClass">
              <id property="id" column="id"></id>
              <result property="name" column="name"></result>
      <!--        获取学生信息信息-->
              <collection property="stus" ofType="cn.sycoder.domain.Student">
                  <id property="id" column="sId"></id>
                  <result property="name" column="sName"></result>
                  <result property="age" column="age"></result>
                  <result property="classId" column="class_id"></result>
              </collection>
          </resultMap>
      
          <select id="getById" resultMap="basicMap">
              select
                  c.*,s.id sId,s.name sName,s.age,s.class_id
              from
                  class c left join student s on c.id = s.class_id
              where c.id = #{id}
          </select>
      

2.4多对一的处理

  • 关联(association):如果我的类里面有其它对象的关联关系,可以使用 association 来进行操作

    属性描述
    property映射到列结果的字段或属性。如果用来匹配的 JavaBean存在给定名字的属性,那么它将会被使用。否则 MyBatis 将会寻找给定名称的字段。无论是哪一种情形,你都可以使用通常的点式分隔形式进行复杂属性导航。比如,你可以这样映射一些简单的东西:“username”,或者映射到一些复杂的东西上:“address.street.number”。
    javaType一个 Java 类的完全限定名,或一个类型别名(关于内置的类型别名,可以参考上面的表格)。 如果你映射到一个 JavaBean,MyBatis 通常可以推断类型。然而,如果你映射到的是HashMap,那么你应该明确地指定 javaType 来保证行为与期望的相一致。
  • 传统操作

    • 使用级联操作

       <resultMap id="basicMap" type="cn.sycoder.domain.Student">
              <id property="id" column="id"></id>
              <result property="name" column="name"></result>
              <result property="age" column="age"></result>
              <result property="classId" column="class_id"></result>
              <result property="cls.id" column="cId"></result>
              <result property="cls.name" column="cName"></result>
          </resultMap>
      
          <select id="listAllStus" resultMap="basicMap">
              select
              stu.*,c.id cId,c.name cName
              from
              student stu left join class c on stu.class_id = c.id
          </select>
      
  • 使用 association 操作

    • 代码

      <resultMap id="AssociationMap" type="cn.sycoder.domain.Student">
              <id property="id" column="id"></id>
              <result property="name" column="name"></result>
              <result property="age" column="age"></result>
              <result property="classId" column="class_id"></result>
              <association property="cls" javaType="cn.sycoder.domain.MyClass">
                  <id property="id" column="cId"></id>
                  <result property="name" column="cName"></result>
              </association>
          </resultMap>
      
          <select id="listAllStusByAssociation" resultMap="AssociationMap">
              select
              stu.*,c.id cId,c.name cName
              from
              student stu left join class c on stu.class_id = c.id
          </select>
      

3 嵌套 select 查询

  • 以多条sql 的方式执行

3.1关联关系 assciation select

  • 查询学生信息,包含班级信息

    <resultMap id="AssociationSelectMap" type="cn.sycoder.domain.Student">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
            <result property="classId" column="class_id"></result>
            <association property="cls" column="class_id"
                         select="cn.sycoder.mapper.StudentMapper.getClassById"/>
        </resultMap>
    
        <select id="listAllStusByAssociationSelect" resultMap="AssociationSelectMap">
            select * from student
        </select>
    
        <select id="getClassById" resultType="cn.sycoder.domain.MyClass">
            select * from class where id = #{id}
        </select>
    
  • 如果关联的是多个结果集使用 resultSet

    属性描述
    column当使用多个结果集时,该属性指定结果集中用于与 foreignColumn 匹配的列(多个列名以逗号隔开),以识别关系中的父类型与子类型。
    foreignColumn指定外键对应的列名,指定的列将与父类型中 column 的给出的列进行匹配。
    resultSet指定用于加载复杂类型的结果集名字。
    <resultMap id="blogResult" type="Blog">
      <id property="id" column="id" />
      <result property="title" column="title"/>
      <association property="author" javaType="Author" resultSet="authors" column="author_id" foreignColumn="id">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="email" column="email"/>
        <result property="bio" column="bio"/>
      </association>
    </resultMap>
    

3.2 collection select

  • 需求:通过班级去查学生,使用嵌套 select 查询

    <resultMap id="collectionSelect" type="cn.sycoder.domain.MyClass">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <!--        获取学生信息信息-->
            <collection property="stus" ofType="cn.sycoder.domain.Student"
                    select="getStudentByClassId"    column="id"/>
        </resultMap>
        <select id="getByClassId" resultMap="collectionSelect">
            select * from class where id = #{id}
        </select>
    
        <select id="getStudentByClassId" resultType="cn.sycoder.domain.Student">
            select * from student where class_id = #{id}
        </select>
    

3.3 关联查询的总结

  • 优点:
    • 可以实现延迟加载,前提是要配置
    • sql 写起来变得简单了
  • 缺点:
    • 发起了多条 sql,正常查询只发起一条sql

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

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

相关文章

记账本选择标签选择时间,计算器---记录一下

html部分 <template><view class"pages-main"><!-- 标题栏 --><!-- #ifndef MP-TOUTIAO --><view class"" :style"height:barHeight px;"></view><!-- #endif --><!-- #ifdef MP-TOUTIAO -->&…

node.js安装和配置

软件介绍 Node.js是一个免费的、开源的、跨平台的JavaScript运行时环境&#xff0c;允许开发人员在浏览器之外编写命令行工具和服务器端脚本。 Node.js是一个基于Chrome JavaScript运行时建立的一个平台。 Node.js是一个事件驱动I/O服务端JavaScript环境&#xff0c;基于Googl…

Java连接数据库的各种细节错误(细节篇)

目录 前后端联调&#xff08;传输文件&#xff09; ClassNotFoundException: SQLException: SQL语法错误: 数据库连接问题: 驱动问题: 资源泄露: 并发问题: 超时问题: 其他库冲突: 配置问题: 网络问题: SSL/TLS问题: 数据库权限问题: 驱动不兼容: 其他未知错误…

祝贺!2023美丽汉字小达人市级比赛和区级自由报名获奖名单发布

昨天&#xff0c;汉字小达人的主办方《中文自修》杂志社在官网发布了两个公示&#xff1a;《“中文自修杯”第十届上海市小学生“美丽汉字小达人”市级活动获奖名单公示》、《“中文自修杯”第十届上海市小学生“美丽汉字小达人”区级活动“自由报名”获奖名单公示》。 这两份名…

在虚拟机的Windows操作系统中:通过Jar方式若依项目,以及在外部的访问!

&#x1f4da;&#x1f4da; &#x1f3c5;我是默&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; ​​ &#x1f31f;在这里&#xff0c;我要推荐给大家我的专栏《Windows》。&#x1f3af;&#x1f3af; &#x1f680;无论你是编程小白&#xff0c;还是有…

SCA面面观 | 五大维度提升,让SCA产品走向成熟

随着开源软件的迅速崛起&#xff0c;特别是在2021年SolarWinds和Log4j漏洞事件引发全球关注后&#xff0c;软件成分分析&#xff08;Software Composition Analysis&#xff0c;简称SCA&#xff09;越来越受到业界的重视。SCA产品已经逐渐成为企业软件供应链资产管理、漏洞管理…

【UE 材质】切换颜色、纹理时的过渡效果

效果 步骤 1. 新建一个工程&#xff0c;创建Basic关卡 2. 创建一个材质&#xff0c;这里命名为“M_Plane”&#xff0c;打开这个材质&#xff0c;在材质图表中添加如下节点 注意“Noise”节点中的函数选择“Voronoi” 3. 对材质“M_Plane”创建材质实例 4. 在场景中放置一个平…

Java_Mybatis_缓存

缓存 1.概述 Mybatis 缓存&#xff1a;MyBatis 内置了一个强大的事务性查询缓存机制&#xff0c;它可以非常方便地配置和定制 2.会话缓存&#xff08;一级缓存&#xff09; sqlSession 级别的&#xff0c;也就是说&#xff0c;使用同一个 sqlSession 查询同一 sql 时&#x…

Impala4.x源码阅读笔记(二)——Impala如何高效读取Iceberg表

前言 本文为笔者个人阅读Apache Impala源码时的笔记&#xff0c;仅代表我个人对代码的理解&#xff0c;个人水平有限&#xff0c;文章可能存在理解错误、遗漏或者过时之处。如果有任何错误或者有更好的见解&#xff0c;欢迎指正。 Iceberg表是一种用于存储大规模结构化数据的…

Vue指令之v-on

v-on指令用于注册事件&#xff0c;作用是添加监听与提供事件触发后对应的处理函数。 v-on有两种语法&#xff0c;在提供处理函数的时候既可以直接使用内联语句&#xff0c;也可以提供函数的名字。 第一种语法是直接提供内联语句&#xff0c;如下 v-on:事件名 "内联语句…

外贸SOHO建站教程?海洋建站推广如何做?

外贸SOHO建站推广的步骤&#xff1f;国际贸易网站建设方法&#xff1f; 随着互联网的普及和发展&#xff0c;越来越多的外贸SOHO从业者选择通过建立自己的网站来拓展业务。那么&#xff0c;如何搭建一个专业、高效的外贸网站呢&#xff1f;海洋建站将为您提供一份详细的外贸SO…

Java - Bean的生命周期

Bean的生命周期之5步 Bean生命周期的管理&#xff0c;可以参考Spring的源码&#xff1a;AbstractAutowireCapableBeanFactory类的doCreateBean()方法。 Bean生命周期可以粗略的划分为五大步&#xff1a; 第一步&#xff1a;实例化Bean 第二步&#xff1a;Bean属性赋值 第三…

扫描电镜(SEM)样品在进行扫描电镜观察前需要进行哪些处理

对于扫描电镜&#xff08;Scanning Electron Microscope&#xff0c;SEM&#xff09;样品的制备&#xff0c;需要经过一系列处理步骤以确保样品表面的干净、导电性好&#xff0c;并且能够提供高质量的显微图像。以下是一些常见的处理步骤&#xff1a; 1. 固定样品&#xff08;…

Vue 学习随笔系列七 -- 表单动态生成

表单动态生成 文章目录 表单动态生成1、动态表单组件封装2、组件引用3、实现效果 1、动态表单组件封装 <!-- 动态生成下拉框&#xff0c;可同理生成input框等 --> <template><el-dialogcustom-class"custom-dialog":title"dialogTitle":vi…

Linux 使用定时任务

在Linux中&#xff0c;你可以使用cron&#xff08;定时任务管理器&#xff09;来设置和管理定时任务。以下是使用cron的基本步骤 编辑定时任务列表 打开终端&#xff0c;输入以下命令来编辑当前用户的定时任务列表 crontab -e如果是要编辑系统范围的定时任务&#xff0c;可以…

如何在忘记密码的情况下恢复解锁 iPhone

您忘记了 iPhone 密码吗&#xff1f;Apple 官方通常建议将 iPhone 恢复至出厂设置以将其删除。这种修复很不方便&#xff0c;甚至可能比问题本身更麻烦。 如果您也经历过同样的情况&#xff0c;并且想知道忘记了 iPhone 密码并且不想恢复它该怎么办&#xff0c;我们的终极指南…

docker基本管理和docker相关概念

docker是开源的的应用容器引擎&#xff0c;基于go语言开发的&#xff0c;运行在linux系统当中的开源的轻量级的"虚拟机。 docker的容器技术可以在一台主机上轻松的为任何应用创建一个轻量级的&#xff0c;可以移植的&#xff0c;自给自足的容器 docker的宿主机是linux系…

ElementPlus table 中嵌套 input 输入框

文章目录 需求分析 需求 vue3 项目中 使用UI组件库 ElementPlus 时&#xff0c;table 中嵌入 input输入框 分析 <template><div class"p-10"><el-table :data"tableData" border><el-table-column prop"date" label&qu…

jemeter,http cookie管理器

Http Cookie管理器自动实现Cookie关联的原理&#xff1a; (默认:作用域在同级别的组件) 一:当Jmeter第1次请求服务器的时候,如果说服务器有通过响应头的Set-Cookie有返回Cookie,那么Http Cookie管理器就会自动的保存这些Cookie的值。 二&#xff1a;当Jmeter第2-N次请求服务器的…

【同步FIFO_2023.12.13】

同步fifo&#xff0c;写时钟和读时钟为同一个时钟&#xff0c;用于交互数据缓冲 fifo的深度&#xff1a;同一块数据内存的大小 reg [2:0] Mem [8];//宽度3&#xff0c;深度8典型同步fifo的三部分 fifo写控制逻辑&#xff1a;写地址、写有效信号&#xff0c;fifo写满、写错等状…
最新文章