Mybatis之自定义映射resultMap

本文涉及的产品
应用实时监控服务-可观测链路OpenTelemetry版,每月50GB免费额度
可观测可视化 Grafana 版,10个用户账号 1个月
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 【1月更文挑战第3天】一、resultMap处理字段和属性的映射关系二、多对一映射处理1、级联方式处理映射关系2、使用association处理映射关系3、分步查询1. 查询员工信息2. 查询部门信息三、一对多映射处理1、collection2、分步查询3. 查询部门信息4. 根据部门id查询部门中的所有员工四、延迟加载


学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您:

想系统/深入学习某技术知识点…

一个人摸索学习很难坚持,想组团高效学习…

想写博客但无从下手,急需写作干货注入能量…

热爱写作,愿意让自己成为更好的人…

文章目录

前言

一、resultMap处理字段和属性的映射关系

二、多对一映射处理

1、级联方式处理映射关系

2、使用association处理映射关系

3、分步查询

1. 查询员工信息

2. 查询部门信息

三、一对多映射处理

1、collection

2、分步查询

1. 查询部门信息

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

四、延迟加载

总结


前言

一、resultMap处理字段和属性的映射关系

二、多对一映射处理

1、级联方式处理映射关系

2、使用association处理映射关系

3、分步查询

  1. 查询员工信息
  2. 查询部门信息
    三、一对多映射处理
    1、collection
    2、分步查询
  3. 查询部门信息
  4. 根据部门id查询部门中的所有员工
    四、延迟加载

一、resultMap处理字段和属性的映射关系

  • resultMap:设置自定义映射
  • 属性:
  • id:表示自定义映射的唯一标识,不能重复
  • type:查询的数据要映射的实体类的类型
  • 子标签:
  • id:设置主键的映射关系
  • result:设置普通字段的映射关系
  • 子标签属性:
  • property:设置映射关系中实体类中的属性名
  • column:设置映射关系中表中的字段名
  • 若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射,即使字段名和属性名一致的属性也要映射,也就是全部属性都要列出来
<resultMapid="empResultMap"type="Emp"><idproperty="eid"column="eid"></id><resultproperty="empName"column="emp_name"></result><resultproperty="age"column="age"></result><resultproperty="sex"column="sex"></result><resultproperty="email"column="email"></result></resultMap><!--List<Emp>getAllEmp();--><selectid="getAllEmp"resultMap="empResultMap">select*fromt_emp</select>
  • 若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性名符合Java的规则(使用驼峰)。此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系
  1. 可以通过为字段起别名的方式,保证和实体类中的属性名保持一致
<!--List<Emp>getAllEmp();--><selectid="getAllEmp"resultType="Emp">selecteid,emp_nameempName,age,sex,emailfromt_emp</select>
  1. 可以在MyBatis的核心配置文件中的setting标签中,设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中数据时,自动将_类型的字段名转换为驼峰,例如:字段名user_name,设置了mapUnderscoreToCamelCase,此时字段名就会转换为userName。
    核心配置文件详解
<settings><settingname="mapUnderscoreToCamelCase"value="true"/></settings>

二、多对一映射处理

查询员工信息以及员工所对应的部门信息

