beego连接Mysql,生成WebAPI+CRUD和Swagger
1.安装工具,下载 Go: https://studygolang.com/dl/golang/go1.13.6.windows-amd64.msi
go get -u github.com/astaxie/beego go get -u github.com/beego/bee go get -u github.com/go-sql-driver/mysql
2.创建项目(注:须进入创建的目录,如: cd beevue)
bee api beevue
cd beevue
3.创建DB,导入数据
-- Dumping database structure for test CREATE DATABASE IF NOT EXISTS `test` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `test`; -- Dumping structure for table test.langgen CREATE TABLE IF NOT EXISTS `langgen` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pid` int(11) DEFAULT NULL, `langname` varchar(255) DEFAULT NULL, `module` varchar(255) DEFAULT NULL, `labelcode` varchar(255) DEFAULT NULL, `labelvalue` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
4.连接DB生成API
bee generate appcode -driver="mysql" -conn= "root:root@tcp(127.0.0.1:3306)/test?charset=utf8"
5.修改main.go,配置数据连接
package main import ( _ "beevue/routers" _ "github.com/go-sql-driver/mysql" "time" "github.com/astaxie/beego" "github.com/astaxie/beego/orm" ) func init(){ orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/iemes_v1?charset=utf8", 30, 30) orm.Debug = true orm.DefaultTimeLoc = time.UTC } func main() { if beego.BConfig.RunMode == "dev" { beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" } beego.Run() }
6.启动服务,浏览
bee run -gendoc=true -downdoc=true