百木园-与人分享,
就是让自己快乐。

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\":[]}]}]

无论从事什么行业,只要做好两件事就够了,一个是你的专业、一个是你的人品,专业决定了你的存在,人品决定了你的人脉,剩下的就是坚持,用善良專業和真诚赢取更多的信任。


来源:https://www.cnblogs.com/phpper/p/17254640.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » Gorm 实现无限树形菜单

相关推荐

  • 暂无文章