MyBatis:一对一关联查询
MyBatis从入门到放弃三:一对一关联查询
前言
简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化。 那么mybatis实现一对一的关联查询则是使用association属性和resultMap属性。
搭建开发环境
创建student表、teacher表来搭建一对一的关联查询场景,student表添加外键supervisor_id实现和teacher表的关联

1 CREATE TABLE [dbo].[t_teacher](
2 [id] [int] IDENTITY(1,1) NOT NULL,
3 [name] [nvarchar](30) NULL,
4 [title] [nvarchar](30) NULL,
5 PRIMARY KEY CLUSTERED
6 (
7 [id] ASC
8 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
9 ) ON [PRIMARY]
10
11 GO


1 CREATE TABLE [dbo].[t_student](
2 [id] [int] IDENTITY(1,1) NOT NULL,
3 [name] [nvarchar](30) NULL,
4 [major] [nvarchar](30) NULL,
5 [supervisor_id] [int] NULL,
6 PRIMARY KEY CLUSTERED
7 (
8 [id] ASC
9 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
10 ) ON [PRIMARY]

一对一关联查询
一对一关联的关键是在mapper.xml中创建resultMap。 如下代码看到了在studentResultMap中添加了属性association,property是在model类中的外键属性名称,别忘记指定JavaType

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="com.autohome.mapper.Student">
6 <resultMap id="studentResultMap" type="Student">
7 <id property="id" column="id"/>
8 <result property="name" column="name"/>
9 <result property="major" column="major"/>
10 <association property="supervisor" javaType="Teacher">
11 <id property="id" column="t_id" />
12 <result property="name" column="t_name"/>
13 <result property="title" column="title"/>
14 </association>
15 </resultMap>
16
17
18 <select id="getStudentById" parameterType="int" resultMap="studentResultMap">
19 SELECT st.id,st.name,st.major,
20 t.id t_id,t.name t_name,t.title
21 FROM t_student st inner join t_teacher t on st.supervisor_id=t.id
22 where st.id=#{id}
23 </select>
24 </mapper>

teacher model
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public class Teacher { private int id; private String name; private String title; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; }} |
student model
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class Student { private int id; private String name; private String major; private Teacher supervisor; public Teacher getSupervisor() { return supervisor; } public void setSupervisor(Teacher supervisor) { this.supervisor = supervisor; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; }} |
单元测试
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Test public void getStudentById(){ SqlSession sqlSession=null; try{ sqlSession=sqlSessionFactory.openSession(); Student student = sqlSession.selectOne("com.autohome.mapper.Student.getStudentById",1); System.out.println("id:"+student.getId()+",name:"+student.getName()+",导师姓名:"+student.getSupervisor().getName()+",导师职称:"+student.getSupervisor().getTitle()); }catch(Exception e){ e.printStackTrace(); }finally { sqlSession.close(); } } |
附实现截图

嵌套resultMap
如上的resultmap用起来总是不方便的,万一后续再到其他关联查询需要用到teacher表呢,那么我们把teacherResultMap单独拿出来,不仅是resultMap可以嵌套,sql语句也可以嵌套。
分别创建studentResultMap和suprvisorResultMap。后在studentResultMap的association中使用resultMap引用supervisorResultMap。

<mapper namespace="com.autohome.mapper.Student">
<resultMap id="studentResultMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="major" column="major"/>
<association property="supervisor" resultMap="suprvisorResultMap"/>
</resultMap>
<resultMap id="suprvisorResultMap" type="Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_name"/>
<result property="title" column="title"/>
</resultMap>
<select id="getStudentById" parameterType="int" resultMap="studentResultMap">
SELECT st.id,st.name,st.major,
t.id t_id,t.name t_name,t.title
FROM t_student st inner join t_teacher t on st.supervisor_id=t.id
where st.id=#{id}
</select>
</mapper>

实现结果和上面相同。
MyBatis:一对一关联查询的更多相关文章
- MyBatis学习(四)MyBatis一对一关联查询
一对一关联查询即.两张表通过外键进行关联.从而达到查询外键直接获得两张表的信息.本文基于业务拓展类的方式实现. 项目骨架 配置文件conf.xml和db.properties前几节讲过.这里就不细说了 ...
- mybatis一对一关联查询——(八)
1.需求 查询所有订单信息,关联查询下单用户信息. 注意: 因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则为一对多查 ...
- Mybatis一对一关联查询
有两张表,老师表teacher和班级表class,一个class班级对应一个teacher,一个teacher对应一个class 需求是根据班级id查询班级信息(带老师的信息) 创建teacher和c ...
- 五 Mybatis一对一关联查询的两种方式(基于resultType&基于resultMap)
关联查询: 一个用户对应多个订单,一个订单只有一个用户 订单关联用户:两种方式 一:基于resultTYpe,一个与表关系一样的pojo实现 主表订单,从表用户 首先要有一个与关联查询表关系一样的po ...
- MyBatis 一对一关联查询
xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...
- Mybatis之关联查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- MyBatis从入门到放弃三:一对一关联查询
前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是使用association属性和resultM ...
- Mybatis学习4——一对一关联查询方法2------实体作为属性
实体order和user采用resultMap order package pojo; import java.util.Date; public class Order { private Inte ...
- Mybatis学习4——一对一关联查询方法1--创建实体
创建一个实体继承两个实体之一,另一个实体作为属性 实体1. order package pojo; import java.util.Date; public class Order { privat ...
随机推荐
- FreeRTOS学习笔记--任务优先级
FreeRTOSConfig.h 中的常量configMAX_PRIORITIES的值就是任务优先级的最大数值,这个数值可以按照自己的需要改动,当然值越大,内核对内存的开销就越大,一般设置一个满足自己 ...
- Tensorflow[目录结构]
1 - Tensorflow源码目录结构 基于2018年5月28日github的tensorflow源码,即1.8版本 第一层: tensorflow: 核心代码目录. third_party:第三方 ...
- golang 常用包安装
常用到的: go get -v github.com/nsf/gocode go get -v github.com/rogpeppe/godef go get -v golang.org/x/too ...
- eclipse中文字体大小修改
貌似有不少人苦恼eclipse中文字体大小修改问题,默认的eclipse中文字体很小,和英文字体大小完全不在一个调子上,因为默认的eclipse juno中英文字体是Consolas,字体大小是10, ...
- Luogu1979 NOIP2013D2T3 华容道 搜索、最短路
题目传送门 题意:给出一个$N \times M$的棋盘,棋盘上有一些块可以移动,有一些块无法移动.$Q$次询问,每一次询问给出三个块$a,b,c$,将$a$块变为空格,空格旁边可移动的块可以与空格交 ...
- [Spark][Python]DataFrame的左右连接例子
[Spark][Python]DataFrame的左右连接例子 $ hdfs dfs -cat people.json {"name":"Alice",&quo ...
- ExtJS初探:了解 Ext Core
Ext Core是一款和jQuery媲美的轻型JS库,基于MIT许可.对于Dom的操作,我个人还是比较喜欢用jQuery.当然如果项目中用的是ExtJS框架,也就没必要多引用一个jQuery,Ext ...
- 计算机网络什么是OSI7层模型、TCP/IP4层模型理解
模型图解 应用层 就是最顶层的.通常指的应用程序初始走的协议比如有 TFTP,HTTP,SNMP,FTP,SMTP,DNS,Telnet 表示层 主要对数据应用层的数据包进行加密 会话层 建立.管理. ...
- rrd文件及rrd文件与实际数据的对比研究。
一,什么是rrd文件? 所 谓的“Round Robin” 其实是一种存储数据的方式,使用固定大小的空间来存储数据,并有一个指针指向最新的数据的位置.我们可以把用于存储数据的数据库的空间看成一个圆,上 ...
- .apply()用法和call()的区别
Js apply方法详解我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里 ...