将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再处理一下即可

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好,关于你的问题,你可以通过以下步骤来实现: 1. 查询数据库获取相关数据,比如获取父级菜单和子级菜单的数据。 2. 针对父级菜单的数据,将其遍历出来,并根据其 ID 对子菜单进行筛选,将子菜单添加到相应的父菜单的子节点中。 3. 最终将所有的父节点组成一个数组返回给前端渲染即可。 具体代码实现可参考以下示例: ``` const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '******', database: 'test_db' }); const getParentMenu = () => { return new Promise((resolve, reject) => { const sql = 'SELECT * FROM menu WHERE parent_id = 0'; connection.query(sql, (error, results, fields) => { if (error) { reject(error); } else { resolve(results); } }); }); }; const getChildMenusByParentId = (parentId) => { return new Promise((resolve, reject) => { const sql = `SELECT * FROM menu WHERE parent_id = ${parentId}`; connection.query(sql, (error, results, fields) => { if (error) { reject(error); } else { resolve(results); } }); }); }; const buildMenuTree = async () => { const parentMenus = await getParentMenu(); const menuTree = parentMenus.map((parent) => { const parentId = parent.id; const childMenus = getChildMenusByParentId(parentId); parent.children = childMenus; return parent; }); return menuTree; }; buildMenuTree().then((menuTree) => { console.log(menuTree); }).catch((error) => { console.log(error); }); ``` 以上示例代码仅供参考,具体实现方式需要根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值