如何在Java中深度递归父子关系

时间:2019-03-01 06:24:43

标签: spring-boot

此数据集看起来像下面的查询结果,这是一个非常简单的TABLE dndclasses的输出; SQL命令:

id │ parent_id │           name            
════╪═══════════╪═══════════════════════════
  1 │         ¤ │ Warrior
  2 │         ¤ │ Wizard
  3 │         ¤ │ Priest
  4 │         ¤ │ Rogue
  5 │         1 │ Fighter
  6 │         1 │ Paladin
  7 │         1 │ Ranger
  8 │         2 │ Mage
  9 │         2 │ Specialist wizard
 10 │         3 │ Cleric
 11 │         3 │ Druid
 12 │         3 │ Priest of specific mythos
 13 │         4 │ Thief
 14 │         4 │ Bard
 15 │        13 │ Assassin
(15 rows)

output:我们现在需要的结果应如下所示:

[
    {
        "Name": "Wizard",
        "Sub Classes": ["Mage", "Specialist wizard"]
    }
    {
        "Name": "Priest",
        "Sub Classes": ["Cleric", "Druid", "Priest of specific mythos"]
    }
    {
        "Name": "Warrior",
        "Sub Classes": ["Fighter", "Paladin", "Ranger"]
    }
    {
        "Name": "Rogue",
        "Sub Classes": ["Thief", "Bard"]
    }
    {
        "Name": "Rogue",
        "Sub Classes": {
            "Name": "Thief", "Sub Classes": ["Assassin"]
        }
    }
]

0 个答案:

没有答案