8. 自定义映射resultMap
在 Mybatis 中,resultType 和 resultMap 都用于定义查询结果的映射关系。它们的使用场景如下:
- 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 适用于简单的查询操作,能够方便快捷地获取数据库表中的数据,并且使用起来比较简单。
- 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 的规则(使用驼峰)此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系:
- 可以通过为字段起别名的方式,保证和实体类中的属性名保持一致
- 可以在 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 中的延迟加载指的是在查询主体对象时,只查询主体对象的信息,而对于关联的属性或集合对象,直到访问这些对象时才会进行查询操作,以达到减少查询次数、提高性能的目的。
延迟加载的作用是:
- 减少查询次数:当需要查询的数据量较大时,可以通过延迟加载避免一次性查询所有的数据,大大降低数据库的负载和查询时间,提高查询效率。
- 避免冗余数据:当主体对象包含多个关联属性或集合对象时,如果一次性加载所有数据,有可能会产生冗余的查询数据,而使用延迟加载可避免这种情况的出现,节省资源。
- 提升性能:当关联对象比较复杂时,一次性加载所有数据可能会显著降低程序的运行效率,使用延迟加载技术可以避免这种问题,并提高程序的运行速度。
延迟加载在 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的更多相关文章
- MyBatis_06(自定义映射resultMap)
主题:自定义映射resultMap "自定义映射resultMap",可以解决什么问题: 1-"属性" 和 "字段名" 不一致的情况 2-& ...
- 自定义映射resultMap
resultMap处理字段和属性的映射关系 如果字段名与实体类中的属性名不一致,该如何处理映射关系? 第一种方法:为查询的字段设置别名,和属性名保持一致 下面是实体类中的属性名: private In ...
- mybatis-自定义映射resultMap
自定义映射resultMap resultMap处理字段和属性的映射关系 resultMap:设置自定义映射 属性: id:表示自定义映射的唯一标识,不能重复 type:查询的数据要映射的实体类的类型 ...
- resultMap自定义映射(多对一)
自定义resultMap,处理复杂的表关系,实现高级结果集映射 1) id :用于完成主键值的映射 2) result :用于完成普通列的映射 3) association :一个复杂的类型关联;许多 ...
- mybatis:自定义映射关系resultMap
创建表t_emp 定义实体类 package org.example.entity; public class Emp { private Integer empId; private String ...
- MyBatis的类型自定义映射
背景 利用MyBatis将数据库的时间类型映射成Java8的时间类型,引申对不同类型的自定义映射 实现方法 1.实现MyBatis中TypeHandler接口 @MappedTypes(value = ...
- 学习windows编程 day4 之 自定义映射
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...
- Elasticsearch 自定义映射
尽管在很多情况下基本域数据类型 已经够用,但你经常需要为单独域自定义映射 ,特别是字符串域.自定义映射允许你执行下面的操作: 全文字符串域和精确值字符串域的区别 使用特定语言分析器 优化域以适应部分匹 ...
- resultMap自定义映射---8.3.1. 解决列名(表中的字段名称)和实体类中的属性名不一致
1.1.1.1. 步骤一:将驼峰匹配注释掉 --------------测试完成后仍然 回来开启 其他地方可能用到 一旦注释掉驼峰匹配,那么再通过queryUserById查询的结果中,用 ...
- MyBatis(七) 自定义映射结果ResultMap
(1)接口中对应的方法 public Emp getEmpById(Integer id); (2)Mapper文件 <resultMap type="com.eu.bean.Emp& ...
随机推荐
- 在k8s上安装Harbor
在k8s上安装Harbor 先前条件 <kubernetes(k8s) 存储动态挂载><在k8s(kubernetes)上安装 ingress V1.1.3> 参考我之前的文档 ...
- day05-SpringCloud Eureka-服务注册与发现02
SpringCloud Eureka-服务注册与发现02 3.搭建EurekaServer集群-实现负载均衡&故障容错 3.1为什么需要集群EurekaServer? 微服务RPC远程服务调用 ...
- 一道名题-(csp 儒略日)的心得与技巧
引: 如果你见到一个oi对着 4713,1582 146097 2299160 颠颠地笑,不用怀疑,他是在做那道名题--<csp-s2020 T1 儒略日> 这道题,我做了三年,平均每年做 ...
- LeeCode 动态规划(三)
完全背包问题 题目描述 有 n 件物品和容量为 w 的背包,给你两个数组 weights 和 values,分别表示第 i 件物品的重量和价值,每件物品可以放入多次,求解将哪些物品装入背包可使得物品价 ...
- TS(二)内置对象与class类
内置对象 ECMA内置对象 Boolean.Number.String.RegExp.Date.Error const regexp:RegExp = /\w\d\s const number:Num ...
- javasec(一)java反射
这篇文章介绍javasec基础知识--java反射. 0x01 反射是什么? 反射是一种机制,利用反射机制动态的实例化对象.读写属性.调用方法.构造函数. 在程序运行状态中,对于任意一个类或对象,都能 ...
- [aac @ 0x1dd24c0] Input contains NaN/+-Inf
ffmpeg编码pcm为aac时报错:[aac @ 0x1dd24c0] Input contains NaN/+-Inf 在./configure选项后面加上下列选项就可以 --enable-enc ...
- linux发行版中的i386/i686/x86-64/的区别
在yum上找32位的i386找不到,看到i686以为是64位呢,原来它也是32位啊 i686 只是i386的一个子集,支持的cpu从Pentium 2 (686)开始,之前的型号不支持. 备注: 1. ...
- 【性能】JDK和Jmeter的安装与配置
一.JDK环境配置 1. 下载JDK 官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downl ...
- dp杂题选做
树的数量 题目其实挺简单的,难点在于状态的设计(其实也没多难). 令 \(f_i\) 表示 \(i\) 个点的 \(m\) 叉树的数量,发现无法转移.设 \(g_{i,j}\) 表示根节点所在子树内有 ...