大家好,又见面了,我是你们的朋友全栈君。
Mybatis 查询结果返回 Map、List
- 测试数据
- 查询返回单个结果
- 返回单个 Map
- PoemMapper.xml
- PoemMapper.java
- PoemMapperTest.java
- 输出结果
- 返回单个 LinkedHashMap
- PoemMapper.xml
- PoemMapper.java
- PoemMapperTest.java
- 输出结果
- 返回单个 Map
- 查询返回多个结果
- 返回 List<Map>
- PoemMapper.xml
- PoemMapper.java
- PoemMapperTest.java
- 输出结果
- 返回 Map<Map>
- PoemMapper.xml
- PoemMapper.java
- PoemMapperTest.java
- 输出结果
- 返回 List<LinkedHashMap>
- PoemMapper.xml
- PoemMapper.java
- PoemMapperTest.java
- 输出结果
- 返回 Map<LinkedHashMap>
- PoemMapper.xml
- PoemMapper.java
- PoemMapperTest.java
- 输出结果
- 返回 List<Map>
- 统计结果返回 List
> 再转 Map > - PoemMapper.xml
- PoemMapper.java
- PoemMapperTest.java
- 输出结果
- 参考资料
测试数据
数据库 SQL测试数据 – 笑虾原创诗词表
查询返回单个结果
返回单个 Map
设置返回值类型 resultType="java.util.Map"
PoemMapper.xml
<code style="margin-left:0"> <select id="selectMap" resultType="java.util.Map"> SELECT id, title, author FROM poem LIMIT 1 </select></code>
PoemMapper.java
以Map
为最外层容器时就要加 @MapKey("id")
指定提取 id
作为 key
。
<code style="margin-left:0"> @MapKey("id") Map<Long, Object> selectMap();</code>
PoemMapperTest.java
<code style="margin-left:0"> @Test public void selectMap() { Map<Long, Object> map = poemMapper.selectMap(); System.out.println(JSON.toJSONString(map)); } </code>
输出结果
注意:Map
是无序的,所以这里的字段
并没有按SQL
中的顺序
来显示。
<code style="margin-left:0">{ "1":{ "author":"笑虾","id":1,"title":"痴情癫"}}</code>
返回单个 LinkedHashMap
用来实现按SQL
中的顺序
来显示字段。
PoemMapper.xml
设置返回值类型 resultType="java.util.LinkedHashMap"
。
<code style="margin-left:0"> <select id="selectLinkedHashMap" resultType="java.util.LinkedHashMap"> SELECT id, title, author FROM poem LIMIT 1 </select></code>
PoemMapper.java
<code style="margin-left:0">LinkedHashMap<String, Object> selectLinkedHashMap(); </code>
PoemMapperTest.java
<code style="margin-left:0">LinkedHashMap<String, Object> linkedHashMap = poemMapper.selectLinkedHashMap();</code>
输出结果
注意:LinkedHashMap
中字段
按SQL
中的顺序
显示。
<code style="margin-left:0">{ "id":1,"title":"痴情癫","author":"笑虾"}</code>
查询返回多个结果
- 用
List
保留住SQL
中ORDER By
的排序。 - 用
LinkedHashMap
保留住SQL
中SELECT 字段
的排序。
返回 List<Map>
PoemMapper.xml
<code style="margin-left:0"> <select id="selectMapList" resultType="java.util.Map"> SELECT id, title, author FROM poem </select></code>
PoemMapper.java
<code style="margin-left:0">List<Map<String, Object>> selectMapList();</code>
PoemMapperTest.java
<code style="margin-left:0">List<Map<String, Object>> maps = poemMapper.selectMapList();</code>
输出结果
<code style="margin-left:0">[ { "author":"笑虾","id":1,"title":"痴情癫"}, { "author":"笑虾","id":2,"title":"爱云说"}, { "author":"笑虾","id":3,"title":"恨灯小"}, 略。。。 ]</code>
返回 Map<Map>
PoemMapper.xml
<code style="margin-left:0"> <select id="selectMapMap" resultType="java.util.Map"> SELECT id, title, author FROM poem </select></code>
PoemMapper.java
<code style="margin-left:0">@MapKey("id") Map<String, Map<String, Object>> selectMapMap();</code>
PoemMapperTest.java
<code style="margin-left:0">Map<String, Map<String, Object>> stringMapMap = poemMapper.selectMapMap();</code>
输出结果
<code style="margin-left:0">{ "1":{ "author":"笑虾","id":1,"title":"痴情癫"}, "2":{ "author":"笑虾","id":2,"title":"爱云说"}, "3":{ "author":"笑虾","id":3,"title":"恨灯小"}, 略。。。 }</code>
返回 List<LinkedHashMap>
PoemMapper.xml
<code style="margin-left:0"> <select id="selectListLinkedHashMap" resultType="java.util.LinkedHashMap"> SELECT id, title, author FROM poem </select></code>
PoemMapper.java
<code style="margin-left:0">List<LinkedHashMap<String, Object>> selectListLinkedHashMap(); </code>
PoemMapperTest.java
<code style="margin-left:0">List<LinkedHashMap<String, Object>> linkedHashMap = poemMapper.selectListLinkedHashMap();</code>
输出结果
<code style="margin-left:0">[ { "id":1,"title":"痴情癫","author":"笑虾"}, { "id":2,"title":"爱云说","author":"笑虾"}, { "id":3,"title":"恨灯小","author":"笑虾"}, 略。。。 ]</code>
返回 Map<LinkedHashMap>
PoemMapper.xml
<code style="margin-left:0"> <select id="selectMapLinkedHashMap" resultType="java.util.LinkedHashMap"> SELECT id, title, author FROM poem </select></code>
PoemMapper.java
<code style="margin-left:0">@MapKey("id") Map<String, LinkedHashMap<String, Object>> selectMapLinkedHashMap();</code>
PoemMapperTest.java
<code style="margin-left:0">Map<String, LinkedHashMap<String, Object>> mapLinkedHashMaps = poemMapper.selectMapLinkedHashMap();</code>
输出结果
<code style="margin-left:0">{ "1":{ "id":1,"title":"痴情癫","author":"笑虾"}, "2":{ "id":2,"title":"爱云说","author":"笑虾"}, "3":{ "id":3,"title":"恨灯小","author":"笑虾"}, 略。。。 }</code>
统计结果返回 List> 再转 Map>
PoemMapper.xml
<code style="margin-left:0"> <select id="countByAuthor" resultType="javafx.util.Pair"> SELECT author, count( id ) AS `数量` FROM poem GROUP BY author </select></code>
PoemMapper.java
<code style="margin-left:0">List<Pair<Integer, Long>> countByAuthor();</code>
PoemMapperTest.java
<code style="margin-left:0">List<Pair<Integer, Long>> list = poemMapper.countByAuthor(); Map<Integer, Long> map = list.stream() .collect(Collectors.toMap(Pair::getKey, Pair::getValue));</code>
输出结果
查询结果返回的是这样的一个List
。用List
实现也可以。
<code style="margin-left:0">[{ "key":"笑虾","value":16},{ "key":"金小侠","value":3}]</code>
还需要用Collectors.toMap
转为Map
才得到我们想要的最终效果。
<code style="margin-left:0">{ "笑虾":16,"金小侠":3}</code>
参考资料
笑虾:Mybatis 查询结果返回 Optional<T>
javafx.util.Pair
《Java8实战》 – 读书笔记 – Stream流操作2:用流收集数据:6.1 收集器简介 – toMap
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185900.html原文链接:https://javaforall.cn
未经允许不得转载:木盒主机 » mybatis返回值_存储过程获取查询结果