MyBatis XML中 resultMap 与 resultType 的使用场景

Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

在 MyBatis 的 XML 映射文件里,resultMapresultType都和查询结果的映射相关,不过它们的适用场景有明显不同。下面为你详细介绍它们各自的使用情况:

1. resultType的适用情况

resultType适用于下面这些场景:

  • 返回简单数据类型:当查询结果是像IntegerStringLong这样的单个值时,就可以使用resultType

    xml

  • <select id="countUsers" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM users
    </select>
    
  • 返回 JavaBean 对象:若查询结果的列名和 JavaBean 的属性名完全一样,能直接进行映射,此时适合用resultType

    xml

  • <select id="getUserById" resultType="com.example.User">
        SELECT id, username, email FROM users WHERE id = #{id}
    </select>
    
  • 返回 Map 对象:要是想把查询结果以键值对的形式返回,就可以把resultType指定为map

    xml

  • <select id="getUserMap" resultType="map">
        SELECT id, username, email FROM users WHERE id = #{id}
    </select>
    

2. resultMap的适用情况

resultMap适用于以下场景:

  • 列名和属性名不匹配:当数据库表的列名和 JavaBean 的属性名不一致时,就需要借助resultMap来手动定义映射关系。

    xml

  • <resultMap id="UserResultMap" type="com.example.User">
        <id property="id" column="user_id"/>
        <result property="username" column="user_name"/>
        <result property="email" column="user_email"/>
    </resultMap>
    
    <select id="getUserById" resultMap="UserResultMap">
        SELECT user_id, user_name, user_email FROM users WHERE user_id = #{id}
    </select>
    
  • 处理复杂的关联映射:在处理一对一、一对多等复杂的对象关联关系时,必须使用resultMap
    • 一对一关联

      xml

  • <resultMap id="OrderResultMap" type="com.example.Order">
        <id property="id" column="order_id"/>
        <result property="orderNumber" column="order_number"/>
        <association property="user" javaType="com.example.User">
            <id property="id" column="user_id"/>
            <result property="username" column="username"/>
        </association>
    </resultMap>
    
  • 一对多关联

    xml

    • <resultMap id="UserResultMap" type="com.example.User">
          <id property="id" column="user_id"/>
          <result property="username" column="username"/>
          <collection property="orders" ofType="com.example.Order">
              <id property="id" column="order_id"/>
              <result property="orderNumber" column="order_number"/>
          </collection>
      </resultMap>
      
  • 映射复合字段:当需要把多个列映射到 Java 对象的一个属性时,比如把first_namelast_name合并映射到fullName属性,就要用到resultMap

    xml

  • <resultMap id="UserResultMap" type="com.example.User">
        <result property="fullName" column="{first=first_name,last=last_name}" 
                select="com.example.UserMapper.getFullName"/>
    </resultMap>
    

3. 总结

  • 使用resultType的情况:查询结果简单,且列名和 Java 对象属性名能自动对应上时使用。
  • 使用resultMap的情况:查询结果复杂,或者需要手动定义列名和属性名的映射关系,以及处理对象间的关联关系时使用。

一般来说,resultType使用起来更简单便捷,而resultMap的功能更强大,能应对复杂的映射需求。

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值