简体   繁体   English

如何在spring boot中从JPA对象映射

[英]How to map from JPA Object in spring boot

good day all: I have a little issue I want you help me to resolve.大家好:我有一个小问题希望你帮我解决。 My problem is the next, I have to find the shorter distance of the stores according to google maps and according the latitude and longitude, anyway, I have the query that do these, my problem is when I call to jparepository it returns an array and I need to return me with a specific format.我的问题是下一个,我必须根据谷歌地图和经纬度找到更短的商店距离,无论如何,我有做这些的查询,我的问题是当我调用jparepository它返回一个数组和我需要以特定格式返回给我。

This is my end-point I'm trying to get from jparepository这是我试图从jparepository获得的jparepository

@RequestMapping(value = "api/promos/distance/", method = RequestMethod.GET)
public ResponseEntity<?> distance(@RequestParam(required = false) Double latitude, Double longitude, Long radius) {
    try {
        return ResponseEntity.status(HttpStatus.OK)
                .body(locationDAO.findDistance(latitude,longitude,radius));

    } catch (Exception ex){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.toString());
    }
}

this is my repository with the params and return 2 values这是我的带有参数的存储库并返回 2 个值

@Query(value = "select sp.id as id, \n" +
            "          ( 6371  * acos( cos( radians(:latitude) ) \n" +
            "      * cos( radians( sp.latitude )) \n" +
            "      * cos( radians( sp.longitude ) - radians(:longitude)) \n" +
            "      + sin( radians(:latitude) ) \n" +
            "      * sin( radians( sp.latitude ) ) ) ) AS distance \n" +
            "        from promo_locations sp \n" +
            "having distance < :radius ORDER BY distance ASC", nativeQuery = true)
    List<Object> findDistance(
            @Param("latitude") Double latitude,
            @Param("longitude") Double longitude,
            @Param("radius") Long radius);

and it show me this它向我展示了这个

[
    [
        1,
        137.94383549078196
    ],
    [
        2,
        137.94383549078196
    ]
]

but I need to show me in this format:但我需要以这种格式向我展示:

[
    {
        "id" : 1,
        "distance" : 137.94383549078196
    },
    {
        "id" : 2,
        "distance" :137.94383549078196
    }
]

If someone knows how to map or how to convert I will thank you so much如果有人知道如何映射或如何转换,我将非常感谢

take a look at this and this看看这个这个
basically you need to define SqlResultSetMapping for your native query基本上你需要为你的本地查询定义SqlResultSetMapping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM