在 Mybatis 中,resultType 和 resultMap 都用于定义查询结果的映射关系。它们的使用场景如下:

  1. resultType

resultType 用于指定返回结果的数据类型,通常用于返回简单类型的结果以及返回 vo 或 dto 等自定义类型的结果。例如:

<select id="findUserById" parameterType="int" resultType="com.example.User">
select * from user where id = #{id}
</select>

这里 resultType 指定了查询结果将以 com.example.User 类型返回,Mybatis 会自动将查询结果映射到 User 对象中。resultType 适用于简单的查询操作,能够方便快捷地获取数据库表中的数据,并且使用起来比较简单。

  1. resultMap

resultMap 用于自定义查询结果的映射规则,通常用于处理复杂的查询结果,或者需要使用嵌套查询等复杂查询操作。例如:

<resultMap id="userResultMap" type="com.example.User">
<id property="id" column="user_id" />
<result property="username" column="user_name" />
<result property="email" column="user_email" />
<association property="orders" javaType="list" resultMap="orderResultMap" />
</resultMap> <resultMap id="orderResultMap" type="com.example.Order">
<id property="id" column="order_id" />
<result property="amount" column="order_amount" />
<result property="createTime" column="create_time" />
</resultMap> <select id="findUserById" parameterType="int" resultMap="userResultMap">
select u.id as user_id, u.username as user_name, u.email as user_email,
o.id as order_id, o.amount as order_amount, o.create_time
from user u left join order o on u.id = o.user_id
where u.id = #{id}
</select>

这里 resultMap 定义了 User 类型和 Order 类型的结果映射规则,Mybatis 会根据这些规则将复杂的查询结果映射到对应的 Java 类型中。对于复杂的查询操作,使用 resultMap 可以很好地处理查询结果中的各种细节,支持结果的拼接、嵌套等操作,使查询逻辑更加清晰。

1. resultMap 处理字段和属性的映射关系

若字段名和实体类中的属性名不一致,则可以通过 resultMap 设置自定义映射

<!--
resultMap:设置自定义映射
属性:
id:表示自定义映射的唯一标识
type:查询的数据要映射的实体类的类型
子标签:
id:设置主键的映射关系
result:设置普通字段的映射关系
association:设置多对一的映射关系
collection:设置一对多的映射关系
属性:
property:设置映射关系中实体类中的属性名
column:设置映射关系中表中的字段名
-->
<resultMap id="userMap" type="User">
<id property="id" column="id"></id>
<result property="userName" column="user_name"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</resultMap>
<!--List<User> testMohu(@Param("mohu") String mohu);-->
<select id="testMohu" resultMap="userMap">
<!--select * from t_user where username like '%${mohu}%'-->
select id,user_name,password,age,sex from t_user where user_name likeconcat('%',#{mohu},'%')
</select>

  • resultMap:设置自定义映射

  • 属性:

    • id:表示自定义映射的唯一标识
    • type:查询的数据要映射的实体类的类型
  • 子标签 :

    • id:设置主键的映射关系
    • result:设置普通字段的映射关系
    • association:设置多对一的映射关系
    • collection:设置一对多的映射关系
  • 属性 :

    • property:设置映射关系中实体类中的属性名
    • column:设置映射关系中表中的字段名

若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性名符合 Java 的规则(使用驼峰)此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系:

  1. 可以通过为字段起别名的方式,保证和实体类中的属性名保持一致
  2. 可以在 MyBatis 的核心配置文件中设置一个全局配置信息 mapUnderscoreToCamelCase,可

以在查询表中数据时,自动将_类型的字段名转换为驼峰

例如:字段名 user_name,设置了 mapUnderscoreToCamelCase,此时字段名就会转换为 userName

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

2. 多对一映射处理

2.1 级联方式处理映射关系

场景模拟: 查询员工信息以及员工所对应的部门信息

语法显示: (注意 column 和 property 的位置)

    <resultMap id="empAndDeptByEmpId" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<result column="dept_id" property="dept.deptId"></result>
<result column="dept_name" property="dept.deptName"></result>
</resultMap> <select id="getEmpAndDeptById" resultMap="empAndDeptByEmpId">
select t_emp.* , t_dept.*
from t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = #{empId}
</select>

2.2 使用 association 处理映射关系

    <resultMap id="empAndDeptByEmpIdTwo" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<association property="dept" javaType="Dept">
<result column="dept_id" property="deptId"></result>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap> <select id="getEmpAndDeptById" resultMap="empAndDeptByEmpIdTwo">
select t_emp.* , t_dept.*
from t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = #{empId}
</select>

  • association : 处理多对一的映射关系 (处理实体类型的属性)
  • property : 设置需处理映射关系的属性的属性名
  • JavaType : 设置要处理的属性的类型

3. 分步查询

① 查询员工信息

代码示例 :

    /**
* 通过分步查询员工信息
* @param empId
* @return
*/
Emp getEmpByStep(@Param("empId") Integer empId);

    <resultMap id="empDeptSteMap" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<association property="dept"
select="com.north.mybatis.mapper.DeptMapper.getEmpDeptByStep"
column="dept_id"
></association>
</resultMap> <!--
select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId)
column:将sql以及查询结果中的某个字段设置为分步查询的条件
--> <select id="getEmpByStep" resultMap="empDeptSteMap">
select * from t_emp where emp_id = #{empId}
</select>

