首页 > 其他分享 >Gorm 实现无限树形菜单

Gorm 实现无限树形菜单

时间:2023-03-25 14:12:58浏览次数:49  
标签:city 菜单 name INSERT pid id 树形 VALUES Gorm

原文链接:https://www.zhoubotong.site/post/91.html
通常树形菜单的实现基本就是递归调用,大部分场景毕竟这种数据不多,性能倒是并不突出, 下面给个demo,有兴趣的朋友可以看看: 新建一个city表:

CREATE TABLE `city` (
  `id` int NOT NULL AUTO_INCREMENT,
  `pid` int NOT NULL DEFAULT '0' COMMENT '父级别id',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '0' COMMENT '名称',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `pid` (`pid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

测试数据:

INSERT INTO `city` (`id`, `pid`, `name`) VALUES (1, 0, '广东省');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (2, 1, '广州市');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (3, 2, '广州天河区');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (4, 2, '海珠区');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (5, 0, '福建省');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (6, 5, '厦门市');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (7, 4, '新港');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (8, 4, '沙园');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (9, 7, '新港一区');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (10, 0, '湖北省');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (11, 10, '武汉市');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (12, 11, '黄陂区');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (13, 11, '洪山区');
INSERT INTO `city` (`id`, `pid`, `name`) VALUES (14, 12, '黄陂天河街道');

demo.go:

package main

import (
    "encoding/json"
    "fmt"

    "gorm.io/driver/mysql"
    "gorm.io/gorm"
)

var DB gorm.DB

func (u TreeNode) TableName() string {
    return "city"
}

type TreeNode struct {
    Id       int         `json:"id"`
    Pid      int         `json:"pid" gorm:"index"`
    Name     string      `json:"name" gorm:"size:255"`
    Children []*TreeNode `json:"children" gorm:"-"`
}

func init() {
    dsn := "root:uos123456@tcp(11213:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local"
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
    if err != nil {
        panic("failed to connect database")
    }
    DB = *db
}
func main() {

    data := TreeList(10) // 查询武汉市下的子节点
    str, _ := json.Marshal(data)
    fmt.Printf("%s\\n", str)

}

// TreeList 递归处理子节点list
func TreeList(pid int) []*TreeNode {
    var list []*TreeNode
    DB.Model(TreeNode{}).Where("pid= ?", pid).Find(&list)
    result := make([]*TreeNode, 0)
    for _, v := range list {
        if v.Pid == pid {
            v.Children = TreeList(v.Id)
            if v.Children == nil {
                v.Children = make([]*TreeNode, 0)
            }
            result = append(result, v)
        }
    }

    return result

}

输出结果:

[Running] go run "/media/uos/G/web/demo-go/demo.go"
[{"id":11,"pid":10,"name":"武汉市","children":[{"id":12,"pid":11,"name":"黄陂区","children":[{"id":14,"pid":12,"name":"黄陂天河街道","children":[]}]},{"id":13,"pid":11,"name":"洪山区","children":[]}]}]

标签:city,菜单,name,INSERT,pid,id,树形,VALUES,Gorm
From: https://www.cnblogs.com/phpper/p/17254640.html

相关文章