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

如果字段名与实体类中的属性名不一致,该如何处理映射关系?

  • 第一种方法:为查询的字段设置别名,和属性名保持一致

    下面是实体类中的属性名:

      private Integer empId;
    private String empName;
    private Integer age;
    private String gender;

    这是建表时设置的字段名:

    emp_id    emp_name    age    gender

    我们只需要在Mapper.xml中在写sql语句时,对字段名进行设置别名,使得与属性名一致:

     select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}
  • 第二种方法:当字段符合Mysql要求使用下划线,而属性名符合Java要求使用驼峰,此时可以在Mybatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,就可以在查询表中数据时,自动将下划线类型的字段名转换为驼峰。

     <settings>
    <!--将下划线映射为驼峰-->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
  • 第三种方法:使用resultMap处理

        <!--
    resultMap:设置自定义的映射关系
    id:唯一标识
    type:处理映射关系的实体类的类型
    常用标签:
    id:处理主键和实体类中属性的映射关系
    result:处理普通字段和实体类中属性的映射关系
    column:设置映射关系中的字段名,必须是sql查询出的某个字段
    property:设置映射关系中的属性的属性名,必须是处理实体类型类型中的属性名
    --> <resultMap id="empResultMap" 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>
    </resultMap>

    <!-- Emp getEmpByEmpId(@Param("empId") Integer emId);--> <select id="getEmpByEmpId" resultMap="empResultMap">
    select * from t_emp where emp_id = #{empId}
    </select>

多对一的映射关系

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

当Emp实体类中具有Dept对象,但是字段中不存在这个属性,我们需要将Dept对象中的属性与查询的字段名建立映射关系。

    <resultMap id="empAndDeptResultMap" 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="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
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.使用association处理映射关系

  • association:处理多对一的映射关系(处理实体类类型的属性)
  • property:设置需要处理映射关系的属性的属性名
  • javaType:设置要处理的属性的类型
    <resultMap id="empAndDeptResultMap" 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">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>

3.分步查询

  • 首先查询员工的信息

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

    <resultMap id="empAndDeptByStepResultMap" 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>
    <!--
    select:设置分步查询,查询某个属性的值的sql标识(namespace.sqlId)
    column:将sql以及查询结果中的某个字段设置为分步查询的条件
    -->
    <association property="dept"
    select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
    column="dept_id"></association>
    </resultMap>
    <!-- Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
    select * from t_emp where emp_id = #{empId}
    </select>
  • 根据员工所对应的部门id查询部门信息

        /**
    * 分步查询第二步:根据员工所对应的id查询部门信息
    * @param deptId
    * @return
    */
    Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
       <!-- Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
    select * from t_dept where depy_id = #{deptId}
    </select>
  • 分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:

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

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

    此时就可以实现按需加载,获取的数据是什么,就会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载。


一对多的映射关系

1.collection

    /**
* 根据部门id查部门中员工的信息
* @param deptId
* @return
*/
Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);
   <resultMap id="deptAndEmpResultMap" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
<!--
ofType:设置collection标签所处理的集合属性中存储数据的类型
-->
<collection property="emps" ofType="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>
</collection>
</resultMap>
 <!--Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
select *
from t_dept
LEFT JOIN t_emp
ON t_dept.dept_id = t_emp.dept_id
WHERE t_dept.dept_id = #{deptId};
</select>

2.分步查询

  • 查询部门信息

        /**
    * 分步查询部门以及部门中的员工信息第一步
    * @param id
    * @return
    */
    Dept getDeptAndEmpByStepOne(@Param("id") Integer id);
      <resultMap id="deptAnEmpResultMapByStep" type="Dept">
    <id column="dept_id" property="depyId"></id>
    <result column="dept_name" property="deptName"></result>
    <collection property="emps"
    select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
    column="dept_id"></collection>
    </resultMap>
        <!--    Dept getDeptAndEmpByStepOne(@Param("id") Integer id);-->
    <select id="getDeptAndEmpByStepOne" resultMap="">
    select * from t_dept where dept_id = #{deptId}
    </select>
  • 根据部门id查询部门中的员工信息

        /**
    * 分步查询部门以及部门中的员工信息第二步
    * @param dept_id
    * @return
    */
    List<Emp> getDeptAndEmpByStepTwo(@Param("dept_id") Integer dept_id);
        <resultMap id="empAndDeptByStepResultMap" 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.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
    column="dept_id"></association>
    </resultMap>
        <!--List<Emp> getDeptAndEmpByStepTwo(@Param("dept_id") Integer dept_id);-->
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
    select * from t_emp where dept_id = #{deptId}
    </select>

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

  1. mybatis-自定义映射resultMap

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

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

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

  3. MyBatis的类型自定义映射

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

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

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

  5. Elasticsearch 自定义映射

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

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

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

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

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

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

    collection:处理一对多和多对多的关系 1) POJO中的属性可能会是一个集合对象,我们可以使用联合查询,并以级联属性的方式封装对象.使用collection标签定义对象的封装规则 publi ...

  9. Mybatis 高级结果映射 ResultMap Association Collection

    在阅读本文章时,先说几个mybatis中容易混淆的地方: 1. mybatis中的列不是数据库里的列而是查询里的列,可以是别名(如 select user_name as userName,这时col ...

随机推荐

  1. 简答一波 HashMap 常见八股面试题 —— 算法系列(2)

    请点赞,你的点赞对我意义重大,满足下我的虚荣心. Hi,我是小彭.本文已收录到 GitHub · Android-NoteBook 中.这里有 Android 进阶成长知识体系,有志同道合的朋友,关注 ...

  2. Redis 5 种基本数据结构(String、List、Hash、Set、Sorted Set)详解 | JavaGuide

    首发于:Redis 5 种基本数据结构详解 - JavaGuide 相关文章:Redis常见面试题总结(上) . Redis 5 种基本数据结构(String.List.Hash.Set.Sorted ...

  3. 基于Docker-compose搭建Redis高可用集群-哨兵模式(Redis-Sentinel)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_110 我们知道,Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3) ...

  4. Vue3:不常用的Composition API && Fragment、Teleport、Suspense && 与Vue2对比的一些变化

    1 # 一.Vue3不常用的Composition API 2 # 1.shallowReactive与shallowRef 3 .shallowReactive: 只处理对象最外层属性的响应式(浅响 ...

  5. JDBC与ODBC的区别

    JDBC简介JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,它是Java十三个规范之一.可以为多种关系数据库提供统一访 ...

  6. 提交代码的其他方式,不单单只有git

    1.  xftp提交代码至服务器,直接连接服务器(如果使用可以直接到官网下载一个试用版或者家庭教育版的,本人不推荐使用破解版毕竟是直接和公司服务器对接出问题不好交代) // https://www.n ...

  7. identity4 系列————开篇概念

    前言 identity4 其实是openid connection, 那么我们还听说过openid 还有 oauth 2.0 那么下面就介绍一下Oath 2.0和openid 还有 openid co ...

  8. 网站制作工具之EditPlus的使用

    这里分享网站制作教程所使用到的软件,我个人开发使用的是EditPlus和Dreamweaver这两款软件.在百度搜索一下这两个软件,安装好后就可以使用了. EditPlus的使用方法 EditPlus ...

  9. ZJU-199001 第三周练习 2 数字特征值 位运算算法

    题目 对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值. 对于一个整数, 从个位开始对每一位数字编号, 个位是 \(1\) 号, 十位是 \(2\) 号, 以此类推. 这个整数在第位上的数字 ...

  10. metasploit进行局域网远控

    用metasploit进行局域网远程控制 Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估,提供真正的安全 ...