SpringDataJpa使用原生sql查询之后返回map类型 有key有value

有问题的写法

// 使用List<Object[]>接收原生sql查询后返回的数据
@Query(value = "select a.id as uid,a.name,a.token,b.id as aid,b.address,b.door from user a left join address b on a.id = b.uid where a.is_delete = 0 and a.id = :userId order by b.id desc", nativeQuery = true)
List<Object[]> getAddressAndUserInfo(@Param("userId") String userId);

通常使用上述方式接收返回数据的朋友比较多 此时返回的数据是列表 不存在key value形式

返回示例

{
  "code": "200",
  "message": "SUCCESS",
  "result": [
    [
      "U00000000001",
      "小哥",
      "",
      8,
      "哈哈",
      "8栋223房间"
    ],
    [
      "U00000000001",
      "小哥",
      "",
      7,
      "哈哈",
      "7栋223房间"
    ],
    [
      "U00000000001",
      "小哥",
      "",
      6,
      "哈哈",
      "6栋223房间"
    ],
    [
      "U00000000001",
      "小哥",
      "",
      5,
      "哈哈",
      "5栋223房间"
    ],
    [
      "U00000000001",
      "小哥",
      "",
      4,
      "哈哈",
      "4栋223房间"
    ],
    [
      "U00000000001",
      "小哥",
      "",
      3,
      "哈哈",
      "3栋223房间"
    ],
    [
      "U00000000001",
      "小哥",
      "",
      2,
      "哈哈",
      "2栋223房间"
    ],
    [
      "U00000000001",
      "小哥",
      "",
      1,
      "哈哈",
      "2栋223房间"
    ]
  ]
}

正常写法

// 使用List<Map>接收原生sql查询后返回的数据
@Query(value = "select a.id as uid,a.name,a.token,b.id as aid,b.address,b.door from user a left join address b on a.id = b.uid where a.is_delete = 0 and a.id = :userId order by b.id desc", nativeQuery = true)
List<Map> getAddressAndUserInfo(@Param("userId") String userId);

返回示例

{
  "code": "200",
  "message": "SUCCESS",
  "result": [
    {
      "uid": "U00000000001",
      "door": "8栋223房间",
      "name": "小哥",
      "aid": "8",
      "address": "哈哈",
      "token": ""
    },
    {
      "uid": "U00000000001",
      "door": "7栋223房间",
      "name": "小哥",
      "address": "哈哈",
      "aid": "7",
      "token": ""
    },
    {
      "uid": "U00000000001",
      "name": "小哥",
      "door": "6栋223房间",
      "address": "哈哈",
      "aid": "6",
      "token": ""
    },
    {
      "uid": "U00000000001",
      "name": "小哥",
      "door": "5栋223房间",
      "aid": "5",
      "address": "哈哈",
      "token": ""
    },
    {
      "uid": "U00000000001",
      "name": "小哥",
      "aid": "4",
      "address": "哈哈",
      "door": "4栋223房间",
      "token": ""
    },
    {
      "uid": "U00000000001",
      "name": "小哥",
      "address": "哈哈",
      "door": "3栋223房间",
      "aid": "3",
      "token": ""
    },
    {
      "uid": "U00000000001",
      "name": "小哥",
      "address": "哈哈",
      "door": "2栋223房间",
      "token": "",
      "aid": "2"
    },
    {
      "uid": "U00000000001",
      "name": "小哥",
      "address": "哈哈",
      "aid": "1",
      "door": "2栋223房间",
      "token": ""
    }
  ]
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页