详解 Mybatis 查询结果返回 Map、List集合

1. 查询返回单个结果

1.1. 返回单个 Map

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

NowjavaMapper.xml

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


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

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


NowjavaMapperTest.java

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


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

{"1":{"author":"nowjava.com","id":1,"title":"时代Java"}}

1.2. 返回单个 LinkedHashMap

查询结果能保持 SQL 语句中 select 字段1, 字段2... 的顺序。

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

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


NowjavaMapper.java

LinkedHashMap<String, Object> selectLinkedHashMap();


NowjavaMapperTest.java

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


输出结果
注意:LinkedHashMap中字段按SQL中的顺序显示。

{"id":1,"title":"时代Java","author":"nowjava.com"}

2. 查询返回多个结果

2.1. 返回 List<Map>

NowjavaMapper.xml

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


NowjavaMapper.java

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


NowjavaMapperTest.java

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


输出结果

[
    {"author":"nowjava.com","id":1,"title":"时代Java"},
    {"author":"nowjava.com","id":2,"title":"Java实例"},
    {"author":"nowjava.com","id":3,"title":"Java手册"}, 
    ...
]

2.2. 返回 Map<Map>

NowjavaMapper.xml

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


NowjavaMapper.java

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


NowjavaMapperTest.java

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


输出结果

{
    "1":{"author":"nowjava.com","id":1,"title":"时代Java"},
    "2":{"author":"nowjava.com","id":2,"title":"Java实例"},
    "3":{"author":"nowjava.com","id":3,"title":"Java手册"},
    ...
}

2.3. 返回 List<LinkedHashMap>

NowjavaMapper.xml

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


NowjavaMapper.java

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


NowjavaMapperTest.java

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


输出结果

[
    {"id":1,"title":"时代Java","author":"nowjava.com"},
    {"id":2,"title":"Java实例","author":"nowjava.com"},
    {"id":3,"title":"Java手册","author":"nowjava.com"},
    ...
]

2.4. 返回 Map<LinkedHashMap>

NowjavaMapper.xml

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


NowjavaMapper.java

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


NowjavaMapperTest.java

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


输出结果

{
    "1":{"id":1,"title":"时代Java","author":"nowjava.com"},
    "2":{"id":2,"title":"Java实例","author":"nowjava.com"},
    "3":{"id":3,"title":"Java手册","author":"nowjava.com"},
    ...
}

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

NowjavaMapper.xml

展开阅读全文

本文系作者在时代Java发表,未经许可,不得转载。

如有侵权,请联系nowjava@qq.com删除。

编辑于

关注时代Java

关注时代Java