MyBatis配置一对一关联查询的两种方式及其双向获取时注意问题

如果<association>标签中用了select属性来引用一个查询<select>,那么此关联标签<association>返回的类型(即javaType)就是引用的<select>标签所返回的类型,无法在<association></association>之间使用<id><result>标签来映射返回类与表的关系!还有一些主键名字相同等情况下的处理事项,可参考下面成功的一个映射文件例子:

 

<span style="font-size:18px;"><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.domain.classMapper">  
    
    <!-- 一对一关联关系的关联查询结果嵌套查询 -->
    <!-- 在customer对象中获取IDCard对象,以下方式如果要成功,常用的方法是:Customer和IDCard表的主键名不能一样,或者在查询语句中给其命名别名;下面是主体对象的id用关联它的表的外键的值来赋值,从而避免了两个对象都是由查询出来的同一个id字段赋值的。 -->
    <select id="getCustomerDetail1" resultMap="customerResultMap">
     select * from IDCard i, customers c where c.id = i.customerid and c.id=#{id}
     <!-- 改变主键名一样的两个表的先后顺序(sql语句中from后面)可以会改变下面<association>的<id>标签所得到的值 -->
    </select>
    <resultMap type="Customer" id="customerResultMap">
     <id property="id" column="customerid"/>
     <result property="name" column="name"/>
     <result property="address" column="address"/>
     <association property="card" javaType="IDCard">
     <id property="id" column="id"/>
     <result property="cardNumber" column="num"/>
     </association>
    </resultMap>
    
    <!-- 在IDcard对象中获取Customer对象 -->
    <select id="getCardDetail1" resultMap="cardResultMap">
     select * from IDCard i, customers c where c.id = i.customerid and i.id=#{id}
    </select>
    <resultMap type="IDCard" id="cardResultMap">
     <id property="id" column="id"/>
     <result property="cardNumber" column="num"/>
     <association property="customer" javaType="Customer">
     <id property="id" column="customerid"/><!-- 此处column用的是IDCard表中的外键,因为IDCard和Customer两表的主键名一样 -->
     <result property="name" column="name"/>
     <result property="address" column="address"/>
     </association>
    </resultMap>
    
    <!-- 以下为一对一关联关系的查询语句嵌套查询 -->
    <!-- 在customer查询中嵌套IDCard查询结果 -->
    <select id="getCustomerDetail2" parameterType="int" resultMap="customerResultMap2">
select * from customers where id=#{id}
</select>
    <select id="getCard4Cus" parameterType="int" resultMap="card4Cus">
     select * from idcard where customerid = #{customerid}
    </select>
    
    <resultMap type="Customer" id="customerResultMap2">
     <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="address" column="address"/>
     <association property="card" column="id"  select="getCard4Cus" />
    </resultMap>
    
    <resultMap type="IDCard" id="card4Cus">
     <id property="id" column="id"/>
     <result property="cardNumber" column="num"/>
    </resultMap>
    
    <!-- 在IDcard查询中嵌套Customer查询结果 -->
<select id="getCustomer" parameterType="int" resultType="Customer">
select * from customers where id=#{id}
</select>
    <select id="getCardDetail2" parameterType="int" resultMap="idCardResultMap2">
     select * from idcard where id = #{id}
    </select>
    
    <resultMap type="IDCard" id="idCardResultMap2">
     <!-- <id property="id" column="id"/> -->
     <result property="cardNumber" column="num"/>
     <association property="customer" column="customerid" select="getCustomer">
     <!--如果使用了select属性则此处<id><result>等标签的配置毫无用处! -->
     </association>
    </resultMap>
 </mapper> </span>

你可能感兴趣的:(MyBatis配置一对一关联查询的两种方式及其双向获取时注意问题)