【发布时间】:2021-05-31 01:46:19
【问题描述】:
我有一个 SQL 视图 (IdView),它具有以下值。我对此视图只有读取权限,对基础表没有任何访问权限。
+-------+-------+------------+------------+---------+-----------+----------------+
| ID | Name | Desc | Relation | ChildId | ChildName | ChildDesc |
+-------+-------+------------+------------+---------+-----------+----------------+
| 80121 | Car | Model A | Kits | 50123 | Bolt | Hexagonal Bolt |
| 80121 | Car | Model A | Kits | 50124 | Nut | 25mm Dia |
| 80121 | Car | Model A | Spare | 50125 | screw | Type A |
| 80121 | Car | Model A | Spare | 50126 | Shaft | 10m long |
| 80122 | Bike | Model H | Spare | 50127 | Oil | Standard oil |
+-------+-------+------------+------------+---------+-----------+----------------+
现在我必须在用户点击以下 URL 时提供以下响应,即对于 id 80121
http://localhost:8080/items?id=80121 。将有 2 个关系:Kits 和 Spare。我想将所有套件保存在一个键中,并且类似地备用在另一个键中,如下所示。
{
"Id": "80121",
"Name": "Car",
"Desc": "Model A",
"Kits": [
{
"Id": "50123",
"Name": "Bolt",
"Desc": "Hexagonal Bolt"
},
{
"Id": "50124",
"Name": "Nut",
"Desc": "25mm Dia"
},
],
"Spare": [
{
"Id": "50125",
"Name": "screw",
"Desc": "Type A"
},
{
"Id": "50126",
"Name": "Shaft",
"Desc": "10m long"
},
]
}
同样当用户点击 http://localhost:8080/items?id=80112
{
"Id": "80112",
"Name": "Bike",
"Desc": "Model H",
"Kits": [],
"Spare": [
{
"Id": "50127",
"Name": "Oil",
"Desc": "Standard oil"
}
]
}
每个请求只有一个 id。请帮助实现这一目标
我在下面试过了。
存储库
@Repository
public interface MyDataRepo extends JpaRepository<List, String> {
@Query(value="select ID,Name,Desc,Relation,ChildId,ChildName,ChildDesc from myview
WHERE ID=?1",nativeQuery=true)
List<Data> findAllCategory(String id);
public static interface Data {
String getid();
String getname();
String getdesc();
String getrelation();
String getchildid();
String getchildname();
String getchilddesc();
}
}
服务:
public List<Data> getMyData(String id) {
return repo.findAllCategory(id);
}
控制器:
@GetMapping("/items")
public ResponseEntity<List<Data>> retrieveData(@RequestParam("id") String id) {
List<Data> stud = service.getMyData(id);
return ResponseEntity.ok().body(stud);
}
80121 的电流输出:
[{
"Id": "80121",
"Name": "Car",
"Desc": "Model A",
"Relation":"Kits",
"ChildId":"50123",
"ChildName":"Bolt",
"ChildDesc":"Hexagonal Bolt"
}, {
"Id": "80121",
"Name": "Car",
"Desc": "Model A",
"Relation":"Kits",
"ChildId":"50124",
"ChildName":"Nut",
"ChildDesc":"25mm Dia"
}, {
"Id": "80121",
"Name": "Car",
"Desc": "Model A",
"Relation":"Spare",
"ChildId":"50125",
"ChildName":"screw",
"ChildDesc":"10m long "
}, {
"Id": "80121",
"Name": "Car",
"Desc": "Model A",
"Relation":"Spare",
"ChildId":"50126",
"ChildName":"Shaft",
"ChildDesc":"Pasted Seal"
}]
我是 Java 和 Spring Boot 的初学者。我应该使用视图列创建自定义 POJO 或实体吗?我不知道如何创建这个嵌套的 JSON 并继续进行。任何方向将不胜感激。
【问题讨论】:
标签: java json spring-boot hibernate jackson