将SQL查询结果返回为Map组成的结果集

在xml文件里加上这段结果格式,分别是统计的字段和对应数量

<resultMap id="typeCountMap" type="java.util.Map">
       <result property="pointLocationType" column="point_location_type" jdbcType="VARCHAR"/>
       <result property="count" column="count" jdbcType="INTEGER"/>
</resultMap>

property是返回map的key,

colum是数据库字段名称,

jdbcType写上字段类型

下面查询引用这个resultMap即可

<select id="countType" resultMap="typeCountMap">
        select point_location_type ,count(*) num
        from ev_point_acquisition epa
        group by point_location_type
</select>

resultMap填上resultMap的id

返回结果格式是List<Map<String ,Integer>>

[
    {
      "pointLocationType": "核电站",
      "num": 10
    },
    {
      "pointLocationType": "雷达",
      "num": 9
    },
    {
      "pointLocationType": "机场",
      "num": 10
    },
    {
      "pointLocationType": "桥梁",
      "num": 10
    },
    {
      "pointLocationType": "水电站",
      "num": 10
    },
    {
      "pointLocationType": "医院",
      "num": 11
    },
    {
      "pointLocationType": "仓库",
      "num": 10
    },
    {
      "pointLocationType": "火车站",
      "num": 10
    },
    {
      "pointLocationType": "隧道",
      "num": 6
    }
  ]

如果想得到Map再处理一下即可

在xml文件里加上这段结果格式,分别是统计的字段和对应数量 property是返回map的key, colum是数据库字段名称, jdbcType写上字段类型 下面查询引用这个resultMap即可 resultMap填上resultMap的id 返回结果格式是List > [ { "pointLocationType": "核电站", "num": 10 }, { "pointLocationType": "雷达", "num": 9 }, { "pointLocationType": "机场", "num": 10 }, { "pointLocationType": "桥梁", "num": 10 }, { "pointLocationType": "水电站", "num": 10 }, { "pointLocationType": "医院", "num": 11 }, { "pointLocationType": "仓库", "num": 10 }, { "pointLocationType": "火车站", "num": 10 }, { "pointLocationType": "隧道", "num": 6 } ] 如果想得到Map再处理一下即可
经验分享 程序员 微信小程序 职场和发展