广告
返回顶部
首页 > 资讯 > 数据库 >mybatis查询多个结果返回map--@MapKey使用方法
  • 367
分享到

mybatis查询多个结果返回map--@MapKey使用方法

mybatisjavamysql 2023-09-16 20:09:28 367人浏览 泡泡鱼
摘要

        目录         介绍  源码分析: 介绍 在使用mybatis的时候遇到一种情况,查询结果集返回多个,想用map接收,以前是用List接收,然后java代码中转换成map。其实mybatis是支持返回map的。下面

       

目录

       

介绍

 源码分析:


介绍

在使用mybatis的时候遇到一种情况,查询结果集返回多个,想用map接收,以前是用List<对象>接收,然后java代码中转换成map。其实mybatis是支持返回map的。下面就介绍使用方法。

测试代码:

@GetMapping(value = "dd")    public String test4(String params){        log.info("dsds" + params);        Map map = businessPriceMapper.getmaphh();        log.info("dsva ",map);        // 保存数据        return "1122";    }

下面@MapKey中的id是BusinessPricePO中的一个属性id,否则会报错:There is no getter for property named 'id' in 'class java.lang.Class' 

public interface BusinessPriceMapper extends BaseMapper {    @MapKey("id")    Map getmaphh();}
        SELECT id,apply_id FROM business_price LIMIT 10    

查询结果:

 源码分析

spring-boot继承mybatis,执行的时候会进入com.baomidou.mybatisplus.core.override.MybatisMapperMethod#execute

这个类,不同继承方式不同,这个自己打断点进入,查看

执行进入:com.baomidou.mybatisplus.core.override.MybatisMapperMethod#executeFORMap 

private  Map executeForMap(sqlSession sqlSession, Object[] args) {        Map result;        Object param = method.convertArgsToSqlCommandParam(args);        if (method.hasRowBounds()) {            RowBounds rowBounds = method.extractRowBounds(args);            result = sqlSession.selectMap(command.getName(), param, method.getMapKey(), rowBounds);        } else {// 执行这里的查询map方法            result = sqlSession.selectMap(command.getName(), param, method.getMapKey());        }        return result;    }

 再执行:org.apache.ibatis.session.defaults.DefaultSqlSession#selectMap(java.lang.String, java.lang.Object, java.lang.String, org.apache.ibatis.session.RowBounds)

public  Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {    final List list = selectList(statement, parameter, rowBounds);    final DefaultMapResultHandler mapResultHandler = new DefaultMapResultHandler<>(mapKey,            configuration.getObjectFactory(), configuration.getObjectWrapperFactory(), configuration.getReflectorFactory());    final DefaultResultContext context = new DefaultResultContext<>();    for (V o : list) {// 将返回结果,循环处理放入context中      context.nextResultObject(o);// 这里再进行转换      mapResultHandler.handleResult(context);    }// 处理完,再拿出转换后的结果,返回    return mapResultHandler.getMappedResults();  }

 处理返回Map的handler

org.apache.ibatis.executor.result.DefaultMapResultHandler#handleResult

public void handleResult(ResultContext context) {    final V value = context.getResultObject();    final MetaObject mo = MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory);    // TODO is that assignment always true?// 通过反射,找到key的get方法,再获取值    final K key = (K) mo.getValue(mapKey);// 将对象转换为map    mappedResults.put(key, value);  }

最终返回。

来源地址:https://blog.csdn.net/wangfenglei123456/article/details/128110607

您可能感兴趣的文档:

--结束END--

本文标题: mybatis查询多个结果返回map--@MapKey使用方法

本文链接: https://www.lsjlt.com/news/410312.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作