
大家好,又见面了,我是你们的朋友全栈君。
设置返回值类型 resultType="java.util.Map"
<select id="selectMap" resultType="java.util.Map">
SELECT id, title, author FROM poem LIMIT 1
</select>以Map为最外层容器时就要加 @MapKey("id") 指定提取 id 作为 key。
@MapKey("id")
Map<Long, Object> selectMap(); @Test
public void selectMap() {
Map<Long, Object> map = poemMapper.selectMap();
System.out.println(JSON.toJSONString(map));
} 注意:Map是无序的,所以这里的字段并没有按SQL中的顺序来显示。
{
"1":{
"author":"笑虾","id":1,"title":"痴情癫"}}用来实现按SQL中的顺序来显示字段。
设置返回值类型 resultType="java.util.LinkedHashMap" 。
<select id="selectLinkedHashMap" resultType="java.util.LinkedHashMap">
SELECT id, title, author FROM poem LIMIT 1
</select>LinkedHashMap<String, Object> selectLinkedHashMap(); LinkedHashMap<String, Object> linkedHashMap = poemMapper.selectLinkedHashMap();注意:LinkedHashMap中字段按SQL中的顺序显示。
{
"id":1,"title":"痴情癫","author":"笑虾"}List保留住SQL中ORDER By的排序。LinkedHashMap保留住SQL中SELECT 字段的排序。 <select id="selectMapList" resultType="java.util.Map">
SELECT id, title, author FROM poem
</select>List<Map<String, Object>> selectMapList();List<Map<String, Object>> maps = poemMapper.selectMapList();[
{
"author":"笑虾","id":1,"title":"痴情癫"},
{
"author":"笑虾","id":2,"title":"爱云说"},
{
"author":"笑虾","id":3,"title":"恨灯小"},
略。。。
] <select id="selectMapMap" resultType="java.util.Map">
SELECT id, title, author FROM poem
</select>@MapKey("id")
Map<String, Map<String, Object>> selectMapMap();Map<String, Map<String, Object>> stringMapMap = poemMapper.selectMapMap();{
"1":{
"author":"笑虾","id":1,"title":"痴情癫"},
"2":{
"author":"笑虾","id":2,"title":"爱云说"},
"3":{
"author":"笑虾","id":3,"title":"恨灯小"},
略。。。
} <select id="selectListLinkedHashMap" resultType="java.util.LinkedHashMap">
SELECT id, title, author FROM poem
</select>List<LinkedHashMap<String, Object>> selectListLinkedHashMap(); List<LinkedHashMap<String, Object>> linkedHashMap = poemMapper.selectListLinkedHashMap();[
{
"id":1,"title":"痴情癫","author":"笑虾"},
{
"id":2,"title":"爱云说","author":"笑虾"},
{
"id":3,"title":"恨灯小","author":"笑虾"},
略。。。
] <select id="selectMapLinkedHashMap" resultType="java.util.LinkedHashMap">
SELECT id, title, author FROM poem
</select>@MapKey("id")
Map<String, LinkedHashMap<String, Object>> selectMapLinkedHashMap();Map<String, LinkedHashMap<String, Object>> mapLinkedHashMaps = poemMapper.selectMapLinkedHashMap();{
"1":{
"id":1,"title":"痴情癫","author":"笑虾"},
"2":{
"id":2,"title":"爱云说","author":"笑虾"},
"3":{
"id":3,"title":"恨灯小","author":"笑虾"},
略。。。
} <select id="countByAuthor" resultType="javafx.util.Pair">
SELECT author, count( id ) AS `数量` FROM poem GROUP BY author
</select>List<Pair<Integer, Long>> countByAuthor();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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185900.html原文链接:https://javaforall.cn