当前位置: 首页>编程语言>正文

java stream 返回map中所有的value对象集合 mapper 返回一个list


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

  • 0. 测试数据
  • 1. 查询返回单个结果
  • 1.1. 返回单个 Map
  • PoemMapper.xml
  • PoemMapper.java
  • PoemMapperTest.java
  • 输出结果
  • 1.2. 返回单个 LinkedHashMap
  • PoemMapper.xml
  • PoemMapper.java
  • PoemMapperTest.java
  • 输出结果
  • 2. 查询返回多个结果
  • 2.1. 返回 List<Map>
  • PoemMapper.xml
  • PoemMapper.java
  • PoemMapperTest.java
  • 输出结果
  • 2.2. 返回 Map<Map>
  • PoemMapper.xml
  • PoemMapper.java
  • PoemMapperTest.java
  • 输出结果
  • 2.3. 返回 List<LinkedHashMap>
  • PoemMapper.xml
  • PoemMapper.java
  • PoemMapperTest.java
  • 输出结果
  • 2.4. 返回 Map<LinkedHashMap>
  • PoemMapper.xml
  • PoemMapper.java
  • PoemMapperTest.java
  • 输出结果
  • 3. 统计结果返回 List<Pair<Integer, Long>> 再转 Map<Integer, Long>>
  • PoemMapper.xml
  • PoemMapper.java
  • PoemMapperTest.java
  • 输出结果
  • 总结
  • 参考资料


0. 测试数据

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

1. 查询返回单个结果

1.1. 返回单个 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":"痴情癫"}}

1.2. 返回单个 LinkedHashMap

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

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":"笑虾"}

2. 查询返回多个结果

2.1. 返回 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":"恨灯小"}, 
	略。。。
]

2.2. 返回 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":"恨灯小"},
	略。。。
}

2.3. 返回 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":"笑虾"},
	略。。。
]

2.4. 返回 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":"笑虾"},
	略。。。
}

3. 统计结果返回 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<String, Long>> countByAuthor();

PoemMapperTest.java

List<Pair<String, Long>> list = poemMapper.countByAuthor();
Map<String, 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}

总结

  1. 需要保留SQLORDER By排序效果的地方用List
  2. 需要保留SQLSELECT 字段顺序的地方用LinkedHashMap
  3. 想将结果装进Map需要指定一个字段作为KEY
    3.1. 因为 key 是唯一的,所以通常会使用主键 作为 key
    3.2. 当然也可以利用这个特性,指定一个有重复值的字段,得到一个去重的结果。

参考资料

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

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


https://www.xamrdz.com/lan/5kb1924602.html

相关文章: