zoukankan      html  css  js  c++  java
  • 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();
    }
  • 相关阅读:
    shell (check return of each line)(PIPESTATUS[@])and sudoer
    mysql slow query---pt-query-digest----db structure consistency,monitor table records before and after transaction.
    http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html
    PHP数据过滤
    PHP二维数组排序函数
    LBS配置
    jQuery+Ajax+PHP+Mysql实现分页显示数据
    流量高,资费贵。且用且珍惜
    jQuery实现的全选、反选和不选功能
    jquery 点点滴滴小记
  • 原文地址:https://www.cnblogs.com/GoQC/p/6216967.html
Copyright © 2011-2022 走看看