resulttype和resultMap区别详解

目录

一、对象不同

1. resultMap

2. resultType

3. 分析

二、描述不同

1、resultMap

2、resulTtype

三、类型适用不同


一、对象不同

1. resultMap

如果查询出来的结果的列名和实体属性不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系(示例代码如下)。

<!--结果返回类型采用resultMap定义-->
<select id="findCardById" parameterType="int" resultMap="findCard">
    select * from card where id=#{id}
</select>

<!--对上述resultMap进行自定义映射-->
<resultMap id="findCard" type="card">
    <id property="id" column="id"/>
    <result property="number" column="number"/>
</resultMap>

2. resultType

resultType使用resultType进行输出映射,只有查询结果显示的列名和实体的属性名一致时,该列才可以映射成功。

<!--结果返回类型采用resultType定义-->
<select id="findCardById" parameterType="int" resultType="card">
    select * from card where id=#{id}
</select>

3. 分析

从上述的实例代码可以看出,针对相同的类的映射,resultType和resultMap均可实现。

  1. 对于resultMap而言,除了需要在<select>标签中进行申明外,还需要单独使用<resultMap></resultMap>实现实体属性与数据库表列名之间的自定义映射,适合多表查询
  2. 对于resultType而言,仅需在<select>标签中用resultType属性申明结果返回类型即可,适合单表查询

二、描述不同

1、resultMap

对于一对一表连接的处理方式通常为在主表A的实体中添加嵌套另一个表B的实体,然后在mapper.xml中采用<association>元素进行对另一个表B的连接处理,其中<association>元素中的select的值为表B对应的SQL语句的唯一标识,一般为namespace+SQL语句的id。

在下述例子中,person实体与card实体是一对一的关系,查询要求是:根据person表中id的值查询用户的id,name,age,sex以及卡号number,但是在person表中只有number对应的card表的id值,故需要采用resulMap。

1.1 Cad类

package com.chen.pojo;
public class Card {
    private int id;
    private String number;
    //toString方法、set以及get方法省略
}

1.2 Cad类对应数据库表

 1.3 Cad类对应映射文件

<mapper namespace="com.chen.mapper.CardMapper" >
    <select id="findCardById" parameterType="int"   resultMap="findCard">
        select * from card where id=#{id}
    </select>
    <resultMap id="findCard" type="card">
        <id property="id" column="id"/>
        <result property="number" column="number"/>
    </resultMap>
</mapper>

1.4 Person类

package com.chen.pojo;
public class Person {
    private int id;
    private String name;
    private int age;
    private String sex;
    //toString方法、set以及get方法省略
}

 1.5 Person类应数据库表

1.6  Person类对应映射文件

<select id="findPersonById"
            parameterType="Integer"
            resultMap="CardWithPerson">
        select * from person where id=#{id}
    </select>
    <!--自定义结果集映射,对上述resultMap进行映射处理-->
    <resultMap id="CardWithPerson" type="person">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <!--使用association实现一对一映射
        property为实体属性
        javaType为该实体属性的类型
        select指向SQL语句值为对应语句的唯一标识,由于该参数来自上一条语句的及如果,
        所以值为即namespace+id
        column为所需属性对应在库表中的列名
        -->
        <association property="cardnumber" javaType="Card" column="card_id"
            select="com.chen.mapper.CardMapper.findCardById"/>
    </resultMap>

2、resulTtype

resultType无法查询结果映射到pojo对象的pojo属性中,根据对结构集查询遍历的需要选择使用resultType还是resultMap。适用于单表查询。

三、类型适用不同

1、resultmap:mybatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,只不过采用resultMap时需要对该resultMap进行自定义映射处理,即采用<resultMap>元素定义映射。

2、resulttype:resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。
 

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
resultTyperesultMapMyBatis中用于映射查询结果的两种方式。 resultType是直接指定查询结果的返回类型,可以是一个Java类或基本数据类型。当查询结果的列名与返回类型的属性名一致时,该列可以成功映射到返回类型的属性上。 比如,通过在select语句中使用resultType属性,可以将查询结果映射到一个实体类中。 resultMap则是对外部ResultMap的引用,可以通过定义一个ResultMap来指定查询结果的映射规则。ResultMap定义了查询结果的列与实体类属性之间的映射关系。使用resultMap可以更灵活地定义查询结果的映射规则,适用于复杂的映射场景。 所以,resultType是直接指定返回类型,而resultMap是通过定义ResultMap来指定映射规则。resultType适用于简单的映射场景,而resultMap适用于复杂的映射场景。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [resulttyperesultMap区别详解](https://blog.csdn.net/qq_26893841/article/details/127883346)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [MyBatis中关于resultTyperesultMap区别介绍](https://download.csdn.net/download/weixin_38717031/12793901)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chen_PK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值