mybatis中一对一关系映射
一对一关系中普通的配置方式
一.多表连接查询语句:
<select id="selectStudentWithAddress" parameterType="int"
resultMap="StudentWithAddressResult">
select
s.stud_id, s.name, s.email,s.dob,s.phone,
a.addr_id, a.street, a.city, a.state, a.zip,a.country
from
students s left outer join addresses a
on
s.addr_id=a.addr_id
where
stud_id=#{id}
</select>
1. 把所有的查询结果,在一个resultMap中映射
<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" />
<!--adderss是Student的内置对象-->
<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>
2.使用【嵌套结果】ResultMap,实现一对一关系映射(就是说在一个resultMap中映射部分字段,在另一个映射结果中关联)
<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>
注:<association>是关联的意思,常被用来表示(has-one)类型的关联。就是对象1里面关联另一个对象2
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="dob" column="dob" />
<result property="phone" column="phone" />
<association property="address" resultMap="AddressResult" />
</resultMap>
3.定义【内联】的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" 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>
二.嵌套查询语句select,实现一对一关系映射
在一个映射结果中,嵌套了另一个select语句
<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查询,专门查询Address
<select id="findAddressById" parameterType="int"
resultMap="AddressResult">
select * from addresses
where addr_id=#{id}
</select>
Student封装映射,里面关联了查询address使用的select语句,并指定数据库表中的这个关联的外键列的名字,这里是addr_id
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="dob" column="dob" />
<result property="phone" column="phone" />
<!--第一条sql语句查出的addr_id值当作参数传给findAddressById,然后封装个Address对象传给address-->
<!--相当于将AddressResult结果集映射进来封装成一个Address类型的对象,传给Student类型中的address属性-->
<!-- property="address" 这是类中的属性 将column="addr_id"的值传给select="findAddressById" 这个语句,返回address对应的结果集-->
<association property="address" column="addr_id"
select="findAddressById" />
</resultMap>
查询Student的select语句,这里不用写多表查询,因为对于address的关联查询,已经在上边定义好了,并且在结果映射中关联进来了
<select id="selectStudentWithAddress" parameterType="int"
resultMap="StudentWithAddressResult">
select * from students
where stud_id=#{id}
</select>
三.实现插入功能,要注意ADDRESSES表中的ADDR_ID字段在STUDENTS表中做主键
<insert id="insertStudent" parameterType="Student">
<selectKey keyProperty="studId" resultType="int" order="BEFORE">
select my_seq.nextval from dual
</selectKey>
INSERT INTO
STUDENTS(STUD_ID,NAME,EMAIL,DOB,PHONE,ADDR_ID)
<!-- 这里注意,使用以下的顺序调用,才可以使最后的ADDR_ID有值-->
<!-- mapper.insertAddress(address);
<!--在这里调用这个方法后address对象就会利用序列自动生成主键addrId,并且保存到address对象中-->
mapper.insertStudent(stu);
sqlSession.commit();
-->
VALUES(#{studId},#{name},#{email},#{dob},#{phone},#{address.addrId})
<!-- 如果是对象自己取自己的值用作插入或判断条件,不可以写#{this.属性} 应当直接写#{属性} -->
</insert> <insert id="insertAddress" parameterType="Address">
<selectKey keyProperty="addrId" resultType="int" order="BEFORE">
select my_seq.nextval from dual
</selectKey>
INSERT INTO
ADDRESSES(ADDR_ID,STREET,CITY,STATE,ZIP,COUNTRY)
VALUES(#{addrId},#{street},#{city},#{state},#{zip},#{country})
</insert>
mybatis中一对一关系映射的更多相关文章
- Mybatis中对象关系映射
在实际开发中,实体类之间有一对一.一对多.多对多的关系,所以需要正确配置它们对应关系,Mybatis通过配置文件能够从数据库中获取列数据后自动封装成对象. 如:一个订单Orders类对应一个用户Use ...
- 【mybatis深度历险系列】mybatis中的高级映射一对一、一对多、多对多
学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下mybatis中的高级映射,包括一对一.一对多.多对多,希望多有需要 ...
- 问题记录:EntityFramework 一对一关系映射
EntityFramework 一对一关系映射有很多种,比如主键作为关联,配置比较简单,示例代码: public class Teacher { public int Id { get; set; } ...
- Hibernate(三)——框架中的关系映射
在设计数据库时我们会考虑,表与表之间的关系,例如我们前边经常提到的一对一,一对多,多对多关系,在数据库中我们通过外键,第三张表等来实现这些关系.而Hibernate时间实体类和数据库中的表进行的映射, ...
- 【mybatis深度历险系列】mybatis中的输入映射和输出映射
在前面的博文中,小编介绍了mybatis的框架原理以及入门程序,还有mybatis中开发到的两种方法,原始开发dao的方法和mapper代理方法,今天博文,我们来继续学习mybatis中的相关知识,随 ...
- Hibernate One-to-One Mappings 一对一关系映射
Hibernate One-to-One Mappings 一对一关系映射 关键:一对一关系映射和多对一关系映射非常像.仅仅是unique 属性值为 true 样例:一个员工仅仅能有一个地址. Hib ...
- Hibernate框架(三)框架中的关系映射
在设计数据库时我们会考虑,表与表之间的关系,例如我们前边经常提到的一对一,一对多,多对多关系,在数据库中我们通过外键,第三张表等来实现这些关系.而Hibernate时间实体类和数据库中的表进行的映射, ...
- 【mybatis xml】数据层框架应用--Mybatis(三)关系映射之一对一关系映射
实际的开发中,对数据库的操作常常会涉及到多张表,这在面向对象中就涉及到了对象与对象之间的关联关系. 针对多表之间的操作,MyBatis提供了关联映射,通过关联映射就可以很好的处理对象与对象之间的关联关 ...
- (五)mybatis之一对一关系
一.需求分析 需求:查询订单信息关联查询用户信息 分析:一条订单只能由一个消费者来下单,也就是说从订单的角度来说与消费者是一对一的关系. 二.建数据库表和实体对象 其中订单表中的字段user_id对应 ...
随机推荐
- leetcood学习笔记-203-移除链表元素
题目描述: 方法:#在改pre链表时 head中的值也改变 class Solution(object): def removeElements(self, head, val): "&qu ...
- CSS——背景及应用
CSS 可以添加背景颜色和背景图片,以及进行图片设置. none : 无背景图(默认的) url : 使用绝对或相对地址指定背景图像 background-image 属性允许指定一个图片展示在背景中 ...
- 单调栈(最大子矩形强化版)——牛客多校第八场A
求01矩阵里有多少个不同的1矩阵 首先预处理出pre[i][j]表示i上面连续的1个数,对每行的高度进行单调栈处理 栈里的元素维护两个值:pre[i][j]和向前延伸最多能维护的位置pos 然后算贡献 ...
- hdu多校第七场 1006(hdu6651) Final Exam 博弈
题意: 有n道题,这n道题共m分,要求你至少做出k道才能及格,你可以自由安排复习时间,但是只有某道题复习时间严格大于题目分配的分值时这道题才能够被做出来,求最少的,能够保证及格的复习时间.复习时间和分 ...
- HDU6438 Buy and Resell 2018CCPC网络赛 -低买高卖-贪心经典题
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. 出过很多次:5 ...
- HDU3605: Escape-二进制优化建图-最大流
目录 目录 思路: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 目录 题意:传送门 原题目描述在最下面. \(n(n\leq 100000)\)个人\(m(m\leq 10) ...
- ionic:temple
ylbtech-ionic:temple 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylb ...
- 杂项:ionic
ylbtech-杂项:ionic ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化html.css和js的性能,构建高效的应用程序,而且还可以用于构建Sass和AngularJS ...
- Git及github使用(三)更新自己的github代码
如果之前上传的代码到目前有所改动,想要更新github上的代码文件.希望本篇对你有所帮助. 1.拉取代码本地修改后上传代码 提交成功后的效果如下: 2.更新展示在github首页的readme内容 上 ...
- 【ASP.Net Core】不编译视图文件
原文:[ASP.Net Core]不编译视图文件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/aqtata/article/details/818 ...