resulttype和resultMap区别详解

た 入场券 2023-10-02 23:03 39阅读 0赞

目录

一、对象不同

  1. resultMap

  2. resultType

  3. 分析

二、描述不同

1、resultMap

2、resulTtype

三、类型适用不同


一、对象不同

1. resultMap

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

  1. <!--结果返回类型采用resultMap定义-->
  2. <select id="findCardById" parameterType="int" resultMap="findCard">
  3. select * from card where id=#{id}
  4. </select>
  5. <!--对上述resultMap进行自定义映射-->
  6. <resultMap id="findCard" type="card">
  7. <id property="id" column="id"/>
  8. <result property="number" column="number"/>
  9. </resultMap>

2. resultType

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

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

3. 分析

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

  1. 对于resultMap而言,除了需要在标签中用resultType属性申明结果返回类型即可,适合单表查询

二、描述不同

1、resultMap

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

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

1.1 Cad类

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

1.2 Cad类对应数据库表

8411f3a7c7cc4f288173ed8a8cc3eb2a.png

1.3 Cad类对应映射文件

  1. <mapper namespace="com.chen.mapper.CardMapper" >
  2. <select id="findCardById" parameterType="int" resultMap="findCard">
  3. select * from card where id=#{id}
  4. </select>
  5. <resultMap id="findCard" type="card">
  6. <id property="id" column="id"/>
  7. <result property="number" column="number"/>
  8. </resultMap>
  9. </mapper>

1.4 Person类

  1. package com.chen.pojo;
  2. public class Person {
  3. private int id;
  4. private String name;
  5. private int age;
  6. private String sex;
  7. //toString方法、set以及get方法省略
  8. }

1.5 Person类应数据库表

4fba1b434e3e449c89d8d296c0a044a9.png

1.6 Person类对应映射文件

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

2、resulTtype

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

三、类型适用不同

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

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

发表评论

表情:
评论列表 (有 0 条评论,39人围观)

还没有评论,来说两句吧...

相关阅读