Mybatis select返回值为map时,选取表字段的两列作为key,value

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

 

转载于:https://www.cnblogs.com/GoQC/p/6216967.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值