mybatis之resultmap的使用及其与一对一关联查询一对多关联查询的结合

本文介绍了MyBatis中如何配置一对一和一对多关联查询,包括XML映射文件中的resultMap元素详细设置,如id、result、association及collection等属性的具体使用方法。

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

1.一对一关联查询(在ordermapper.xml中配置):

<resultMap type="order" id="order_user_map">
         <!-- <id>用于映射主键 -->
         <id property="id" column="id"/>
         <!-- 普通字段用result映射 -->
         <result property="userId" column="user_id"/>
         <result property="number" column="number"/>
         <result property="createtime" column="createtime"/>
         <result property="note" column="note"/>
         <!-- aociation用于配置一对一关系
            property:order里面的user属性
            JAVAtYPE:user的数据类型,支持别名
            -->
         <association property="User" javaType="com.itheima.mybatis.pojo.User">
             <id property="id" column="user_id"/>
         <!-- 普通字段用result映射 -->
         <result property="username" column="username"/>
         <result property="address" column="address"/>
         <result property="birthday" column="birthday"/>
         <result property="sex" column="sex"/>
           
         </association>
         
       </resultMap>
        <select id="getOrderUserMap" resultMap="order_user_map">
          select 
            o.`id`,
            o.`user_id` userId,
            o.`number`,
            o.`createtime`,
            o.`note`,
            u.username,
            u.address,
            u.birthday,
            u.sex
          from `order` o
          left join user u
           on u.id=o.user_id
       </select>

2.一对多关联查询

<resultMap type="com.itheima.mybatis.pojo.User" id="user_order_map">
         <!-- <id>用于映射主键 -->
         <id property="id" column="id"/>
         <!-- 普通字段用result映射 -->
         <result property="username" column="username"/>
         <result property="address" column="address"/>
         <result property="birthday" column="birthday"/>
         <result property="sex" column="sex"/>
         <!-- collection用于对应一对多关联
              property:user中order的属性
              oftype:order的数据类型 支持别名
          -->
         <collection property="orders"  ofType="com.itheima.mybatis.pojo.Order">
             <id property="id" column="oid"/>
         <result property="number" column="number"/>
         <result property="createtime" column="createtime"/>
         <result property="note" column="note"/>
         </collection>
       </resultMap>
        <select id="getUserOrderMap" resultMap="user_order_map">
         select 
            o.`id` oid,
            o.`user_id` userId,
            o.`number`,
            o.`createtime`,
            o.`note`,
            u.username,
            u.address,
            u.birthday,
            u.sex
          from `user` u
          left join `order` o
           on u.id=o.user_id
       </select>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值