Mybatis 查询结果返回 MapList<Map>Pair
Posted 笑虾
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis 查询结果返回 MapList<Map>Pair相关的知识,希望对你有一定的参考价值。
Mybatis 查询结果返回 Map、List<Map>、Pair
测试数据
查询返回单个结果
返回单个 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":"笑虾"
查询返回多个结果
- 用
List
保留住SQL
中ORDER By
的排序。 - 用
LinkedHashMap
保留住SQL
中SELECT 字段
的排序。
返回 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
参考资料
javafx.util.Pair<K,V>
《Java8实战》 - 读书笔记 - Stream流操作2:用流收集数据:6.1 收集器简介 - toMap
以上是关于Mybatis 查询结果返回 MapList<Map>Pair的主要内容,如果未能解决你的问题,请参考以下文章