MyBatis的ResultMaps之一对一关系
1. 一对一关系
在我们domain的包中,每一个student都关联了一个地址。STUDENT的表中拥有ADDR_ID的列这个是作为ADDRESS表的外主键。
STUDENT表的信息如下:
ADDRESS表的信息如下:
让我们来看一下如何通过获取Student信息包括Adress信息。
public class Address
{
private Integer addrId;
private String street;
private String city;
private String state;
private String zip;
private String country;
// setters & getters
}
public class Student
{
private Integer studId;
private String name;
private String email;
private PhoneNumber phone;
private Address address;
//setters & getters
}
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
<result property="address.addrId" column="addr_id"/>
<result property="address.street" column="street"/>
<result property="address.city" column="city"/>
<result property="address.state" column="state"/>
<result property="address.zip" column="zip"/>
<result property="address.country" column="country"/>
</resultMap>
<select id="selectStudentWithAddress" parameterType="int"
resultMap="StudentWithAddressResult">
SELECT STUD_ID, NAME, EMAIL, A.ADDR_ID, STREET, CITY, STATE,
ZIP, COUNTRY
FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A ON
S.ADDR_ID=A.ADDR_ID
WHERE STUD_ID=#{studId}
</select>
我们可以在<resultMap>的节点中,嵌套放入Address的信息。在<resultMap>中,学生的地址参数值是通过address.XX来获取的。像这样,我们可以将嵌套对象的属性引用到任何地方。我们可以使用如下的方法来调用嵌套对象:
public interface StudentMapper
{
Student selectStudentWithAddress(int studId);
}
int studId = 1;
StudentMapper studentMapper =
sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectStudentWithAddress(studId);
System.out.println("Student :"+student);
System.out.println("Address :"+student.getAddress());
上面展示了一种方法关于一对一的映射。可是问题来了,如果我们调用的Address对象中也包含了其它的对象,也就是说Address对象与其它对象也存在关系的话,那我们不就要做同样的嵌套工作。
Maybatis提供了更好的方法,那就是应用Nested ResultMap和Nested Select的声明。下面笔者将会介绍。
2. 使用用Nested ResultMap
我们可以获取Student的信息连带着Adress信息,我们可以运用到NestedResultMap,使用方法如下的例子:
<resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</resultMap>
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" resultMap="AddressResult"/>
</resultMap>
<select id="findStudentWithAddress" parameterType="int"
resultMap="StudentWithAddressResult">
SELECT STUD_ID, NAME, EMAIL, A.ADDR_ID, STREET, CITY, STATE,
ZIP, COUNTRY
FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A ON
S.ADDR_ID=A.ADDR_ID
WHERE STUD_ID=#{studId}
</select>
上面的例子中,<association>元素可能被使用加载一个对象的关联。在例子中,<association>>元素中还涉及到<resultMap>的节点,这个使用与前面的是一样的。我们还可使用如下的方法来使用<association>
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" javaType="Address">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</association>
</resultMap>
3. 使用 Nested Select
我们可以使用嵌套的Select语句来获取Student信息上连带着Adress的信息。
<resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</resultMap>
<select id="findAddressById" parameterType="int"
resultMap="AddressResult">
SELECT * FROM ADDRESSES WHERE ADDR_ID=#{id}
</select>
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" column="addr_id"
select="findAddressById"/>
</resultMap>
<select id="findStudentWithAddress" parameterType="int"
resultMap="StudentWithAddressResult">
SELECT * FROM STUDENTS WHERE STUD_ID=#{Id}
</select>
在这个方法中,这个<association>元素的select有名称放到了<select>的id中。所以执行上面的语句将会执行两条SQL语句,一个是findStudentById,另一个是findAddressById,而且后者主要就是加载Addresss的信息。
我们可以用下面的方法来执行findStudentWithAddress.
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectStudentWithAddress(studId);
System.out.println(student);
System.out.println(student.getAddress());
MyBatis的ResultMaps之一对一关系相关推荐
- Mybatis xml中配置一对一关系association一对多关系collection
Mybatis xml中配置一对一关系association&一对多关系collection 今天在配置一对一关系映射以及一对多关系映射的时候,把collection中应该使用的ofType配 ...
- MyBatis:注解简化一对一关系
上期 注解式是真的好用! 一对一的关系:客户拥有一张身份证,而身份证也只有一个客户. 客户表中有身份证的cid,可以通过这个cid匹配身份证表中的cid,拿到那个具体的身份证信息 反之,身份证通过本身 ...
- MyBatis之级联——一对一关系
在学数据库概论的时候会听到这么几个词:数据库的关系分为一对一.一对多.多对多.对于在学校里学的知识已经忘得差不多了,在这里简单的提一下数据库的关系.此篇是介绍MyBatis是如何实现数据库中一对一关系 ...
- 创建以mybatis为基础的web项目(2)mabitis中的一对一关系项目实战
mabitis中的一对一关系项目实战: 1.首先根据创建以mybatis为基础的web项目(1)中的流程将web项目部署好 打开IDE,建立web工程 在lib下面粘贴mybatis的jar,log4 ...
- 七、MyBatis教程之四多表关系的实现
在MyBatis中,多表关系没有像Hibernate中体现的那么明显,关系型数据库表与表之间的关系主要有: 1.一对一关系 账户表-----账户详情表 2.多对一关系 学生和班级 3.一对多关系 班级 ...
- 7. MyBatis多表查询 - 一对一 - 一对多 - 多对多
7. MyBatis多表查询 - 一对一 - 一对多 - 多对多 前言 在前面的篇章,我们已经熟悉了单表查询,下面我们来看看如何进行 多表查询. 数据准备 create database if not ...
- MyBatis关联映射:一对一、一对多
一.一对一 场景:生活中每一个人都有一个身份证,这是最简单的一对一的关系. (1)用户表 (2)身份证表 (3)用户实体对象,com.xuliugen.mybatis.demo.bean.User ( ...
- Mybatis之表之间映射关系总结
1.什么是表之间映射关系 就是多张表进行关联,如果查询等操作不只是与一张表有关系,同时其他表也要进行操作. 2.数据库中的连接查询 表结构: 顾客表:customers ...
- 【Mybatis高级映射】一对一映射、一对多映射、多对多映射
前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...
- Mybatis 关联查询(一对一与一对多)
数据库表与表之间的关系: 1.一对一查询 需求:查询所有订单信息,关联查询下单用户信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息 ...
最新文章
- Anchor Boxes——目标检测质量的关键
- linux系统CPU,内存,磁盘,网络流量监控脚本
- iOS开展block说明
- Precision和Recall
- 农民斗地主——Binder fuzz安全研究
- @EnableAutoConfiguration注解的实现原理
- docker与kuberentes基本概念与操作学习
- Atitit 插件机制原理与设计微内核 c# java 的实现attilax总结
- 春节期间小游戏同时在线人数最高达2800万人/小时
- Alpha(9/10)
- Round trip
- 数据结构(C++语言版)第三版pdf
- hfss matlab api 天线,应用HFSS-MATLAB-API设计圆极化微带天线
- 最好用的屏幕分辨率修改器:SwitchResX for Mac
- 经济型EtherCAT运动控制器(四):ModbusRTU或ModbusTcp与触摸屏通讯
- 如何让微软Onedrive同步其他硬盘的文件
- 存储器管理的内存连续分配方式详解
- 【北京线下】FMI2018人工智能大数据技术沙龙第869期
- vue+element_ui纯前端下载csv文件
- 福建厦门计算机春季高考考哪些学校,福建省2020年春季高考总分多少?春季高考本科院校有哪些?...