publicclassEmp {  
privateIntegereid;  
privateStringempName;  
privateIntegerage;  
privateStringsex;  
privateStringemail;  
privateDeptdept;
//...构造器、get、set方法等}

1、级联方式处理映射关系

<resultMapid="empAndDeptResultMapOne"type="Emp"><idproperty="eid"column="eid"></id><resultproperty="empName"column="emp_name"></result><resultproperty="age"column="age"></result><resultproperty="sex"column="sex"></result><resultproperty="email"column="email"></result><resultproperty="dept.did"column="did"></result><resultproperty="dept.deptName"column="dept_name"></result></resultMap><!--EmpgetEmpAndDept(@Param("eid")Integereid);--><selectid="getEmpAndDept"resultMap="empAndDeptResultMapOne">select*fromt_empleftjoint_deptont_emp.eid=t_dept.didwheret_emp.eid=#{eid}
</select>

2、使用association处理映射关系

  • association:处理多对一的映射关系
  • property:需要处理多对的映射关系的属性名
  • javaType:该属性的类型
<resultMapid="empAndDeptResultMapTwo"type="Emp"><idproperty="eid"column="eid"></id><resultproperty="empName"column="emp_name"></result><resultproperty="age"column="age"></result><resultproperty="sex"column="sex"></result><resultproperty="email"column="email"></result><associationproperty="dept"javaType="Dept"><idproperty="did"column="did"></id><resultproperty="deptName"column="dept_name"></result></association></resultMap><!--EmpgetEmpAndDept(@Param("eid")Integereid);--><selectid="getEmpAndDept"resultMap="empAndDeptResultMapTwo">select*fromt_empleftjoint_deptont_emp.eid=t_dept.didwheret_emp.eid=#{eid}
</select>

3、分步查询

1. 查询员工信息

  • select:设置分布查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
  • column:设置分步查询的条件
//EmpMapper里的方法/*** 通过分步查询,员工及所对应的部门信息* 分步查询第一步:查询员工信息* @param  * @return com.atguigu.mybatis.pojo.Emp* @date 2022/2/27 20:17*/EmpgetEmpAndDeptByStepOne(@Param("eid") Integereid);
<resultMapid="empAndDeptByStepResultMap"type="Emp"><idproperty="eid"column="eid"></id><resultproperty="empName"column="emp_name"></result><resultproperty="age"column="age"></result><resultproperty="sex"column="sex"></result><resultproperty="email"column="email"></result><associationproperty="dept"select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"></association></resultMap><!--EmpgetEmpAndDeptByStepOne(@Param("eid") Integereid);--><selectid="getEmpAndDeptByStepOne"resultMap="empAndDeptByStepResultMap">select*fromt_empwhereeid=#{eid}
</select>

2. 查询部门信息

//DeptMapper里的方法/*** 通过分步查询,员工及所对应的部门信息* 分步查询第二步:通过did查询员工对应的部门信息* @param* @return com.atguigu.mybatis.pojo.Emp* @date 2022/2/27 20:23*/DeptgetEmpAndDeptByStepTwo(@Param("did") Integerdid);
<!--此处的resultMap仅是处理字段和属性的映射关系--><resultMapid="EmpAndDeptByStepTwoResultMap"type="Dept"><idproperty="did"column="did"></id><resultproperty="deptName"column="dept_name"></result></resultMap><!--DeptgetEmpAndDeptByStepTwo(@Param("did") Integerdid);--><selectid="getEmpAndDeptByStepTwo"resultMap="EmpAndDeptByStepTwoResultMap">select*fromt_deptwheredid=#{did}
</select>

三、一对多映射处理

publicclassDept {
privateIntegerdid;
privateStringdeptName;
privateList<Emp>emps;
//...构造器、get、set方法等}

1、collection

  • collection:用来处理一对多的映射关系
  • ofType:表示该属性对饮的集合中存储的数据的类型
<resultMapid="DeptAndEmpResultMap"type="Dept"><idproperty="did"column="did"></id><resultproperty="deptName"column="dept_name"></result><collectionproperty="emps"ofType="Emp"><idproperty="eid"column="eid"></id><resultproperty="empName"column="emp_name"></result><resultproperty="age"column="age"></result><resultproperty="sex"column="sex"></result><resultproperty="email"column="email"></result></collection></resultMap><!--DeptgetDeptAndEmp(@Param("did") Integerdid);--><selectid="getDeptAndEmp"resultMap="DeptAndEmpResultMap">select*fromt_deptleftjoint_empont_dept.did=t_emp.didwheret_dept.did=#{did}
</select>

2、分步查询

1. 查询部门信息

/*** 通过分步查询,查询部门及对应的所有员工信息* 分步查询第一步:查询部门信息* @param did * @return com.atguigu.mybatis.pojo.Dept* @date 2022/2/27 22:04*/DeptgetDeptAndEmpByStepOne(@Param("did") Integerdid);
<resultMapid="DeptAndEmpByStepOneResultMap"type="Dept"><idproperty="did"column="did"></id><resultproperty="deptName"column="dept_name"></result><collectionproperty="emps"select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"column="did"></collection></resultMap><!--DeptgetDeptAndEmpByStepOne(@Param("did") Integerdid);--><selectid="getDeptAndEmpByStepOne"resultMap="DeptAndEmpByStepOneResultMap">select*fromt_deptwheredid=#{did}
</select>

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

/*** 通过分步查询,查询部门及对应的所有员工信息* 分步查询第二步:根据部门id查询部门中的所有员工* @param did* @return java.util.List<com.atguigu.mybatis.pojo.Emp>* @date 2022/2/27 22:10*/List<Emp>getDeptAndEmpByStepTwo(@Param("did") Integerdid);
<!--List<Emp>getDeptAndEmpByStepTwo(@Param("did") Integerdid);--><selectid="getDeptAndEmpByStepTwo"resultType="Emp">select*fromt_empwheredid=#{did}
</select>

四、延迟加载

  • 分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:
  • lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
  • aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载
  • 此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType=“lazy(延迟加载)|eager(立即加载)”
<settings><!--开启延迟加载--><settingname="lazyLoadingEnabled"value="true"/></settings>
@TestpublicvoidgetEmpAndDeptByStepOne() {
SqlSessionsqlSession=SqlSessionUtils.getSqlSession();
EmpMappermapper=sqlSession.getMapper(EmpMapper.class);
Empemp=mapper.getEmpAndDeptByStepOne(1);
System.out.println(emp.getEmpName());
}
  • 关闭延迟加载,两条SQL语句都运行了
  • 开启延迟加载,只运行获取emp的SQL语句

@TestpublicvoidgetEmpAndDeptByStepOne() {
SqlSessionsqlSession=SqlSessionUtils.getSqlSession();
EmpMappermapper=sqlSession.getMapper(EmpMapper.class);
Empemp=mapper.getEmpAndDeptByStepOne(1);
System.out.println(emp.getEmpName());
System.out.println("----------------");
System.out.println(emp.getDept());
}
  • 开启后,需要用到查询dept的时候才会调用相应的SQL语句
  • fetchType:当开启了全局的延迟加载之后,可以通过该属性手动控制延迟加载的效果,fetchType=“lazy(延迟加载)|eager(立即加载)”
<resultMapid="empAndDeptByStepResultMap"type="Emp"><idproperty="eid"column="eid"></id><resultproperty="empName"column="emp_name"></result><resultproperty="age"column="age"></result><resultproperty="sex"column="sex"></result><resultproperty="email"column="email"></result><associationproperty="dept"select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"fetchType="lazy"></association></resultMap>

总结

以上就是Mybatis之自定义映射resultMap的相关知识点,希望对你有所帮助。

积跬步以至千里,积怠惰以至深渊。时代在这跟着你一起努力哦!

相关文章
|
17天前
|
SQL Java 数据库连接
MyBatis 的映射关系
MyBatis 核心功能之一是映射关系,支持一对一、一对多和多对多三种 ORM 映射。通过实体类与配置文件结合,开发者可灵活实现数据关联,提升数据库操作效率。
163 4
|
4月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
344 5
|
6月前
|
SQL XML Java
七、MyBatis自定义映射resultMap
七、MyBatis自定义映射resultMap
172 6
|
6月前
|
Java 数据库连接 mybatis
MyBatis篇-映射关系(1-1 1-n n-n)
本文介绍了MyBatis中四种常见关系映射的配置方法,包括一对一、一对多、多对一和多对多。**一对一**通过`resultMap`实现属性与字段的映射;**一对多**以用户-角色为例,使用`&lt;collection&gt;`标签关联集合数据;**多对一**以作者-博客为例,利用`&lt;association&gt;`实现关联;**多对多**则通过引入第三方类(如UserForDept)分别在User和Dept类中添加集合属性,并配置对应的`&lt;collection&gt;`标签完成映射。这些方法解决了复杂数据关系的处理问题,提升了开发效率。
|
9月前
|
XML Java 数据库连接
Mybatis映射关系
简介:本文介绍了MyBatis框架中四种常见的关系映射方式,包括一对一、一对多、多对一及多对多。一对一通过简单属性映射实现;一对多通过在主对象中添加集合属性并使用`&lt;collection&gt;`标签映射子对象集合;多对一则利用`&lt;association&gt;`标签在主对象中映射单个子对象;多对多需引入第三方类,分别在两个主对象中添加对方的集合属性,并通过`&lt;collection&gt;`标签实现映射。
167 32
|
3月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
608 1
Spring boot 使用mybatis generator 自动生成代码插件
|
6月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
539 0
|
8月前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
315 2
|
11月前
|
Java 数据库连接 Maven
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
450 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
|
11月前
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
343 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块