当前位置: 首页 > news >正文

自定义映射resultMap

自定义映射resultMap

自定义映射resultMap

  • 自定义映射resultMap
  • resultMap处理字段和属性的映射关系
    • 字段名和属性名不一致的情况,如何处理映射关系?
      • 1、为查询的字段设置别名,和属性名保持一致
      • 2、核心配置文件(mybatis-config.xml)中设置一个全局配置
      • 3、使用resultMap自定义映射处理
        • (1)处理多对一的映射关系:
          • ①级联方式处理
          • ②association
          • ③分步查询
        • (2)处理一对多的映射关系:


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

字段名和属性名不一致的情况,如何处理映射关系?

属性名
在这里插入图片描述
字段名
在这里插入图片描述

1、为查询的字段设置别名,和属性名保持一致

在这里插入图片描述

    <select id="getEmpByEmpId" resultType="Emp">select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}</select>

2、核心配置文件(mybatis-config.xml)中设置一个全局配置

当字段符合MySQL的要求使用_,而属性符合java的要求使用驼峰
,此时可以在MyBatis的核心配置文件(mybatis-config.xml)中设置一个全局配置,可以自动将下划线映射为驼峰
emp_id:empId,emp_name:empName

    <settings><!--将下划线映射为驼峰--><setting name="mapUnderscoreToCamelCase" value="true"/><!--开启延迟加载--><setting name="lazyLoadingEnabled" value="true"/><!--按需加载--><setting name="aggressiveLazyLoading" value="false"/></settings>

3、使用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 like
concat('%',#{mohu},'%')
</select>
(1)处理多对一的映射关系:

多个员工对应一个部门

①级联方式处理
    <!--  级联方式处理--><resultMap id="empAndDeptResultMapOne" 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="empAndDeptResultMapOne">selectt_emp.*,t_dept.*from t_empleft join t_depton t_emp.dept_id = t_dept.dept_idwhere t_emp.emp_id = #{empId}</select>
②association
    <!--association--><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:设置需要处理映射关系的属性的属性名javaType:设置要处理的属性的类型--><association property="dept" javaType="Dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result></association></resultMap><select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">selectt_emp.*,t_dept.*from t_empleft join t_depton t_emp.dept_id = t_dept.dept_idwhere t_emp.emp_id = #{empId}</select>
③分步查询

在这里插入图片描述
DeptMapper.xml(部门):

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

EmpMapper.xml(员工):

<!--分步查询--><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><!--property:设置需要处理映射关系的属性的属性名select:设置分步查询的sql的唯一标识column:将查询出的某个字段作为分步查询的sql的条件fetchType:在开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载fetchType="eager(立即加载)|lazy(延迟加载)"--><association property="dept" fetchType="eager"select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="dept_id"></association></resultMap><select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">select * from t_emp where emp_id = #{empId}</select>
(2)处理一对多的映射关系:

一个部门对应多个员工
①collection

    <resultMap id="deptAndEmpResultMap" type="Dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result><!--ofType:设置集合类型的属性中存储的数据的类型--><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><select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">SELECT *FROM t_deptLEFT JOIN t_empON t_dept.dept_id = t_emp.dept_idWHERE t_dept.dept_id = #{deptId}</select>

②分步查询
DeptMapper.xml(部门):

    <resultMap id="deptAndEmpResultMapByStep" type="Dept"><id column="dept_id" property="deptId"></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("deptId") Integer deptId);--><select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep">select * from t_dept where dept_id = #{deptId}</select>

EmpMapper.xml(员工):

    <select id="getDeptAndEmpByStepTwo" resultType="Emp">select * from t_emp where dept_id = #{deptId}</select>
http://www.lryc.cn/news/174965.html

相关文章:

  • Android修行手册 - Android Studio去掉方法参数提示、变量类型提示、方法引用Usage提示
  • 【车载开发系列】ECU Application Software程序刷新步骤
  • inject和provide的使用
  • 2023年中国研究生数学建模竞赛D题
  • Unity制作曲线进度条
  • 面试:C++ 11 智能指针
  • 设计模式——3. 抽象工厂模式
  • vscode 无法使用 compilerPath“D:.../bin/arm-none-eabi-g++.exe”解析配置。
  • Vue.js入门模板语法[上] 及Vue.js实现购物车---详细讲解
  • windows下gvim的配置
  • 基于复旦微的FMQL45T900全国产化ARM开发开发套件(核心板+底板)
  • Leetcode Top100(23)环形链表
  • 线性代数基础-行列式
  • RT-Thread(学习)
  • 【MySQL】 MySQL 死锁问题分析优化器特性及优化方案
  • 【C++面向对象侯捷】8.栈,堆和内存管理
  • 在比特币上使用可检索性证明支付存储费用
  • 使用SSE(Server-Sent Events)实现服务端给客户端发消息
  • 【Redis】使用rpm包安装redis
  • 论文阅读-Group-based Fraud Detection Network on e-Commerce Platforms
  • java程序启动时指定JVM内存参数和Xms、Xmx参数学习
  • 【C++编程能力提升】
  • FlashDuty Changelog 2023-09-21 | 自定义字段和开发者中心
  • 贪心算法-
  • 漫谈:C语言 C++ 左值、右值、类型转换
  • 前车之鉴,后车之师
  • WEB使用VUE3实现地图导航跳转
  • 今天聊一聊高性能系统架构设计是什么样的
  • 鼠标不动了怎么办?3招解决问题!
  • 2023-09-23力扣每日一题