当前位置:   article > 正文

SpringDataJpa使用原生sql查询之后返回map类型 有key有value_jpa sql查询返回map类型,没数据报错

jpa sql查询返回map类型,没数据报错

有问题的写法

// 使用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);
  • 1
  • 2
  • 3

通常使用上述方式接收返回数据的朋友比较多 此时返回的数据是列表 不存在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房间"
    ]
  ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

正常写法

// 使用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);
  • 1
  • 2
  • 3

返回示例

{
  "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": ""
    }
  ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/284282
推荐阅读
相关标签
  

闽ICP备14008679号