mybatis什么时候用resulttype 什么时候用resultmap
如果你搜索只是返回一个值,比如说String ,或者是int,那你直接用resultType就行了。
但是你如果是返回一个复杂的对象,就必须定义好这个对象的resultMap的result map。 举个例子吧,例子以ibatis为例:
你有个User 对象, 拥有两个字段id,name。
1.你要获取id为123的name
String name = (String) queryForObject("getUserNameByID", id); <select id="getUserNameByID" resultType="java.lang.String">
Select name from User where id =#id#
</select> 2.你要获取整个User对象
User user = (User) queryForObject("getUserByID", id); <resultMap class="包.User" id="User">
<result property="id" column="ID" />
<result property="name" column="NAME" />
</resultMap> <select id="getUserByID" resultMap="User">
Select ID,NAME from User where id =#id#
</select>
追问
但是,resultType 也可以返回一个对象
<select id="getUserNameByID" resultType="com.bean.User">
Select * from User where id =#id#
</select> 也可以返回一个封装的对象啊
这个跟resultMap是一样的效果
那什么时候是用resultType解决不了的呢?只能用resultMap
追答
你要是反回这个对象用result type,就必须返回这个对象所有信息了,而且没有任何设置,适用用普通的完整返回。 但你用resultmap,因为resultmap,因为resultmap那段是我们自己指定的,可能指定的属性只是User的一部分,而且还可以设置默认值,这是result type做不到的:
resultMap里面只定义 name
<resultMap class="包.User" id="User">
<result property="name" column="NAME" />
</resultMap> <select id="getUserByID" resultMap="User">
Select NAME from User where id =#id#
</select>
mybatis什么时候用resulttype 什么时候用resultmap的更多相关文章
- Mybatis中输出映射resultType与resultMap的区别
Mybatis中输出映射resultType与resultMap的区别 (原文地址:http://blog.csdn.net/acmman/article/details/46509375) 一.re ...
- MyBatis标签之Select resultType和resultMap
摘要:介绍MyBatis 中Select标签的两个属性resultType和resultMap及其区别. 1 MyBatis动态SQL之if 语句 2 MyBatis动态sql之where标签|转 3 ...
- MyBatis 强大之处 多环境 多数据源 ResultMap 的设计思想是 缓存算法 跨数据库 spring boot rest api mybaits limit 传参
总结: 1.mybaits配置工2方面: i行为配置,如数据源的实现是否利用池pool的概念(POOLED – This implementation of DataSource pools JDBC ...
- mybatis 之 parameterType="String" resultType="java.util.HashMap">
public ServiceMessage<Map<String, String>> getGoodsStockNo( List<Map<String, Strin ...
- myBatis的一对多查询,主要利用resultMap实现一次查询多个结果集
日常开发中有这中场景,一个用户有多个角色,一个角色又有多个菜单,想查出一个用户的所有菜单.除了常见的关联查询之外,更使用的应该是利用myBatis的resultMap来实现一次查询出多个结果集,缺点: ...
- MyBatis学习-映射文件标签篇(select、resultMap)
MyBatis 真正的核心在映射文件中.比直接使用 JDBC 节省95%的代码.而且将 SQL 语句独立在 Java 代码之外,可以进行更为细致的 SQL 优化. 一. 映射文件的顶级元素 selec ...
- MyBatis关联查询,表字段相同,resultMap映射问题的解决办法
问题描述:在使用mybatis进行多表联合查询时,如果两张表中的字段名称形同,会出现无法正常映射的问题. 问题解决办法:在查询时,给重复的字段 起别名,然后在resultMap中使用别名进行映射. 给 ...
- MyBatis(3.2.3) - One-to-one mapping using nested ResultMap
We can get Student along with the Address details using a nested ResultMap as follows: <resultMap ...
- MyBatis中的resultType和resultMap
MyBatis的查询在进行映射的时候,返回值类型可以使用resultType同时也可以使用resultMap.前者表示直接的返回值类型,一般是domain名称,当然这里可以写domain的全部路径也可 ...
随机推荐
- 第五章 JavaScript对象及初识面向对象
第五章 JavaScript对象及初识面向对象 一.对象 在JavaScript中,所有事物都是对象,如字符串.数值.数组.函数等. 在JavaScript对象分为内置对象和自定义对象,要处理一些 ...
- URL编码和Base64编码 (转)
我们经常会遇到所谓的URL编码(也叫百分号编码)和Base64编码. 先说一下Bsae64编码.BASE64编码是一种常用的将二进制数据转换为64个可打印字符的编码,常用于在通常处理文本数据 ...
- 批量检测GoAhead系列服务器中Digest认证方式的服务器弱口令
最近在学习用python写爬虫工具,某天偶然发现GoAhead系列服务器的登录方式跟大多数网站不一样,不是采用POST等方法,通过查找资料发现GoAhead是一个开源(商业许可).简单.轻巧.功能强大 ...
- hadoop2.6.0实践:A01 问题处理 DEPRECATED: Use of this script to execute hdfs command is deprecated.
[hadoop@hadoop-master data]$ hadoop dfs -ls /DEPRECATED: Use of this script to execute hdfs command ...
- Linux--慕课学习
刚开始接触Linux,很有幸的在慕课网上看到了Peter老师的Linux入门课程,老师讲课真的式行云流水,深入浅出,循循善诱,层层递进. 老师分享的都是自己多年来总结的经验.看完之后也学到了很多东西. ...
- [转]XHR简介
在XHR诞生前,网页要获取客户端和服务器的任何状态更新,都需要刷新一次,在XHR诞生后就可以完全通过JS代码异步实现这一过程.XHR的诞生也使最初的网页制作转换为开发交互应用,拉开了WEB2.0的 ...
- SQL to Java code for Elasticsearch
Elasticsearch虽然定位为Search Engine,但是因其可以持久化数据,很多时候,我们把Elasticsearch当成Database用,但是Elasticsearch不支持SQL,就 ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [LeetCode] Employee Importance 员工重要度
You are given a data structure of employee information, which includes the employee's unique id, his ...
- [LeetCode] Find K Closest Elements 寻找K个最近元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...