【MyBatis】自定义映射resultMap

article/2025/10/7 8:13:24

目录

1.使用全局配置处理字段名和属性名不一致的情况

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

1.处理一对一映射

2.多对一映射处理

级联方式处理映射关系

使用association处理映射关系

分步查询

3.一对多映射处理

1.collection

2.分步查询


前期准备:

 建立 t_emp 表,并添加数据:

 建立 t_dept 表,并添加数据:

实体类:Emp

package com.atguigu.mybatis.pojo;public class Emp {private Integer empId;private String empName;private Integer age;private String gender;private Dept dept;public Emp() {}public Emp(Integer empId, String empName, Integer age, String gender, Dept dept) {this.empId = empId;this.empName = empName;this.age = age;this.gender = gender;this.dept = dept;}public Integer getEmpId() {return empId;}public void setEmpId(Integer empId) {this.empId = empId;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Dept getDept() {return dept;}public void setDept(Dept dept) {this.dept = dept;}@Overridepublic String toString() {return "Emp{" +"empId=" + empId +", empName='" + empName + '\'' +", age=" + age +", gender='" + gender + '\'' +", dept=" + dept +'}';}
}

实体类:Dept

package com.atguigu.mybatis.pojo;public class Dept {private Integer deptId;private String deptName;public Dept() {}public Dept(Integer deptId, String deptName) {this.deptId = deptId;this.deptName = deptName;}public Integer getDeptId() {return deptId;}public void setDeptId(Integer deptId) {this.deptId = deptId;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName = deptName;}@Overridepublic String toString() {return "Dept{" +"deptId=" + deptId +", deptName='" + deptName + '\'' +'}';}
}

  接口:

public interface EmpMapper {Emp getEmpById(@Param("empId") Integer empId);
}

SQL:

select * from t_emp where emp_id = #{empId};

测试:

   public void test(){SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();SqlSession sqlSession = sqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp empById = mapper.getEmpById(1);System.out.println(empById.toString());}

运行结果:

原因:JAVA中属性名 与 数据库中字段名不一致

1.使用全局配置处理字段名和属性名不一致的情况

若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性
名符合Java的规则(使用驼峰)
此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系:
1.可以通过为字段起别名的方式,保证和实体类中的属性名保持一致
select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId};

2.可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可
以在查询表中数据时,自动将_类型的字段名转换为驼峰
  <settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings>

例如:字段名user_name,设置了mapUnderscoreToCamelCase,此时字段名就会转换为
userName

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

1.处理一对一映射

<resultMap id="empResultMap" type="Emp"><id property="empId" column="emp_id"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="gender" column="gender"></result>
</resultMap><select id="getEmpById" resultMap="empResultMap">select * from t_emp where emp_id = #{empId};</select>
resultMap:设置自定义映射
  • 属性:
  • id:表示自定义映射的唯一标识
  • type:查询的数据要映射的实体类的类型
  • 子标签:
  • id:设置主键的映射关系
  • result:设置普通字段的映射关系
  • association :设置多对一的映射关系
  • collection:设置一对多的映射关系
  • 属性:
  • property:设置映射关系中实体类中的属性名
  • column:设置映射关系中表中的字段名

2.多对一映射处理

级联方式处理映射关系

  <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><!--部门中的字段dept_id与Emp实体类中的属性dept中的deptId相对应 --><!--部门中的字段dept_name与Emp实体类中的属性dept中的deptName相对应 --><result column="dept_id" property="dept.deptId"></result><result column="dept_name" property="dept.deptName"></result></resultMap><select id="getEmpAndDeptById" resultMap="empAndDeptResultMap">SELECT t_emp.*,t_dept.* FROM t_empLEFT JOIN t_deptON t_emp.dept_id=t_dept.dept_idwhere t_emp.emp_id=#{empId}</select>

接口:

Emp getEmpAndDeptById(@Param("empId") Integer empId);

测试:

    public void test2(){SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();SqlSession sqlSession = sqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp empAndDeptById = mapper.getEmpAndDeptById(1);System.out.println(empAndDeptById);}

使用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="dept" javaType="Dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result></association></resultMap>

分步查询

①查询员工信息

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

测试:

    public void test3(){SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();SqlSession sqlSession = sqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp empAndDeptByStep = mapper.getEmpAndDeptByStep(1);System.out.println(empAndDeptByStep);}

分步查询的优点:可以实现延迟加载
但是必须在核心配置文件中设置全局配置信息:
  • lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
  • aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载
