Mybatis 查询结果返回 MapList<Map>Pair

Posted 笑虾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis 查询结果返回 MapList<Map>Pair相关的知识,希望对你有一定的参考价值。

Mybatis 查询结果返回 Map、List<Map>、Pair

测试数据

数据库 SQL测试数据 - 笑虾原创诗词表

查询返回单个结果

返回单个 Map

设置返回值类型 resultType="java.util.Map"

PoemMapper.xml

    <select id="selectMap" resultType="java.util.Map">
        SELECT id, title, author FROM poem LIMIT 1   
    </select>

PoemMapper.java

Map为最外层容器时就要加 @MapKey("id") 指定提取 id 作为 key

    @MapKey("id")
    Map<Long, Object> selectMap();

PoemMapperTest.java

    @Test
    public void selectMap() 
        Map<Long, Object> map = poemMapper.selectMap();
        System.out.println(JSON.toJSONString(map));
      

输出结果

注意:Map是无序的,所以这里的字段并没有按SQL中的顺序来显示。

"1":"author":"笑虾","id":1,"title":"痴情癫"

返回单个 LinkedHashMap

用来实现按SQL中的顺序来显示字段。

PoemMapper.xml

设置返回值类型 resultType="java.util.LinkedHashMap"

    <select id="selectLinkedHashMap" resultType="java.util.LinkedHashMap">
        SELECT id, title, author FROM poem LIMIT 1
    </select>

PoemMapper.java

LinkedHashMap<String, Object> selectLinkedHashMap();    

PoemMapperTest.java

LinkedHashMap<String, Object> linkedHashMap = poemMapper.selectLinkedHashMap();

输出结果

注意:LinkedHashMap字段SQL中的顺序显示。

"id":1,"title":"痴情癫","author":"笑虾"

查询返回多个结果

  1. List保留住SQLORDER By的排序。
  2. LinkedHashMap保留住SQLSELECT 字段的排序。

返回 List<Map>

PoemMapper.xml

    <select id="selectMapList" resultType="java.util.Map">
        SELECT id, title, author FROM poem
    </select>

PoemMapper.java

List<Map<String, Object>> selectMapList();

PoemMapperTest.java

List<Map<String, Object>> maps = poemMapper.selectMapList();

输出结果

[
	"author":"笑虾","id":1,"title":"痴情癫",
	"author":"笑虾","id":2,"title":"爱云说",
	"author":"笑虾","id":3,"title":"恨灯小", 
	略。。。
]

返回 Map<Map>

PoemMapper.xml

    <select id="selectMapMap" resultType="java.util.Map">
        SELECT id, title, author FROM poem 
    </select>

PoemMapper.java

@MapKey("id")
Map<String, Map<String, Object>> selectMapMap();

PoemMapperTest.java

Map<String, Map<String, Object>> stringMapMap = poemMapper.selectMapMap();

输出结果


	"1":"author":"笑虾","id":1,"title":"痴情癫",
	"2":"author":"笑虾","id":2,"title":"爱云说",
	"3":"author":"笑虾","id":3,"title":"恨灯小",
	略。。。

返回 List<LinkedHashMap>

PoemMapper.xml

    <select id="selectListLinkedHashMap" resultType="java.util.LinkedHashMap">
        SELECT id, title, author FROM poem
    </select>

PoemMapper.java

List<LinkedHashMap<String, Object>> selectListLinkedHashMap();    

PoemMapperTest.java

List<LinkedHashMap<String, Object>> linkedHashMap = poemMapper.selectListLinkedHashMap();

输出结果

[
	"id":1,"title":"痴情癫","author":"笑虾",
	"id":2,"title":"爱云说","author":"笑虾",
	"id":3,"title":"恨灯小","author":"笑虾",
	略。。。
]

返回 Map<LinkedHashMap>

PoemMapper.xml

    <select id="selectMapLinkedHashMap" resultType="java.util.LinkedHashMap">
        SELECT id, title, author FROM poem
    </select>

PoemMapper.java

@MapKey("id")
Map<String, LinkedHashMap<String, Object>> selectMapLinkedHashMap();

PoemMapperTest.java

Map<String, LinkedHashMap<String, Object>> mapLinkedHashMaps = poemMapper.selectMapLinkedHashMap();

输出结果


	"1":"id":1,"title":"痴情癫","author":"笑虾",
	"2":"id":2,"title":"爱云说","author":"笑虾",
	"3":"id":3,"title":"恨灯小","author":"笑虾",
	略。。。

统计结果返回 List<Pair<Integer, Long>> 再转 Map<Integer, Long>>

PoemMapper.xml

    <select id="countByAuthor" resultType="javafx.util.Pair">
        SELECT author,	count( id ) AS `数量` FROM	poem GROUP BY author
    </select>

PoemMapper.java

List<Pair<Integer, Long>> countByAuthor();

PoemMapperTest.java

List<Pair<Integer, Long>> list = poemMapper.countByAuthor();
Map<Integer, Long> map = list.stream()
        .collect(Collectors.toMap(Pair::getKey, Pair::getValue));

输出结果

查询结果返回的是这样的一个List<Pair>List<Map>实现也可以。

["key":"笑虾","value":16,"key":"金小侠","value":3]

还需要用Collectors.toMap转为Map才得到我们想要的最终效果。

"笑虾":16,"金小侠":3

参考资料

笑虾:Mybatis 查询结果返回 Optional<T>

javafx.util.Pair<K,V>
《Java8实战》 - 读书笔记 - Stream流操作2:用流收集数据:6.1 收集器简介 - toMap

以上是关于Mybatis 查询结果返回 MapList<Map>Pair的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis 查询结果返回 Optional<T>

Mybatis 查询结果返回 Optional<T>

mybatis中查询结果为空时不同返回类型对应返回值

java mybatis 查询列表会返回为null吗

关于Mybatis将查询结果中添加常量列并返回

使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法