② 根据员工所对应的部门 id 查询部门信息

代码示例如下 :

    /**
* 分步查询的第二步: 根据员工所对应的did查询部门信息
* @param deptId
* @return
*/
Dept getEmpDeptByStep(@Param("deptId") Integer deptId);

        <select id="getEmpDeptByStep" resultType="Dept">
select * from t_dept where dept_id = #{deptId}
</select>

3. 延迟加载

Mybatis 中的延迟加载指的是在查询主体对象时,只查询主体对象的信息,而对于关联的属性或集合对象,直到访问这些对象时才会进行查询操作,以达到减少查询次数、提高性能的目的。

延迟加载的作用是:

  1. 减少查询次数:当需要查询的数据量较大时,可以通过延迟加载避免一次性查询所有的数据,大大降低数据库的负载和查询时间,提高查询效率。
  2. 避免冗余数据:当主体对象包含多个关联属性或集合对象时,如果一次性加载所有数据,有可能会产生冗余的查询数据,而使用延迟加载可避免这种情况的出现,节省资源。
  3. 提升性能:当关联对象比较复杂时,一次性加载所有数据可能会显著降低程序的运行效率,使用延迟加载技术可以避免这种问题,并提高程序的运行速度。

延迟加载在 Mybatis 中的实现是通过动态代理实现的,Mybatis 在查询主体对象时,只返回代理对象,当第一次访问代理对象的相关属性时,会触发查询操作,从而实现延迟加载的效果。不过需要注意的是,Mybatis 中延迟加载的实现需要依赖于持久化框架和数据库的支持,如果底层不支持延迟加载,Mybatis 的延迟加载技术则无法实现,需要采取其他的手段进行基于性能的优化。

4. 一对多映射处理

4.1 collection

/**
* 根据部门id查新部门以及部门中的员工信息
* @param did
* @return
*/
Dept getDeptEmpByDid(@Param("did") int did);
<resultMap id="deptEmpMap" type="Dept">
   <id property="did" column="did"></id>
   <result property="dname" column="dname"></result>
   <!--
ofType:设置collection标签所处理的集合属性中存储数据的类型
-->
<collection property="emps" ofType="Emp">
       <id property="eid" column="eid"></id>
       <result property="ename" column="ename"></result>
       <result property="age" column="age"></result>
       <result property="sex" column="sex"></result>
</collection>
</resultMap>
<!--Dept getDeptEmpByDid(@Param("did") int did);-->
<select id="getDeptEmpByDid" resultMap="deptEmpMap">
select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did =emp.did where dept.did = #{did}
</select>

4.2、分步查询

① 查询部门信息

/**
* 分步查询部门和部门中的员工
* @param did
* @return
*/
Dept getDeptByStep(@Param("did") int did);
<resultMap id="deptEmpStep" type="Dept">
   <id property="did" column="did"></id>
   <result property="dname" column="dname"></result>
   <collection property="emps" fetchType="eager"select="com.atguigu.MyBatis.mapper.EmpMapper.getEmpListByDid" column="did">
</collection>
</resultMap>
<!--Dept getDeptByStep(@Param("did") int did);-->
<select id="getDeptByStep" resultMap="deptEmpStep">
select * from t_dept where did = #{did}
</select>

② 根据部门 id 查询部门中的所有员工

/**
* 根据部门id查询员工信息
* @param did
* @return
*/
List<Emp> getEmpListByDid(@Param("did") int did);
<!--List<Emp> getEmpListByDid(@Param("did") int did);-->
<select id="getEmpListByDid" resultType="Emp">
select * from t_emp where did = #{did}
</select>

分步查询的优点:可以实现延迟加载

但是必须在核心配置文件中设置全局配置信息:

lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载

aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载

此时就可以实现按需加载,获取的数据是什么,就只会执行相应的 sql。此时可通过 association 和

collection 中的 fetchType 属性设置当前的分步查询是否使用延迟加载, fetchType="lazy(延迟加

载)|eager(立即加载)"

