ResultMap的使用

在Mybatis中,可以使用resultMap(结果集映射)作为sql的返回类型

一般用来解决如下问题:

  • 数据库表字段名和实体类属性名不一致的问题;
  • 多对一问题:
    • 例如:多个学生对应同一个老师,查询每个学生信息(包含老师对象属性)
  • 一对多问题:
    • 例如:一个老师教学多个学生,查询某个老师信息及其属下学生(包含学生列表)

1、字段名和属性名不一致问题

数据库表字段名和实体类属性名不一致的问题处理

id	name	pwd	//数据库字段名
id name password //实体类属性名

Mapper中SQL语句编写如下:

<!--结果集映射-->
<resultMap id="UserMap" type="User">
<!--column为数据库中的字段名,property为实体中的属性-->
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap> <select id="getUserById" resultType="UserMap">
select * from mybatis.user where id=#{id};
</select>

2、多对一处理

获得所有学生及其对应老师(多个学生对应一个老师)

Student类

@Data                              //get,set
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Student{
int id;
String name;
Teacher teacher;
}

Teacher类

@Data                              //get,set
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Teacher{
int id;
String name; public Teacher() {
} public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
}

按照结果集嵌套处理

联表查询

<select id="getStudents" resultMap="StudentMap">
select s.id sid, s.name sname, t.name tname
from student s
join teacher t
on s.tid = t.id
</select>
<resultMap id="StudentMap" type="Student">
<!--这里的column是与查询结果的字段名对应,字段重命名了则对应命名后的字段-->
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<!--复杂的属性需要单独处理 对象:association 集合:collection-->
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>

按照查询嵌套处理

相当于子查询:

<select id="getStudent2" resultMap="StudentMap2">
select * from student;
</select> <select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{tid};
</select> <!--这里将学生和对应老师分开查询,将查询结果组合-->
<resultMap id="StudentMap2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>

查询结果:

Student{id=1, name='zhangsan', teacher=Teacher{id=1, name='spong'}}
Student{id=2, name='lisi', teacher=Teacher{id=1, name='spong'}}

3、一对多处理

按指定ID查询老师及其所管理的学生(一个老师对应多个学生)

Student类

@Data                              //get,set
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Student {
int id;
String name;
int tid; public Student() {
} public Student(int id, String name, int tid) {
this.id = id;
this.name = name;
this.tid = tid;
}
}

Teacher类

@Data                              //get,set
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Teacher {
int id;
String name;
List<Student> students;
}

按照结果嵌套处理

<select id="getTeacherById" resultMap="TeacherById">
select t.id id, t.name tname, s.id sid,s.name sname,s.tid tid
from teacher t
join student s
on t.id = s.tid;
</select> <resultMap id="TeacherById" type="Teacher">
<result property="id" column="id"/>
<result property="name" column="tname"/>
<!--获取List<Student>中的泛型使用 ofType-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>

查询结果:

[Teacher{id=1, name='spong', students=[Student{id=1, name='zhangsan', tid=1}, Student{id=2, name='lisi', tid=1}]}]

小结:

  • 关联:association【多对一】
  • 集合:collection【一对多】
  • javaType & ofType
    • javaType 用来指定实体类中属性的类型
    • ofType 用来指定映射到List或者集合中pojo类型,即泛型的类型

