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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM