项目需要从ibatis升级到MyBatis,dao中有一个方法返回Map类型,具体是查询语句查询两个字段,将结果列表字段A的值作为key字段B的值作为value存入Map中作为结果返回;

ibatis中Dao继承SqlMapClientDaoSupport类的queryForMap(String statement, Object param, String key, String value)方法可直接实现;

MyBatis的SqlSession中只有selectMap(String statement, Object parameter, String mapKey),此方法将结果集中指定字段作为key,value则是结果集列表的元素对象们;源码如下:

/**
* The selectMap is a special case in that it is designed to convert a list
* of results into a Map based on one of the properties in the resulting
* objects.
* @param <K> the returned Map keys type
* @param <V> the returned Map values type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param mapKey The property to use as key for each value in the list.
* @return Map containing key pair data.
*/
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);

上网查了一些博文,最终参考:http://blog.csdn.net/sou_liu/article/details/47755635,整理如下:

主要思想是:重写ResultHandler接口,,然后用SqlSession 的select方法,将xml里面的映射文件的返回值配置成 HashMap 就可以了。

xml配置:

<resultMap id="ipDeptResult" type="java.util.HashMap">
<result property="key" column="ip"/>
<result property="value" column="dept"/>
</resultMap>

sql查询语句select出两个字段ip和dept,resultMap配置成上面定义的那个ipDeptResult。

看看Handler的实现就知道为什么resultMap里面的property写成key和value了,其实完全是自定义的。Handler对结果集的每一条记录调用handleResult方法我们重写它进行格式转换。

public class MapResultHandler implements ResultHandler {
@SuppressWarnings("rawtypes")
private final Map mappedResults = new HashMap(); @SuppressWarnings("unchecked")
@Override
public void handleResult(ResultContext context) {
@SuppressWarnings("rawtypes")
Map map = (Map)context.getResultObject();
mappedResults.put(map.get("key"), map.get("value")); // xml 配置里面的property的值,对应的列
}
public Map getMappedResults() {
return mappedResults;
}
}

调用类:

@Override
public <K,V> Map<K,V> queryForMap(String statement, Object params) {
MapResultHandler handler = new MapResultHandler();
sqlSession.select(statement, params, handler);
return handler.getMappedResults();
}

Mybatis select返回值为map时,选取表字段的两列作为key,value的更多相关文章

  1. 解决:oracle+myBatis ResultMap 类型为 map 时,表字段类型有 Long/Blob/Clob 时报错

    前言:最近在做一个通用查询单表的组件,所以 sql 的写法就是 select *,然后 resultType="map" .如果数据库中的表里有字段类型为 Long 等类型时,my ...

  2. mybatis Mapper 中resultType使用方法及返回值为Map的写法

    mybatis学习(七)——resultType解析 resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等.现总结一下再解释 总结: resul ...

  3. mybatis update 返回值

    mybatis sql: <update id="test" parameterType="map"> update test_0731 set n ...

  4. webservice返回值为Map类型的处理方法

    在写一个webservice的时候,方法的返回值是一个复杂类型,处理方法是写一个结果类(Javabean)作为返回值.想着webservice方法返回值为Map的没写过,然后就试着写了一个简单的Dem ...

  5. linux select 返回值

    IBM AIX上 select返回值的 man if  a connect-based socket is specified in the readlist parameter and the co ...

  6. Mybatis(三)返回值

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

  7. mybatis之返回值总结

    mybatis框架让我们能在编程中只需要编写一个接口,然后再编写mapper映射文件,无需编写接口的实现类就可以实现从数据库检索数据.这是mybatis通过动态代理,把mapper映射文件的内容转化为 ...

  8. mybatis hashmap 输入键值对为空时,key 丢失

    参考文档:https://blog.csdn.net/lulidaitian/article/details/70941769 springMVC+mybatis查询数据,返回resultType=” ...

  9. C# 调用 C++ 的 DLL 返回值为 bool 时,值混乱

    现象:C++ 导出函数的返回值为 false,C# 调用该函数获取的返回值却为 true . 原因:C++ 导出函数返回 false 时,采取的方式是: 将 C# 定义的用来接收返回值的 bool 所 ...

随机推荐

  1. Javascript实现页面跳转的几种方式

    概述 相信很多Web开发者都知道,在开发Web程序的时候,对于页面之间的跳转,有很多种,但是有效的跳转则事半功倍,下面就是我在平时的开发过程中所用到的一些JavaScript跳转方式,拿出和大家共享一 ...

  2. LeetCode Paint House II

    原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...

  3. python_excel

    1. xlrd, xlwt, xlutils的关系 Python中一般使用xlrd(excel read)来读取Excel文件,使用xlwt(excel write)来生成Excel文件(可以控制Ex ...

  4. postgresql查询的处理过程

    本文简单描述了Postgresql服务器接收到查询后到返回给客户端结果之间一般操作顺序,总的流程图如下: 第一步: 客户端程序可以是任何符合 PostgreSQL 协议规范的程序,如 JDBC 驱动. ...

  5. Java学习笔记,第三章

    Java基础语法 3.1类型.变量与运算符 3.1.1类型 Java可分为基本类型和类类型或参考类型.基本类型主要有 整数:可分为short整数(2字节,-32768 -- 32767),int整数( ...

  6. [Android Tips] 23. How to fail/stop Gradle task immediately if some conditions are not met

    throw new GradleException("conditions are not met") 参考 How to fail/stop task immediately i ...

  7. 日志时间格式有s,ms,us,如何排序最大10行

    这个比较繁琐,谁有更好方法?告诉我  [root@module tmp]# cat oldboy.txt       12s120001ms12000000us13s[root@module tmp] ...

  8. [转]在Ubuntu 14.04安装和使用Docker

    在Ubuntu 14.04安装和使用Docker 作者:chszs,版权所有,未经同意,不得转载.博主主页:http://blog.csdn.net/chszs Docker是一个开源软件,它可以把一 ...

  9. C# ref、out、params与值类型参数修饰符

    1.值类型: static void Main(string[] args) { ; ; NumVal(a, b); Console.WriteLine("a={0},b={1}" ...

  10. 在Page_Loaded下删除PivotItem出错的解决方案

    之前我一个例子中出现无法再页面Loaded事件中删除PivotItem的情况,页面会报错 Value does not fall within the expected range. 附图 原因是因为 ...