Mybatis——ResultMap(结果集映射)的使用的更多相关文章

  1. Mybatis学习笔记-ResultMap结果集映射

    解决属性名与字段名不一致的问题 新建项目 --> 测试实体类字段不一致的情况 数据库字段:id,name,pwd 实体类属性:id,name,password 输出结果 User{id=1, n ...

  2. mybatis百科-结果集映射类ResultMap

    目录 1 成员变量 2 构造函数 3 其他函数 3.1 setter 和 getter 函数 4 静态内部类 4.1 成员变量 4.2 构造函数 4.3 建造者相关的函数 4.4 获取配置的构造方法参 ...

  3. resultMap结果集映射解决属性名和字段不一致问题

    解决属性名和字段名不一致的问题 1.出现的问题 数据库中的字段 ​ 新建一个项目,拷贝之前的,测试实体类与数据库字段不一致的情况 public class User { private int id; ...

  4. mybatis resultmap标签type属性什么意思

    mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...

  5. mybatis ResultMap详解

    前言 MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或BCNF,但实际事与愿违,那么结果集映射就是MyBatis为我们提供这种理想与现实间转换的手段了,而res ...

  6. mybatis - resultMap

    resultMap有比较强大的自动映射,下面是摘自mybatis中文官网的的片段: 当自动映射查询结果时,MyBatis会获取sql返回的列名并在java类中查找相同名字的属性(忽略大小写). 这意味 ...

  7. mybatis resultMap映射学习笔记

    这几天,百度mybatis突然看不到官网了,不知道百度怎么整的.特此贴出mybatis中文官网: http://www.mybatis.org/mybatis-3/zh/index.html 一个学习 ...

  8. Mybatis resultMap空值映射问题解决

    Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段,例如 查询 name,sex,age,数据库中的 ...

  9. ibatis resultMap 结果集映射

    1.结果集映射 就是将返回的记录,逐个字段映射到java对象上:如果数据库字段与java对象的成员变量名对应的话,则使用resultClas即可 2.实现 结合 ibatis初探这篇文章中提到的pro ...

随机推荐

  1. Python Ethical Hacking - ARP Spoofing

    Typical Network ARP Spoofing Why ARP Spoofing is possible: 1. Clients accept responses even if they ...

  2. C++语法小记---异常处理

    异常处理(C语言) 异常是对代码中可以预知的问题进行处理:代码中不可以预知的问题叫Bug: if () { ... } else { ... } setjmp和longjmp #include < ...

  3. “Python的单例模式有四种写法,你知道么?”——孔乙己

    什么是单例模式 单例模式(Singleton Pattern)是最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该类负责创建自己的 ...

  4. 题解 洛谷 P4569 【[BJWC2011]禁忌】

    考虑用\(AC\)自动机来解决本题这样的多字符串匹配问题. 要最大化魔法分割后得到的禁忌串数目,最优情况肯定为在一个串中每个禁忌串的右端点进行分割.对应到\(AC\)自动机上,就是匹配到一个禁忌串后, ...

  5. APP自动化 -- MobileBy

    一.BobileBy源码 selenium中有 By appium就有MobileBy. 二.MobileBy示例 MobileBy就是继承的By,所以,语法基本是一样的.

  6. 跟老刘学运维day01~谈红帽系统

    第0章 谈红帽系统 1.Linux,是一套免费使用和自由传播的类Unix操作系统,其源代码完全开源: 开源:==>将程序与程序的源代码一起提供给用户的服务模式. 开源四大特点:低风险.高品质.低 ...

  7. 图像增强 | CLAHE 限制对比度自适应直方图均衡化

    1 基本概述 CLAHE是一个比较有意思的图像增强的方法,主要用在医学图像上面.之前的比赛中,用到了这个,但是对其算法原理不甚了解.在这里做一个复盘. CLAHE起到的作用简单来说就是增强图像的对比度 ...

  8. 构建私有的verdaccio npm服务

    用了很长一段时间的cnpmjs做库私有库,发现两个问题 1. 最开始是mysql对表情emoij的支持不好,但由于数据库没办法调整所以只好把第三方库都清掉,只留私有库 2. mac 上面cnpm in ...

  9. 让内层浮动的Div将外层Div撑开 -----清浮动

    清浮动的好处写多了都能体会到,解决高度塌陷, 一般情况下是要清除浮动的,不然会影响下面标签的排版. <div class="parent" style="width ...

  10. 使用@ControllerAdvice处理异常

    在Spring 3.2中,新增了@ControllerAdvice.@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...