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

本文介绍了MyBatis中如何进行一对一关联查询的配置方法,包括嵌套查询和嵌套结果两种方式,并提供了具体的XML映射文件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值