前言

简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化。 那么mybatis实现一对一的关联查询则是使用association属性和resultMap属性。

搭建开发环境

创建student表、teacher表来搭建一对一的关联查询场景,student表添加外键supervisor_id实现和teacher表的关联

 CREATE TABLE [dbo].[t_teacher](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](30) NULL,
[title] [nvarchar](30) NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
 CREATE TABLE [dbo].[t_student](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](30) NULL,
[major] [nvarchar](30) NULL,
[supervisor_id] [int] NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

一对一关联查询

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

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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" javaType="Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_name"/>
<result property="title" column="title"/>
</association>
</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>

teacher model

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

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;
}
}

  

单元测试

@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从入门到放弃三:一对一关联查询的更多相关文章

  1. MyBatis:一对一关联查询

    MyBatis从入门到放弃三:一对一关联查询 前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是 ...

  2. MyBatis从入门到放弃四:一对多关联查询

    前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collection属性,别忽略了ofType属性. 搭建开发 ...

  3. Mybatis学习4——一对一关联查询方法2------实体作为属性

    实体order和user采用resultMap order package pojo; import java.util.Date; public class Order { private Inte ...

  4. Mybatis学习4——一对一关联查询方法1--创建实体

    创建一个实体继承两个实体之一,另一个实体作为属性 实体1. order package pojo; import java.util.Date; public class Order { privat ...

  5. mybatis 使用接口增删改查和两表一对一关联查询

    导包 总配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration ...

  6. MyBatis学习(四)MyBatis一对一关联查询

    一对一关联查询即.两张表通过外键进行关联.从而达到查询外键直接获得两张表的信息.本文基于业务拓展类的方式实现. 项目骨架 配置文件conf.xml和db.properties前几节讲过.这里就不细说了 ...

  7. MyBatis关联查询,一对一关联查询

    数据库E-R关系 实体类 public class City { Long id; String name; Long countryId; Date lastUpdate; } public cla ...

  8. mybatis的动态sql编写以及一对一关系查询和一对多的查询

    创建mybatis数据库,运行以下sql语句 /* SQLyog Ultimate v8.32 MySQL - 5.5.27 : Database - mybatis **************** ...

  9. 一对一关联查询时使用relation连贯操作查询后,调用getLastSql()方法输出的sql语句

    如题: 一对一关联查询时使用relation连贯操作查询后,调用getLastSql()方法输出的sql语句不是一条关联查询语句. 例如: $list = $db->relation(true) ...

随机推荐

  1. To me

    1.流泪的时候不做任何决定: 2.不反复思考同一个问题: 3.不害怕做错什么: 4.有负面情绪是正常的: 5.一切的烦恼都是自找的: 6.说过的话一定要做到: 7.不要去害怕做一件事: 8.无论是对是 ...

  2. Java并发编程:volatile关键字

    volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...

  3. django的简单原理

    一.自定义客户端和服务端的请求响应 1.客户端打开url,向服务器发出请求 2.服务端用socket写一个py,用于接收请求和做出响应 3.服务端接收请求 4.服务端模拟HTTP协议做出响应,状态行为 ...

  4. mysql的部署

    mysql在linux系统中的部署: 二进制包安装软件: 第一步:下载二进制软件,上传到服务器 www.mysql.com mkdir /server/tools -y cd /server/tool ...

  5. Linux 第十一天

    2)SetGID i只有可执行的二进制程序才能设置SGID权限 ii命令执行者要对该程序拥有x(执行)权限 iii命令执行在执行程序的时候,组身份升级为该程序文件的属组 iv SetGID权限同样只在 ...

  6. Django——用户认证和判断用户是否登录

    用户认证 必须通过认证之后才能login(request,user)这样才能保存会话到request中,注销后会话结束 注意 自定义的用户登陆时只不止需要验证用户名和密码的需要写认证,就例如在线教育平 ...

  7. [ 9.28 ]CF每日一题系列—— 940A规律构造

    Description: 输入a,b,x,给你a个0,b个1,你要给出一个组合,让这个组合里存在x位,使得这x为和其x+1位不相等 Solution: 因为肯定有一个正确的答案,所以钻了一下空子,贪心 ...

  8. liunx 安装jdk

    1 下载jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2 切换root ...

  9. Python自动化开发 - MySQL(一)

    本节内容 一.概述 二.下载安装 三.数据库操作 四.数据表操作 五.表内容操作 一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么 ...

  10. CASUAL_NCT

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...