mybatis中一对一关系映射

Posted Magic_Li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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中一对一关系映射的主要内容,如果未能解决你的问题,请参考以下文章

Java 中mybatis 关系映射,比如:一对多

MyBatis的一对一关联关系映射

MyBatis的一对一关联关系映射

mybatis xml数据层框架应用--Mybatis关系映射之一对多关系映射

mybatis一对一关联关系映射

mybatis中一对一关系映射