得到數據庫樹型結構(父ID和子ID)

樹形結構:

就是一個文件夾下包含多個文件夾或者文件,文件夾下還會存在多個文件夾或者文件

!process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDQ0MDY0Mg==,size_16,color_FFFFFF,t_70)

在數據庫中怎麼存儲呢這種數據?

id parentId name
1 0 手機
2 1 智能手機
3 1 非智能手機
4 2 國產智能手機
5 2 非國產智能手機
6 4 華爲
7 4 小米
8 5 蘋果
9 5 三星

我們要得到的是什麼樣子呢?

[
  {
    "id": "1",
    "parentid": "0",
    "name": "手機",
    "children": [
      {
        "id": "2",
        "parentid": "1",
        "name": "智能手機",
        "children": [
          {
            "id": "4",
            "parentid": "2",
            "name": "國產智能手機",
            "children": [
              {
                "id": "6",
                "parentid": "4",
                "name": "華爲",
                "children": []
              },
              {
                "id": "7",
                "parentid": "4",
                "name": "小米",
                "children": []
              }
            ]
          },
          {
            "id": "5",
            "parentid": "2",
            "name": "非國產智能手機",
            "children": [
              {
                "id": "8",
                "parentid": "5",
                "name": "蘋果",
                "children": []
              },
              {
                "id": "9",
                "parentid": "5",
                "name": "三星",
                "children": []
              }
            ]
          }
        ]
      },
      {
        "id": "3",
        "parentid": "1",
        "name": "非智能手機",
        "children": []
      }
    ]
  }
]

實操

定義entity類

//數據庫內容
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="shouji", description="")
public class Shouji implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private String id;

    @TableField("pname")
    private String pname;

    @TableField("parentid")
    private String parentid;

}
//擴展類
@Data
@ToString
public class Shouji Node extends Shouji {

    List<Shouji > children;

}

定義mapper

    <!--public TeachplanNode selectList(String courseId);-->
    <select id="selectList"  resultType="com.lianxizhifu.springboot.entity.ShoujiNode">
        select * from Shouji
    </select>
List<ShoujiNode> selectList();

定義service

List<ShoujiNode> selectList();
    @Override
    public List<ShoujiNode> selectList(String id) {
        //定義得到的結果封裝
        List<ShoujiNode> shoujiNodes = shoujiMapper.selectList();
        //遞歸整理父ID和子ID關係
        List<ShoujiNode> shoujiNodes1 = this.recursionBuildingGroup(shoujiNodes , id);
        return shoujiNodes1 ;
    }

方法一使用java代碼遞歸

    //遞歸迭代樹型結構
    private List<ShoujiNode> recursionBuildingGroup
    					(List<ShoujiNode> ShoujiNodes, String parentId) {
        //定義接受樹型圖節點列表
        List<ShoujiNode> sonList = new ArrayList<>();
        //迭代數據得到所有節點
        for (TeachplanNode res : shoujiNodes) {
            //判斷此節點的父ID是否等於要得到的節點
            if (res.getParentid().equals(parentId)) {
                //遞歸調用當前的ID是否存在子節點
                List<ShoujiNode> list = recursionBuildingGroup
                								(shoujiNodes, res.getId());
                //得到對應的子節點的集合
                res.setChildren(list);
                sonList.add(res);
            }
        }
        return sonList;
    }

方法二使用SQL遞歸

三集菜單(三層)

<resultMap id="teachplanMap" type="com.lianxizhifu.springboot.entity.ShoujiNode">
        <id column="one_id" property="id"></id>
        <result column="one_pname" property="pname"></result>
        <result column="one_parentid" property="parentid"></result>
        <collection property="children" ofType="com.lianxizhifu.springboot.entity.ShoujiNode">
            <id column="tow_id" property="id"></id>
            <result column="tow_pname" property="pname"></result>
            <result column="tow_parentid" property="parentid"></result>
            <collection property="children"  ofType="com.lianxizhifu.springboot.entity.ShoujiNode">
                <id column="thret_id" property="id"></id>
                <result column="thret_pname" property="pname"></result>
                <result column="thret_parentid" property="parentid"></result>
            </collection>
        </collection>
    </resultMap>
    <!--public TeachplanNode selectList(String courseId);-->
    <select id="selectList" parameterType="java.lang.String"
            resultMap="teachplanMap">
        SELECT
        a.id one_id,
        a.pname one_pname,
        a.parentid one_parentid
        b.id tow_id,
        b.pname tow_pname,
        b.parentid tow_parentid
        c.id thret_id,
        c.pname thret_pname,
        c.parentid thret_parentid

        FROM
        teachplan a
        LEFT JOIN shosuji b ON b.parentid = a.id
        LEFT JOIN shosuji c ON c.parentid = b.id
        WHERE
        a.parentid = #{id}

        ORDER BY
        a.orderby,
        b.orderby
    </select>

定義controller

    @GetMapping("/selectList/{id}")
    public List<ShoujiNode> selectList(@PathVariable("id") String id) {
        List<ShoujiNode> shoujis= shoujiService.selectList(id);
        return shoujis;
    }

結果

[
  {
    "id": "1",
    "parentid": "0",
    "name": "手機",
    "children": [
      {
        "id": "2",
        "parentid": "1",
        "name": "智能手機",
        "children": [
          {
            "id": "4",
            "parentid": "2",
            "name": "國產智能手機",
            "children": [
              {
                "id": "6",
                "parentid": "4",
                "name": "華爲",
                "children": []
              },
              {
                "id": "7",
                "parentid": "4",
                "name": "小米",
                "children": []
              }
            ]
          },
          {
            "id": "5",
            "parentid": "2",
            "name": "非國產智能手機",
            "children": [
              {
                "id": "8",
                "parentid": "5",
                "name": "蘋果",
                "children": []
              },
              {
                "id": "9",
                "parentid": "5",
                "name": "三星",
                "children": []
              }
            ]
          }
        ]
      },
      {
        "id": "3",
        "parentid": "1",
        "name": "非智能手機",
        "children": []
      }
    ]
  }
]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章