MyBatis mapper.xml配置一对一关系映射的几种方式整理

所需的类:

public class Student {
    private Integer studId; 
    private String name; 
    private String email; 
    private Date dob;
    private Address address;
    //get and set method
    ......
    }

public class Address {
    private Integer addrId; 
    private String street; 
    private String city; 
    private String state; 
    private String zip; 
    private String country;
    //get and set method
    ......
    }

mapper.xml配置:

第一种:嵌套(同个xml内)结果 ResultMap
在Student的ResultMap中使用association 标签引用Address的ResultMap的id

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

<!-- 根据id查询-->
<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=#{Id} 
</select> 
.....

第二种:使用圆点记法为内嵌的对象的属性赋值
在resultMap中,Student 的address属性使用了圆点记法
被赋上了address对应列的值

......
<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=#{Id} 
</select> 
......

第三种:使用association定义内联的 resultMap
需注意:association javaType=”Address”

<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 id="findStudentWithAddressByStuId"  parameterType="int"  resultMap="StudentResult"> 
    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=#{Id} 
</select> 

第四种:嵌套select查询实现一对一关系映射
在此方式中,association 元素的 select属性被设置成了id为 findAddressById 的语句,然后查询结果赋值

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

参考:Java Persistence with MyBatis 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值