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" />

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

 

注:是关联的意思,常被用来表示(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" />



<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) 


mapper.insertStudent(stu);
sqlSession.commit(); 
-->
VALUES(#{studId},#{name},#{email},#{dob},#{phone},#{address.addrId})

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中一对一关系映射)