1. 程式人生 > >web開發遇到的問題與解決思路

web開發遇到的問題與解決思路

最近開發一個船舶專案,遇到的問題

[問題][6]:一個船舶上物料有很多分類,分類下面還存在分類,分類下面還存在分類,有一些分類下面只存在2級關係分類,有的是3級,有的是4級,如果是最終的類別還要帶具體物料的名字以及基本資訊:

[解決思路][6]:

1.建立表結構

以下為關鍵欄位

欄位名字 型別 備註
storesId bigint(20) 物料ID,主鍵
parentId bigint(20) 父類id
name varchar(64) 物料名稱
storesType varchar(64) 物料or類別

2.由於主鍵唯一性,所以parentId放上一類別的主鍵id,依次迴圈放置parentId,然後迴圈查詢出來就行裡,這樣依賴,看似解決了,問題出在查詢這裡,因為存在2級3級4級等不同的依賴關係,查詢要查詢多少次呢?查詢2次不行,4次也不行。

3.然後只能用遞迴查詢來解決問題了,[程式碼如下][6]

這裡寫圖片描述

4.查詢以json格式傳回前端頁面

{
  "code": "200",
  "message": "OK",
  "data": [
    {
      "displayOrder": 4,
      "status": "NORMAL",
      "createBy
": 1, "createTime": 1463977621000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463977786000, "storesId": 10067, "storesType": "CATEGORY", "code": "WL0002", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit
": "T", "parentId": 0, "children": [ { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463987635000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463987727000, "storesId": 10070, "storesType": "CATEGORY", "code": "SS0001", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 10067, "children": [ { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463991922000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463991922000, "storesId": 10078, "storesType": "CATEGORY", "code": "SS0011", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 10070, "children": [ { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463991946000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463991946000, "storesId": 10079, "storesType": "CATEGORY", "code": "SS0012", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 10078, "children": null } ] } ] }, { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463987643000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463987732000, "storesId": 10071, "storesType": "CATEGORY", "code": "SS0002", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 10067, "children": null } ] }, { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463977646000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463977787000, "storesId": 10068, "storesType": "CATEGORY", "code": "WL0003", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 0, "children": [ { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463991817000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463991816000, "storesId": 10076, "storesType": "CATEGORY", "code": "SS0009", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 10068, "children": null }, { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463991865000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463991864000, "storesId": 10077, "storesType": "CATEGORY", "code": "SS0010", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 10068, "children": null } ] }, { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463977660000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463977790000, "storesId": 10069, "storesType": "CATEGORY", "code": "WL0004", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 0, "children": null }, { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463991048000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463991047000, "storesId": 10072, "storesType": "CATEGORY", "code": "SS0003", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 0, "children": null }, { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463991240000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463991240000, "storesId": 10073, "storesType": "CATEGORY", "code": "SS0004", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 0, "children": null }, { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463991611000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463991611000, "storesId": 10074, "storesType": "CATEGORY", "code": "SS0005", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 0, "children": null }, { "displayOrder": 4, "status": "NORMAL", "createBy": 1, "createTime": 1463991811000, "updateBy": 0, "updateTime": null, "version": 0, "lastUpdate": 1463991815000, "storesId": 10075, "storesType": "CATEGORY", "code": "SS0008", "name": "物料2號", "specification": "12×1", "description": "沒有描述", "unit": "T", "parentId": 0, "children": null } ]
}

5.如果有更好的解決思路,可以留言交流。