beego 代码生成工具体验

mdjdot · · 1176 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

# beego 代码生成工具体验 ## 设计 MySQL 数据库 1. 通过 MySQL Workbench EER Digram 设计表和关系 ![image.png](https://static.studygolang.com/200626/7a33780bbf688321021bc24c6dafdb66.png) 2. 设置 MySQL Workbench 默认目标版本和 MySQL 服务器版本一致 ![image.png](https://static.studygolang.com/200626/c8bdc9157ef101c9d3bdaec663ee22ba.png) 3. 同步模型到数据库 ![image.png](https://static.studygolang.com/200626/d6e27a7702c252fea38ed2b942ddbfe5.png) ``` -- MySQL Workbench Synchronization -- Generated: 2020-06-26 12:07 -- Model: New Model -- Version: 1.0 -- Project: Name of the project -- Author: medea SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; CREATE TABLE IF NOT EXISTS `dotblog`.`users` ( `id` INT(11) NOT NULL COMMENT '用户ID', `name` VARCHAR(255) NOT NULL COMMENT '用户名称', `password` VARCHAR(255) NOT NULL COMMENT '用户密码', `email` VARCHAR(255) NOT NULL COMMENT '用户邮箱', `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '用户创建时间', `avatar` MEDIUMTEXT NULL DEFAULT NULL COMMENT '用户头像', PRIMARY KEY (`id`), UNIQUE INDEX `username_UNIQUE` (`name` ASC), UNIQUE INDEX `email_UNIQUE` (`email` ASC)) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS `dotblog`.`articles` ( `id` INT(11) NOT NULL COMMENT '文章ID', `title` VARCHAR(255) NULL DEFAULT NULL COMMENT '文章标题', `users_id` INT(11) NOT NULL COMMENT '文章用户', `content` TEXT(65535) NOT NULL COMMENT '文章内容', `create_time` TIMESTAMP NOT NULL COMMENT '文章创建时间', PRIMARY KEY (`id`), INDEX `fk_articles_users_idx` (`users_id` ASC), CONSTRAINT `fk_articles_users` FOREIGN KEY (`users_id`) REFERENCES `dotblog`.`users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS `dotblog`.`tags` ( `id` INT(11) NOT NULL COMMENT '标签ID', `tag` VARCHAR(255) NOT NULL COMMENT '标签值', PRIMARY KEY (`id`), UNIQUE INDEX `tag_UNIQUE` (`tag` ASC)) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS `dotblog`.`categories` ( `id` INT(11) NOT NULL COMMENT '类型ID', `category` VARCHAR(255) NOT NULL COMMENT '类型值', PRIMARY KEY (`id`), UNIQUE INDEX `category_UNIQUE` (`category` ASC)) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS `dotblog`.`article_comments` ( `id` INT(11) NOT NULL COMMENT '评论ID', `users_id` INT(11) NOT NULL COMMENT '评论用户', `articles_id` INT(11) NOT NULL COMMENT '评论文章', `comment` TEXT(65535) NOT NULL COMMENT '评论内容', `create_time` TIMESTAMP NOT NULL COMMENT '评论创建时间', PRIMARY KEY (`id`), INDEX `fk_comments_users1_idx` (`users_id` ASC), INDEX `fk_comments_articles1_idx` (`articles_id` ASC), CONSTRAINT `fk_comments_users1` FOREIGN KEY (`users_id`) REFERENCES `dotblog`.`users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_comments_articles1` FOREIGN KEY (`articles_id`) REFERENCES `dotblog`.`articles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS `dotblog`.`article_tags` ( `id` INT(11) NOT NULL COMMENT '文章标签ID', `articles_id` INT(11) NOT NULL COMMENT '文章ID', `tags_id` INT(11) NOT NULL COMMENT '标签ID', PRIMARY KEY (`id`), INDEX `fk_article_tags_articles1_idx` (`articles_id` ASC), INDEX `fk_article_tags_tags1_idx` (`tags_id` ASC), CONSTRAINT `fk_article_tags_articles1` FOREIGN KEY (`articles_id`) REFERENCES `dotblog`.`articles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_article_tags_tags1` FOREIGN KEY (`tags_id`) REFERENCES `dotblog`.`tags` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; CREATE TABLE IF NOT EXISTS `dotblog`.`article_categories` ( `id` INT(11) NOT NULL COMMENT '文章类型ID', `articles_id` INT(11) NOT NULL COMMENT '文章ID', `categories_id` INT(11) NOT NULL COMMENT '类型ID', INDEX `fk_article_categories_articles1_idx` (`articles_id` ASC), INDEX `fk_article_categories_categories1_idx` (`categories_id` ASC), PRIMARY KEY (`id`), CONSTRAINT `fk_article_categories_articles1` FOREIGN KEY (`articles_id`) REFERENCES `dotblog`.`articles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_article_categories_categories1` FOREIGN KEY (`categories_id`) REFERENCES `dotblog`.`categories` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; ``` ## bee api 命令生成项目 1. 执行 `bee api dotblog` 命令生成项目 2. 执行 `go mod init` 生成 go.mod ``` . ├── conf │ └── app.conf ├── controllers │ ├── object.go │ └── user.go ├── go.mod ├── main.go ├── models │ ├── object.go │ └── user.go ├── routers │ └── router.go └── tests └── default_test.go ``` 3. 执行 `bee generate appcode -driver=mysql -conn="user:pwd@tcp(127.0.0.1:3306)/dotblog" -level=3` 基于数据库生成 models、controllers、routers 代码文件 ``` . ├── conf │ └── app.conf ├── controllers │ ├── article_categories.go │ ├── article_comments.go │ ├── article_tags.go │ ├── articles.go │ ├── categories.go │ ├── object.go │ ├── tags.go │ ├── user.go │ └── users.go ├── go.mod ├── main.go ├── models │ ├── article_categories.go │ ├── article_comments.go │ ├── article_tags.go │ ├── articles.go │ ├── categories.go │ ├── object.go │ ├── tags.go │ ├── user.go │ └── users.go ├── routers │ └── router.go └── tests └── default_test.go ``` 4. 执行 `bee generate docs` 生成 swagger 接口文档 ``` . ... ├── swagger │ ├── swagger.json │ └── swagger.yml ... ``` 5. main.go 文件中加入如下代码 ``` import ( "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) func main() { orm.RegisterDataBase("default", "mysql", "user:pwd@tcp(127.0.0.1:3306)/dotblog?charset=utf8") } ``` 6. 执行 `bee run -gendoc=true -downdoc=true` 启动服务和文档 访问 "http://127.0.0.1:8080/v1/articles" 访问接口 访问 "http://127.0.0.1:8080/swagger" 查看接口文档 ![image.png](https://static.studygolang.com/200626/c0cd9298c6db4de70729bc88b0369272.png)

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1176 次点击  
加入收藏 微博
2 回复  |  直到 2020-06-28 09:28:02
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传