Mybatis——ResultMap(结果集映射)的使用
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(结果集映射)的使用的更多相关文章
- Mybatis学习笔记-ResultMap结果集映射
解决属性名与字段名不一致的问题 新建项目 --> 测试实体类字段不一致的情况 数据库字段:id,name,pwd 实体类属性:id,name,password 输出结果 User{id=1, n ...
- mybatis百科-结果集映射类ResultMap
目录 1 成员变量 2 构造函数 3 其他函数 3.1 setter 和 getter 函数 4 静态内部类 4.1 成员变量 4.2 构造函数 4.3 建造者相关的函数 4.4 获取配置的构造方法参 ...
- resultMap结果集映射解决属性名和字段不一致问题
解决属性名和字段名不一致的问题 1.出现的问题 数据库中的字段 新建一个项目,拷贝之前的,测试实体类与数据库字段不一致的情况 public class User { private int id; ...
- mybatis resultmap标签type属性什么意思
mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...
- mybatis ResultMap详解
前言 MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或BCNF,但实际事与愿违,那么结果集映射就是MyBatis为我们提供这种理想与现实间转换的手段了,而res ...
- mybatis - resultMap
resultMap有比较强大的自动映射,下面是摘自mybatis中文官网的的片段: 当自动映射查询结果时,MyBatis会获取sql返回的列名并在java类中查找相同名字的属性(忽略大小写). 这意味 ...
- mybatis resultMap映射学习笔记
这几天,百度mybatis突然看不到官网了,不知道百度怎么整的.特此贴出mybatis中文官网: http://www.mybatis.org/mybatis-3/zh/index.html 一个学习 ...
- Mybatis resultMap空值映射问题解决
Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段,例如 查询 name,sex,age,数据库中的 ...
- ibatis resultMap 结果集映射
1.结果集映射 就是将返回的记录,逐个字段映射到java对象上:如果数据库字段与java对象的成员变量名对应的话,则使用resultClas即可 2.实现 结合 ibatis初探这篇文章中提到的pro ...
随机推荐
- Ethical Hacking - NETWORK PENETRATION TESTING(1)
Pre--Connection-Attacks that can be done before connecting to the network. Gaining Access - How to b ...
- 牛客练习赛 66C公因子 题解
原题 原题 思路 考场想复杂了,搞到自闭-- 实际上,因为差值不变,我们可以先差分,求\(\gcd\),便得到答案(考场时想多了,想到了负数.正数各种复杂的处理,但是不需要),最后处理一下即可 代码 ...
- 【思维+大数(高精度)】number 计蒜客 - 45276
题目: 求 1 到 10^n 的数字中有 3 的数字的数量. 输入格式 1 个整数 n. 输出格式 共一行,1 个整数,表示答案. 数据范围 对于 10% 的数据,n≤8 对于 30% 的数据,n≤1 ...
- 【JVM之内存与垃圾回收篇】StringTable
StringTable String的基本特性 String:字符串,使用一对 "" 引起来表示 String s1 = "Nemo"; // 字面量的定义方式 ...
- apache 添加多个站点
虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术.可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口,也可让多个网站拥有不同的域 ...
- vue的双向数据绑定实现原理(简单)
如果有人问你,学vue学到了什么,那双向数据绑定,是必然要说的. 我们都知道,在vue中,使用数据双向绑定我们都知道是v-modle实现的. 实现原理是通过Object.defineProperty的 ...
- Spring学习之Spring中AOP方式切入声明式事务
mybatis-spring官方文档说明 一个使用 MyBatis-Spring 的其中一个主要原因是它允许 MyBatis 参与到 Spring 的事务管理中.而不是给 MyBatis 创建一个新的 ...
- 解决SyntaxError: Non-UTF-8 code starting with '\xbb'问题
在第一行加入 # coding=utf-8 2020-06-13
- Python List sort()方法
描述 sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数.高佣联盟 www.cgewang.com 语法 sort()方法语法: list.sort(cmp=None, ...
- PHP strchr() 函数
实例 查找 "world" 在 "Hello world!" 中的第一次出现,并返回字符串的其余部分: <?php高佣联盟 www.cgewang.com ...