8. 自定义映射resultMap的更多相关文章

  1. MyBatis_06(自定义映射resultMap)

    主题:自定义映射resultMap "自定义映射resultMap",可以解决什么问题: 1-"属性" 和 "字段名" 不一致的情况 2-& ...

  2. 自定义映射resultMap

    resultMap处理字段和属性的映射关系 如果字段名与实体类中的属性名不一致,该如何处理映射关系? 第一种方法:为查询的字段设置别名,和属性名保持一致 下面是实体类中的属性名: private In ...

  3. mybatis-自定义映射resultMap

    自定义映射resultMap resultMap处理字段和属性的映射关系 resultMap:设置自定义映射 属性: id:表示自定义映射的唯一标识,不能重复 type:查询的数据要映射的实体类的类型 ...

  4. resultMap自定义映射(多对一)

    自定义resultMap,处理复杂的表关系,实现高级结果集映射 1) id :用于完成主键值的映射 2) result :用于完成普通列的映射 3) association :一个复杂的类型关联;许多 ...

  5. mybatis:自定义映射关系resultMap

    创建表t_emp 定义实体类 package org.example.entity; public class Emp { private Integer empId; private String ...

  6. MyBatis的类型自定义映射

    背景 利用MyBatis将数据库的时间类型映射成Java8的时间类型,引申对不同类型的自定义映射 实现方法 1.实现MyBatis中TypeHandler接口 @MappedTypes(value = ...

  7. 学习windows编程 day4 之 自定义映射

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  8. Elasticsearch 自定义映射

    尽管在很多情况下基本域数据类型 已经够用,但你经常需要为单独域自定义映射 ,特别是字符串域.自定义映射允许你执行下面的操作: 全文字符串域和精确值字符串域的区别 使用特定语言分析器 优化域以适应部分匹 ...

  9. resultMap自定义映射---8.3.1. 解决列名(表中的字段名称)和实体类中的属性名不一致

    1.1.1.1.      步骤一:将驼峰匹配注释掉 --------------测试完成后仍然 回来开启  其他地方可能用到 一旦注释掉驼峰匹配,那么再通过queryUserById查询的结果中,用 ...

  10. MyBatis(七) 自定义映射结果ResultMap

    (1)接口中对应的方法 public Emp getEmpById(Integer id); (2)Mapper文件 <resultMap type="com.eu.bean.Emp& ...

随机推荐

  1. 使用 screw(螺丝钉) 快速生成数据库文档

    一.框架介绍 回想起那个去年的7月份,第一份实习,组长让我写一份金蝶云的SQL文档,当时一看2000多张表,当时就猛吸一根烟,然后去gitee看看有没有好的框架快速生成 SQL 文档 ,由此找到了 s ...

  2. python之爬虫三

    20xpath入门 在编写爬虫程序的过程中提取信息是非常重要的环节,但是有时使用正则表达式无法匹配到想要的信息,或者书写起来非常麻烦,此时就需要用另外一种数据解析方法,也就是本节要介绍的 Xpath ...

  3. 碉堡!“万物皆可分”标记模型上线「GitHub 热点速览」

    这周有个让人眼前一亮的图像识别模型 segment-anything,它能精细地框出所有可见物体,它标记出的物体边界线清晰可见.如此出色的模型,自然获得了不少人的赞赏,开源没几天,就拿下了 18k+ ...

  4. MySQL(九)InnoDB行格式

    InnoDB行格式 查看默认行格式: select @@innodb_default_row_format; 查看数据库表使用的行格式 mysql> use atguigudb; Reading ...

  5. 【译】ConfigureAwait FAQ

    .NET 在数年前就在语言和库中添加了 async/await.在那段时间里,它像野火一样蔓延开来,不仅在 .NET 生态系统中,而且在无数其他语言和框架中被复制.在 .NET 中也看到了大量的改进, ...

  6. extend笔记

    JavaScript面向对象 继承extend 1. 概念(主要用途) 将子类中的共性代码 ( 属性和方法 ) 抽取出来 放到父类中 每当有一个新的子类需要用到共性的属性或者方法时 不需要在自己内容复 ...

  7. ChatGPT在工业领域的研究与应用探索-数据与工况认知

    1.      ChatGPT发展现状... 2 2.      ChatGPT如何与工业相结合... 2 3.      ChatGPT在工业领域的研究与应用... 3 1.   ChatGPT发展 ...

  8. [OpenCV-Python] 22 直方图

    文章目录 OpenCV-Python:IV OpenCV中的图像处理 22 直方图 22.1 直方图的计算,绘制与分析 22.1.1 统计直方图 22.1.2 绘制直方图 22.1.3 使用掩模 22 ...

  9. API 扫盲贴,8分钟快速搞懂 API 框架

    API(应用程序编程接口)是一种传递信息和指令的工具,它通过不同的功能和协议等手段,允许不同的软件或系统之间进行通信和交互.作为程序员或开发人员,API 是你日常工作中必不可少的组成部分.在本文中,我 ...

  10. 云原生时代崛起的编程语言Go并发编程实战

    @ 目录 概述 基础理论 并发原语 协程-Goroutine 通道-Channel 多路复用-Select 通道使用 超时-Timeout 非阻塞通道操作 关闭通道 通道迭代 定时器-TimerAnd ...