使用Beego构建完整web项目 beego是Go生态中最受欢迎的框架之一,社区活跃、入门迅速。https://www.imooc.com/search/?words=beego
1.安装Go环境
2.安装或者升级Beego和Bee的开发工具
$ go get -u github.com/astaxie/beego
$ go get -u github.com/beego/bee
1
2
3.添加环境变量
$ open ~/.bash_profile
在打开的文件内输入以下环境变量进行配置
export GOPATH=${HOME}/go
export PATH=${PATH}:${GOPATH}/bin
然后 source ~/.bash_profile
bee new imooc
bee run
CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT ,
username varchar(128) NOT NULL DEFAULT '',
gender tinyint(4) NOT NULL DEFAULT 0,
age int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
insert into user ( username, gender ,age) values('zhangsan' ,1,21) ;
insert into user ( username, gender ,age) values('lisi' ,1,23) ;
insert into user ( username, gender ,age) values('xiaohong' ,2,24) ;
bee generate scaffold user -fields="id:int64,username:string,gender:int,age:int" -driver=mysql conn="root:@tcp( 127.0.0.1:3306)/imooc"
bee generate scaffold user -fields="id:int64,username:string,gender:int,age:int" -driver=mysql conn="root:@tcp( 127.0.0.1:3306)/imooc"
命令运行后 可都选Yes
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "imooc/routers"
_ "github.com/go-sql-driver/mysql"
)
func main() {
orm.RegisterDriver("mysql",orm.DRMySQL)
orm.RegisterDataBase("default","mysql","root:123456@tcp(127.0.0.1:3306)/imooc")
beego.Run()
}
package routers
import (
"github.com/astaxie/beego"
"imooc/controllers"
)
func init() {
//beego.Router("/", &controllers.MainController{})
beego.Include(&controllers.UserController{})
}
附上源码:https://download.csdn.net/download/fj_ut/11249483