mybatis中resultMap和resultType的区别

mybatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap。resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。

public class User {

private int id;

private String username;

private String hashedPassword;

//省略setter/getter方法

}

  1. 使用resultType

<select id="selectUsers" parameterType="int" resultType="com.someapp.model.User">

select id, username, hashedPassword

from some_table

where id = #{id}

</select>

这些情况下,MyBatis 会在幕后自动创建一个 ResultMap,基于属性名来映射列到 JavaBean 的属性上。如果列名没有精确匹配,可以在列名上使用 select 字句的别名来匹配标签。

  1. 使用resultMap

<resultMap id="userResultMap" type="User">

<id property="id" column="user_id" />

<result property="username" column="username"/>

<result property="password" column="password"/>

</resultMap>

<select id="selectUsers" parameterType="int" resultMap="userResultMap">

select user_id, user_name, hashed_password

from some_table

where id = #{id}

</select>

  1. 不同
  1. resultType对应的是java对象中的属性,大小写不敏感;resultMap对应的是对已经定义好了id的resultTupe的引用,key是查询语句的列名,value是查询的值,大小写敏感;
  2. 使用resultType的时候,要保证结果集的列名与java对象的属性相同,而resultMap则不用。
  3. 另外,resultMap 元素,它是 MyBatis 中最重要最强大的元素,它能提供级联查询,缓存等功能。

【MyBatis】resultMap和resultType的区别相关推荐

  1. php 数据类型 map,ResultMap和ResultType的区别

    ResultMap和ResultType的区别 使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType:另一种就是resultMap: 1.re ...

  2. resultMap与resultType的区别

    resultmap与resulttype的区别为:对象不同.描述不同.类型适用不同. 一.对象不同 1.resultmap:resultMap如果查询出来的列名和pojo的属性名不一致,通过定义一个r ...

  3. resultMap和resultType 的区别?

    1.resultMap 如果查询出来的列名和pojo的属性名不一致时,可以通过resultMap对列名和属性名做映射关系; <resultMap type="com.wuhao.one ...

  4. 在mybatis中resultMap与resultType的区别

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...

  5. Mybatis中resultMap和resultType的区别

    MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...

  6. mybatis Resultmap 与 ResultType 区别

    Resultmap 的写法====目的是为了做映射 <resultMap id="BaseResultMap"type="com.suning.jupiter.co ...

  7. resultMap和resultType的区别

    resultMap返回的列名与bean的属性名可以不一致,resultType则要求一致.就具体代码作分析.对于指定resultMap属性,直接读取字符串,而对于指定resultType会解析为具体的 ...

  8. ibatis中resultMap和resultType使用区别

    resultMap 1.resultMap的值为映射的返回值自定义节点Id名称 resultType 1.resultType的值为返回值的类型,且为一列 ---------------------- ...

  9. mybatis中resultMap和resultType区别,三分钟读懂

    先说结论: resultmap与resulttype的区别为:对象不同.描述不同.类型适用不同. 说人话就是,resultmap和resulttype功能差不多,但是resultmap功能更强大 re ...

最新文章

  1. python二进制转字符串
  2. 【转】算法中时间复杂度概括——o(1)、o(n)、o(logn)、o(nlogn)
  3. TColorToHex 与 HexToTColor
  4. Python常见面试题:TCP 协议中的三次握手与四次挥手相关概念详解
  5. 【视频】CCNA——telnet和SSH的配置
  6. Chapter7-8_Deep Learning for Constituency Parsing
  7. linux嵌入式物联网_嵌入式Linux如何加速物联网发展
  8. JS控制文本框内键盘上下左右键的焦点
  9. Spring源码之FactoryBean接口的作用和实现原理
  10. 【领英如何一键批量有效地加好友?】
  11. 什么是CSR以及CSR的作用和生成方法
  12. 海康威视相机开发(一)
  13. 月均播放超2.8亿,vlog会是品牌B站推广新风口吗?
  14. 分布式架构、大数据、机器学习、搜索、推荐、广告
  15. android rsa加密工具类,GitHub - Lerist/encrypt: Android 加密解密工具包。
  16. 3418 杨辉三角形(找规律 + 二分)
  17. 个人也可以通过维瑞申请VeriSign,Thawte 代码签名证书
  18. openwrt winshak 抓取无线beacon帧
  19. Vue_单页应用VS多页应用
  20. C++ STL源码分析——一个万用的 hash function

热门文章

  1. 项目管理软件project下载安装配置图文教程
  2. 有向无环图的所有拓扑排序
  3. ssh登录时自动运行命令
  4. idea中Maven打包
  5. TOCControl绑定Mapcontrol 报调用目标发生异常问题
  6. OCR文字技术与行业应用
  7. 全连接前馈神经网络DNN
  8. 高通MDM9X07_MDM8207_MDM9628 芯片手册使用介绍
  9. nginx 搭建 rtmp
  10. 返利CPS项目怎么做