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 ...
随机推荐
- 关于原型继承中的constructor重定向的问题
上代码 function SuperType(name){ this.name = name; this.colors = ["red","blue",&quo ...
- AI 最小二乘法
最小二乘法 参考链接: https://zhuanlan.zhihu.com/p/27204466
- Luogu P4779 【模板】单源最短路径(标准版)(Dijkstra+堆优化模板)
qwq dij其实和prim挺像的,prim是找权值最小点,dij是找边, 用一个优先队列就可以在加入边的时候直接排序,避免了每次遍历更新min priority_queue <pair< ...
- 一个有趣的问题——HTTP是“超文本传输协议”还是“超文本转移协议”
最近在看<HTTP图解>这本书,书中提到了对国内对HTTP协议名称的翻译问题,并且给出了一些网友讨论的原稿链接,我看了一下觉得挺有意思的,另外我本人也觉得翻译对于理解协议本身非常重要,就整 ...
- eclipse取消自动输入提示
在设置Eclipse自动提示后,按a-z都会显示提示,但是我们需要键入Enter才会输入,而默认的所有都键入,非常弱智,可采用下面方法设置. 1,先找到相关的插件: window -> show ...
- Apache之Rewrite和RewriteRule规则梳理以及http强转https的配置总结
一. 简单实例介绍一般来说,apache配置好http和https后,如果想要做http强转到https,需要设置url重定向规则,大致需要下面几个步骤即可完成配置: 1)在httpd.conf文件里 ...
- openssl版本升级操作记录
需要部署nginx的https环境,之前是yum安装的openssl,版本比较低,如下: [root@nginx ~]# yum install -y pcre pcre-devel openssl ...
- php5.6安装Zend Opcache扩展
假设php5.6安装路径为/data2/php[root@nextcloud src]# pwd/usr/local/src[root@nextcloud src]# wget http://pecl ...
- ACM-ICPC 2018 沈阳赛区网络预赛 G. Spare Tire
这题很好啊,好在我没做出来...大概分析了一下,题目大概意思就是求 问所有满足1<=i<=n且i与m互素的ai之和 最开始我们队的做法是类似线性筛的方法去筛所有数,把数筛出来后剩下数即可, ...
- 个人作业-Week1(新增详细说明)
快速看完整部教材,列出你仍然不懂的5到10个问题,发布在你的个人博客上. 如何提出有价值的问题? 请看这个文章:http://www.cnblogs.com/rocedu/p/5167941.html ...