org.apache.ibatis.binding.MapperMethod中execute方法

...
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else {
...
  private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
Map<K, V> result;
Object param = method.convertArgsToSqlCommandParam(args);
if (method.hasRowBounds()) {
RowBounds rowBounds = method.extractRowBounds(args);
result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey(), rowBounds);
} else {
result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey());
}
return result;
}

映射为map时有个可自定义的参数:mapkey

    private String getMapKey(Method method) {
String mapKey = null;
if (Map.class.isAssignableFrom(method.getReturnType())) {
final MapKey mapKeyAnnotation = method.getAnnotation(MapKey.class);
if (mapKeyAnnotation != null) {
mapKey = mapKeyAnnotation.value();
}
}
return mapKey;
}

通过org.apache.ibatis.annotations.MapKey可以配置该参数,该参数应与sql中select的字段一致且为大写。

最终的resultHandler:

public class DefaultMapResultHandler<K, V> implements ResultHandler {

  private final Map<K, V> mappedResults;
private final String mapKey;
private final ObjectFactory objectFactory;
private final ObjectWrapperFactory objectWrapperFactory; @SuppressWarnings("unchecked")
public DefaultMapResultHandler(String mapKey, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) {
this.objectFactory = objectFactory;
this.objectWrapperFactory = objectWrapperFactory;
this.mappedResults = objectFactory.create(Map.class);
this.mapKey = mapKey;
} public void handleResult(ResultContext context) {
// TODO is that assignment always true?
final V value = (V) context.getResultObject();
final MetaObject mo = MetaObject.forObject(value, objectFactory, objectWrapperFactory);
// TODO is that assignment always true?
final K key = (K) mo.getValue(mapKey);
mappedResults.put(key, value);
} public Map<K, V> getMappedResults() {
return mappedResults;
}
}

mybatis源码追踪2——将结果集映射为map的更多相关文章

  1. mybatis源码追踪1——Mapper方法用法解析

    Mapper中的方法执行时会构造为org.apache.ibatis.binding.MapperMethod$MethodSignature对象,从该类源码中可以了解如何使用Mapper方法. [支 ...

  2. 2018/4/7 Mybatis源码结构概览

    在观看Mybatis源码的过程中,有一点疑惑,就是Mybatis的缓存设计明显有问题,首先,Mybatis缓存分为两级,先说一级,生命周期为一个sqlsession,只有在查询相同方法时才会命中缓存, ...

  3. mybatis 源码分析(一)框架结构概览

    本篇博客将主要对 mybatis 整体介绍,包括 mybatis 的项目结构,执行的主要流程,初始化流程,API 等各模块进行简单的串联,让你能够对 mybatis 有一个整体的把握.另外在 myba ...

  4. Mybatis源码解析(二) —— 加载 Configuration

    Mybatis源码解析(二) -- 加载 Configuration    正如上文所看到的 Configuration 对象保存了所有Mybatis的配置信息,也就是说mybatis-config. ...

  5. MyBatis 源码篇-SQL 执行的流程

    本章通过一个简单的例子,来了解 MyBatis 执行一条 SQL 语句的大致过程是怎样的. 案例代码如下所示: public class MybatisTest { @Test public void ...

  6. MyBatis源码分析(一)

    MyBatis故事: 官方网站:http://www.mybatis.org 官方文档:http://www.mybatis.org/mybatis-3/ GitHub:https://github. ...

  7. 精尽MyBatis源码分析 - SQL执行过程(三)之 ResultSetHandler

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  8. Mybatis源码解读-SpringBoot中配置加载和Mapper的生成

    本文mybatis-spring-boot探讨在springboot工程中mybatis相关对象的注册与加载. 建议先了解mybatis在spring中的使用和springboot自动装载机制,再看此 ...

  9. MyBatis源码分析(一)开篇

    源码学习的好处不用多说,Mybatis源码量少.逻辑简单,将写个系列文章来学习. SqlSession Mybatis的使用入口位于org.apache.ibatis.session包中的SqlSes ...

随机推荐

  1. Java的8种包装类:Wrapper Class

    Java有8种基本数据类型,为什么又要出现对应的8种包装类: 1.Java的8种基本数据类型不支持面向对象编程机制 2.8种基本数据类型不具备“对象”的特性:没有成员变量.方法可供调用 3.例如:某个 ...

  2. hadoop 学习(一)ubuntu14.04 hadoop 安装

    1.创建用户组 sudo addgroup hadoop 2.创建用户 sudo adduser -ingroup hadoop hadoop 回车之后会提示输入密码,输入自己要设定的密码然后一路回车 ...

  3. 2018.07.06 BZOJ 1588: HNOI2002营业额统计(非旋treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

  4. hdu-2955(01背包+逆向思维+审题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:注意p和m[i]是被抓的概率,不能直接用,要转换为逃跑的概率,然后将得到的钱视为背包体积再 ...

  5. Hadoop分布式远程Debug方式

    1.进入目录修改配置文件 cd /cloud/hadoop-2.2.0/etc/hadoop vim hadoop-env.sh2.加入内容(文本最后): #远程调试NameNode export H ...

  6. php读取用友u8采购入库单列表及详细

    <?php class erpData { protected static $erp; public function __construct() { $dbhost ="192.1 ...

  7. 编写属于自己的linux命令

    开篇: 问题和解决思路 在使用一些基础IDE时,工具经常会在我们建立特定文件时给我们一个已经有了一些特定代码的模板文件,但是在linux开发时,没有这样的IDE,怎么办?虽然代码量不是很多,但是能一次 ...

  8. 【翻译】使用Vuex解决Vue中的身份验证

    翻译原文链接:https://scotch.io/tutorials/handling-authentication-in-vue-using-vuex 我的翻译小站:https://www.zcfy ...

  9. 20155320 2016-2017-2 《Java程序设计》第六周学习总结

    20155320 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 输入输出 InputStream的read()方法每次都会尝试读入byte数组长度的数据,并 ...

  10. SwapBuffers的等待,虚伪的FPS(转)

    FPS在实时渲染中扮演着一个重要的角色,也许你会去笑一个不懂FPS是什么的游戏新手,但也许,这只是五十步笑一百步罢了.你能读懂SwapBuffers的深情等待吗?——ZwqXin.com frames ...