项目需要从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();
}

最新文章

  1. plsql查询乱码问题解决
  2. Web前端开发高手进阶
  3. oracle数据库升级记(记一次10.2.0.3版本升级到11.2.0.1版本的过程)
  4. Android -- 使用ViewPager实现画廊效果
  5. 十进制转二进制and位运算符
  6. java攻城狮之路(Android篇)--widget_webview_metadata_popupwindow_tabhost_分页加载数据_菜单
  7. android 定时器的实现
  8. 1085. Perfect Sequence (25)
  9. PowerShell运行cmd命令
  10. 64位操作系统 注册 capicom.dll
  11. Android 开发经验
  12. linux下修改hostid
  13. spring Stack Overflow
  14. ios开发获取SIM卡信息
  15. [Spark性能调优] 源码补充 : Spark 2.1.X 中 Unified 和 Static MemoryManager
  16. 走近webpack(4)--css相关拓展
  17. 自定义GridControl编辑器
  18. Maven项目错误解决小结
  19. 国内Maven仓库 - 2018年5月2日15:54:10
  20. visual studio 2010 c++ fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或

热门文章

  1. 微信 {&quot;errcode&quot;:48001,&quot;errmsg&quot;:&quot;api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]&quot;}
  2. hibernate学习(8)——事务.连接池.锁 相关设置
  3. nginx中相关配置
  4. UML之类图
  5. Hire Me, Microsoft China
  6. 包管理工具Carthage使用
  7. IOS手动添加CoreData
  8. Power-BI 预警触发的设定
  9. Java提高篇——Java 异常处理
  10. 1.初识Linux