此时就可以实现按需加载,获取的数据是什么,就只会执行相应的 sql
此时可通   association和 collection 中的   fetchType 属性设置当前的分步查询是否使用延迟加载, fetchType="lazy(延迟加载)   |  eager(立即加载)"

 测试:

    public void test3(){SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();SqlSession sqlSession = sqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp empAndDeptByStep = mapper.getEmpAndDeptByStep(1);System.out.println(empAndDeptByStep.getEmpName());}

未开启延迟加载(执行了2次SQL): 

开启延迟加载:

    <settings><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings>

 执行了1次SQL:

3.一对多映射处理

1.collection

接口:

Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);

 实体类中,添加 emps集合:

 DeptMapper.xml:

collection :设置一对多的映射关系
ofType :设置 collection 标签所处理的集合属性中存储数据的类型
    <resultMap id="DeptAndEmpByDeptIdResultMap" type="Dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result><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="DeptAndEmpByDeptIdResultMap">SELECT t_emp.*,t_dept.* FROM t_deptLEFT JOIN t_empON t_emp.dept_id=t_dept.dept_idWHERE t_dept.dept_id=#{deptId}</select>

测试:

    public void test4(){SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();SqlSession sqlSession = sqlSessionUtils.getSqlSession();DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);Dept deptAndEmpByDeptId = mapper.getDeptAndEmpByDeptId(1);System.out.println(deptAndEmpByDeptId);}

运行结果: 

2.分步查询

①查询部门信息
    Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);

    <resultMap id="DeptAndEmpByStepResultMap" 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><select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepResultMap">select * from t_dept where dept_id=#{deptId}</select>

②根据部门 id 查询部门中的所有员工
    List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">select * from t_emp where dept_id=#{deptId}</select>

测试:

    public void test5(){SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();SqlSession sqlSession = sqlSessionUtils.getSqlSession();DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);Dept deptAndEmpByStepOne = mapper.getDeptAndEmpByStepOne(1);System.out.println(deptAndEmpByStepOne);}


http://chatgpt.dhexx.cn/article/kIelu2in.shtml

相关文章

浅谈MyBatis中的resultMap(个人总结)

官方文档&#xff1a; mybatis – MyBatis 3 | XML 映射器https://mybatis.org/mybatis-3/zh/sqlmap-xml.html“resultMap 元素是 MyBatis 中最重要最强大的元素......ResultMap 的设计思想是&#xff0c;对简单的语句做到零配置&#xff0c;对于复杂一点的语句&#…

resultMap和resultType的使用区别

如今从事编程工作也大半年了&#xff0c;还是弄不明白mybtis中resultMap和resultType到底怎么使用&#xff0c;看了众多资料&#xff0c;研究了公司和git上不少代码&#xff0c;发现了一些区别 在实体类中定义好了数据库字段&#xff0c;就可以不用resultMap&#xff0c;直接用…

mybatis的resultMap用法

目录 1.前言 2.准备工作 2.1 类型别名 2.2 列名和属性名 2.3 mybatis映射文件处理特殊字符 2.4 sql片段 2.5 mybatis完成模糊查询 3.结果映射&#xff08;resultMap&#xff09; 4. 联表查询 4.1 一对一关联association 4.2 一对多关联collection 5.鉴别器 1.前言 …

mybatis中resultMap的理解

resultMap 元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来, 并在一些情形下允许你做一些 JDBC 不支持的事情。 实际上&#xff0c;在对复杂语句进行联合映射的时候&#xff0c;它很可能可以代替数千行的同等功能的代码。 R…

resultmap的用法

MyBatis中在查询进行select映射的时候&#xff0c;返回类型可以用resultType&#xff0c;也可以用resultMap&#xff0c;resultType是直接表示返回类型的&#xff0c;而resultMap则是对外部ResultMap的引用&#xff0c;但是resultType跟resultMap不能同时存在。在MyBatis进行查…

mybatis中resultMap配置细则

关注公众号【江南一点雨】&#xff0c;专注于 Spring Boot微服务以及前后端分离等全栈技术&#xff0c;定期视频教程分享&#xff0c;关注后回复 Java &#xff0c;领取松哥为你精心准备的 Java 干货&#xff01; resultMap算是mybatis映射器中最复杂的一个节点了&#xff0c;能…

深度解析resultMap标签

一般来说&#xff0c;一张表对应一份mapper映射文件&#xff0c;但部分属性在数据库中的存储并非直接存储其内容&#xff0c;而是存储其唯一标识&#xff0c;比如id&#xff0c;如下表中的员工政治面貌、部门、职级、岗位均是通过各个分表的id进行维护&#xff1a; 因此&#x…

ResultType和ResultMap的区别

