Mybatis之自定义映射resultMap

本文涉及的产品
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
性能测试 PTS,5000VUM额度
简介: 【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的相关知识点,希望对你有所帮助。

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

目录
打赏
0
2
2
1
16
分享
相关文章
|
3天前
|
七、MyBatis自定义映射resultMap
七、MyBatis自定义映射resultMap
22 6
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
文章介绍了MyBatis的简单增删改查操作,包括创建数据表、实体类、配置文件、Mapper接口及其XML文件,并解释了`#{}`预编译参数和`@Param`注解的使用。同时,还涵盖了resultType与resultMap的区别,并提供了完整的代码实例和测试用例。
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
8、Mybatis-Plus 分页插件、自定义分页
这篇文章介绍了Mybatis-Plus的分页功能,包括如何配置分页插件、使用Mybatis-Plus提供的Page对象进行分页查询,以及如何在XML中自定义分页SQL。文章通过具体的代码示例和测试结果,展示了分页插件的使用和自定义分页的方法。
8、Mybatis-Plus 分页插件、自定义分页
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
84 2
|
5月前
|
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
149 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
mybatis使用二:springboot 整合 mybatis,创建开发环境
这篇文章介绍了如何在SpringBoot项目中整合Mybatis和MybatisGenerator,包括添加依赖、配置数据源、修改启动主类、编写Java代码,以及使用Postman进行接口测试。
104 0
mybatis使用二:springboot 整合 mybatis,创建开发环境
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
225 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
springBoot:后端解决跨域&Mybatis-Plus&SwaggerUI&代码生成器 (四)
本文介绍了后端解决跨域问题的方法及Mybatis-Plus的配置与使用。首先通过创建`CorsConfig`类并设置相关参数来实现跨域请求处理。接着,详细描述了如何引入Mybatis-Plus插件,包括配置`MybatisPlusConfig`类、定义Mapper接口以及Service层。此外,还展示了如何配置分页查询功能,并引入SwaggerUI进行API文档生成。最后,提供了代码生成器的配置示例,帮助快速生成项目所需的基础代码。
386 1

云原生

+关注
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等