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 ...
随机推荐
- 【Codeforces 1132D】Stressful Training
Codeforces 1132 D 题意:给\(n\)个电脑的电量和耗电速度,你可以买一个充电器,它的充电速度是每秒\(v\)单位,\(v\)你自己定.问最小的\(v\)能使得在\(k\)秒内每秒给某 ...
- Android6.0权限大全和权限分类
本文转载至: https://blog.csdn.net/qq_26440221/article/details/53097868 自从出了Android6.0权限管理之后,再也不能像以前那样粘贴复制 ...
- proxy config (firefox config)
sudo apt-get install shadowsocks sudo apt-get install polipo 编辑polipo config: sudo vim /etc/polipo/c ...
- eclipse中使用svn提交,更新代码。
在新公司工作,版本管理工具变成了svn,之前一直用git作为版本管理,用的编辑IDE是IntelliJIDEA,在这个编辑器下工作,还是很方便的,但是现在使用eclipse和svn.有点不习惯,但还是 ...
- 【小程序】当遇到bindTap绑定无法跳转到tabbar页面时
如下图: 更换成navigator包裹跳转也不起作用. cart目录在app.json中定义在底部tabBar中 在小程序 导航 文档 最下方表示 所以,以上应改为
- Android下WPS打开Excel2007版也有问题
继上次解决微软office Android版Excel下载并打开Excel文件修改后(http://anforen.com/wp/2017/11/excel-android-mobile/),再上传出 ...
- Intel Artificial Intelligence Conference(2018.11.14)
时间:2018.11.14地点:北京国贸大酒店
- [转]Office导入导出组件权限配置汇总
原文地址:Office导入导出组件权限配置汇总 具体配置方法如下: 1:在服务器上安装office的Excel软件. 2:在"开始"->"运行"中输入 ...
- 请允许我转载一篇关于套接字的博客:Socket
这一篇文章,我将图文并茂地介绍Socket编程的基础知识,我相信,如果你按照步骤做完实验,一定可以对Socket编程有更好地理解. 本文源代码,可以通过这里下载 http://files.cnblog ...
- RabbitMQ 优先级队列-为队列赋权
RabbitMQ 消息收发是按顺序收发,一般情况下是先收到的消息先处理,即可以实现先进先出的消息处理.但如果消息者宕机或其他原因,导致消息接收以后,未确认,那么消息会重新Requeue到队列中,就打破 ...