ResultType和ResultMap都是执行查询语句时返回的结果集 ResultType ResultType相对与ResultMap而言更简单一点。只有满足ORM&#xff08;Object Relational Mapping&#xff0c;对象关系映射&#xff09;时&#xff0c;即数据库表中的字段名和实体类中的属性完全一致时&#x…

resultMap的用法以及关联结果集映射

resultType resultType可以把查询结果封装到pojo类型中&#xff0c;但必须pojo类的属性名和查询到的数据库表的字段名一致。 如果sql查询到的字段与pojo的属性名不一致&#xff0c;则需要使用resultMap将字段名和属性名对应起来&#xff0c;进行手动配置封装&#xff0c;将结…

[MyBatis]-resultMap结果映射集详解

resultMap结果映射集详解 resultmap是mybatis中最复杂的元素之一&#xff0c;它描述如何从结果集中加载对象&#xff0c;主要作用是定义映射规则、级联的更新、定制类型转化器。 resultmap构成元素 元素子元素作用constructoridArg 、arg用于配置构造器方法id将结果集标记为id…

MyBatis中ResultMap详解

简介&#xff1a; MyBatis的每一个查询映射的返回类型都是ResultMap&#xff0c;只是当我们提供的返回类型属性是resultType的时候&#xff0c;MyBatis会自动的给我们把对应的值赋给resultType所指定对象的属性&#xff0c;而当我们提供的返回类型是resultMap的时候&#xff0…

MyBatis多表查询resultMap设定

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 Result Maps 是什么 ResultMap 属性 一、Java数据访问接口 二、XML实现 1、*分步查询 延迟加载 三、Test 1.读入数据 总结 Result Maps 是什么 resultMap 元素是 M…

JS字符串替换指定字符

碰到一个后端数据同步过来的xml文本段&#xff0c;内容里写死的宽度 在1920*1080的分辨率屏幕上&#xff0c;显示有问题&#xff0c;由于数据量太多&#xff0c;前端单独拿数据再处理一遍 RegExp方法&#xff1a; function replaceAll(str, find, replace) {return str.repla…

shell内置字符串替换

shell变量赋值语法&#xff1a; 使用规则解释单引号所见即所得&#xff0c;即输出时会将单引号内的所有內容都原样输出&#xff0c;或者描述为单引号里面看到的是什么就会输出什么&#xff0c;这称为强引用双引号 (默认)输出双引号内的所有内容&#xff1b;如果内容中有命令(…

python字符串替换

str.replace(old, new[, max]) 参数 old – 将被替换的子字符串。 new – 新字符串&#xff0c;用于替换old子字符串。 max – 可选字符串, 替换不超过 max 次 返回值 返回字符串中的 old&#xff08;旧字符串&#xff09; 替换成 new(新字符串)后生成的新字符串&#xff0c;如…

C++ 字符串替换

好久没有更新了&#xff0c;最近有点忙&#xff0c;今天抽点时间给大家水 写一篇关于替换字符串字符的文章 替换函数 string类型的字符串可以用swap函数来替换字符&#xff0c;例如&#xff1a; 冒泡排序 #include <bits/stdc.h> using namespace std;int main () {i…

Python字符串替换方法replace

字符串替换方法replace str1 .replace(old_str&#xff0c;new_str&#xff0c;count)字符串的替换&#xff0c;将str1中的 old_str 替换成new_str old_str:将要被替换的字符串 new_str:新的字符串&#xff0c;替换成的字符串# count:替换的次数,默认是全部替换 返回值:得到一个…

Java字符串替换的方法

java中字符串替换方法主要有三种&#xff0c;分别是replace()、replaceAll()和replaceFirst()&#xff0c;这三种方法可以在三种不同情况应用&#xff0c;下面就由我来具体说明这三种方法的应用情况吧。 replace() replace的参数是char和CharSequence&#xff0c;即可以支持字…

sed 字符串替换

1. sed替换的基本语法为: sed s/原字符串/替换字符串/ 单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需要使用反斜线”\”进行转义。 2. 单引号” ‘ ’”是没有办法用反斜线”\”转义的,这时候只要把命令中的单引号改为双引号就行了,格式如下&#xff1a; # 要…

字符串的替换

一、字符串“打劫&#xff01;我有枪”&#xff0c;“枪”是敏感词汇&#xff0c;替换为“*”&#xff0c;并输出打印 二、字符串“北京欢迎你”&#xff0c;替换字符为“郑州欢迎你们”&#xff0c;并输出打印 package work.twelve;/* 一、字符串“打劫&#xff01;我有枪”…