下载go并安装, 配置环境变量
export GOPATH=/Users/guands/dev/go
export GOBIN=$GOPATH/bin
PATH=$PATH:$GOBIN
进入到go目录src下, 执行以下命令:
go get github.com/astaxie/beego
go get github.com/beego/bee
如果安装失败, 需要配置环境变量:
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
安装bee:
go install github.com/astaxie/bee
验证bee:
bee version
CREATE TABLE `region` (
`code` varchar(32) NOT NULL COMMENT '行政编码',
`name` varchar(128) NOT NULL COMMENT '名称',
`parent_code` varchar(32) NOT NULL COMMENT '父级行政编码',
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='行政划分区域';
INSERT INTO `region` VALUES ('1101', '市辖区', '11');
INSERT INTO `region` VALUES ('110101', '东城区', '1101');
INSERT INTO `region` VALUES ('110101001', '东华门街道', '110101');
INSERT INTO `region` VALUES ('110101001001', '多福巷社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001002', '银闸社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001005', '东厂社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001006', '智德社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001007', '南池子社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001009', '灯市口社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001010', '正义路社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001013', '台基厂社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001014', '韶九社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101001015', '王府井社区居委会', '110101001');
INSERT INTO `region` VALUES ('110101002', '景山街道', '110101');
INSERT INTO `region` VALUES ('110101002001', '隆福寺社区居委会', '110101002');
INSERT INTO `region` VALUES ('110101002002', '吉祥社区居委会', '110101002');
INSERT INTO `region` VALUES ('110101002003', '黄化门社区居委会', '110101002');
INSERT INTO `region` VALUES ('110101002004', '钟鼓社区居委会', '110101002');
INSERT INTO `region` VALUES ('110101002005', '魏家社区居委会', '110101002');
INSERT INTO `region` VALUES ('110101002006', '汪芝麻社区居委会', '110101002');
INSERT INTO `region` VALUES ('110101002008', '景山东街社区居委会', '110101002');
INSERT INTO `region` VALUES ('110101002009', '皇城根北街社区居委会', '110101002');
INSERT INTO `region` VALUES ('110101003', '交道口街道', '110101');
INSERT INTO `region` VALUES ('110101003001', '交东社区居委会', '110101003');
INSERT INTO `region` VALUES ('110101003002', '福祥社区居委会', '110101003');
INSERT INTO `region` VALUES ('110101003003', '大兴社区居委会', '110101003');
INSERT INTO `region` VALUES ('110101003005', '府学社区居委会', '110101003');
INSERT INTO `region` VALUES ('110101003007', '鼓楼苑社区居委会', '110101003');
INSERT INTO `region` VALUES ('110101003008', '菊儿社区居委会', '110101003');
INSERT INTO `region` VALUES ('110101003009', '南锣鼓巷社区居委会', '110101003');
INSERT INTO `region` VALUES ('110101004', '安定门街道', '110101');
代码结构:
main.go
package main
import (
"beego_demo/lib/mysql"
_ "beego_demo/routers"
"github.com/astaxie/beego"
)
func main() {
// 初始化MySQL
mysql.Init()
beego.Run()
}
app.conf
appname = beego_demo
httpport = 8089
runmode = dev
## 用于将json请求参数转变成对象
copyrequestbody = true
mysql.go
package mysql
import (
"beego_demo/models"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
func Init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:root@tcp(localhost:3306)/test?charset=utf8")
orm.SetMaxIdleConns("default", 10)
orm.SetMaxOpenConns("default", 100)
// 注册model
orm.RegisterModel(new(models.Region))
// debug环境下会打印sql语句
orm.Debug = true
}
func Create(param interface{}) (int64, error) {
return orm.NewOrm().Insert(param)
}
func Read(md interface{}, cols ...string) error {
return orm.NewOrm().Read(md, cols...)
}
region.go
package models
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
const REGION_TABLE_NAME string = "region"
type Region struct {
Code string `orm:"pk;column(code)" json:"code"`
Name string `json:"name"`
ParentCode string `json:"parentCode"`
}
func (r *Region) TableName() string {
return REGION_TABLE_NAME
}
func ListRegion(parentCode string) (data []Region, err error) {
qt := orm.NewOrm().QueryTable(REGION_TABLE_NAME)
// 根据参数拼接条件
if parentCode != "" {
qt = qt.Filter("parent_code", parentCode)
}
_, err = qt.All(&data)
return
}
Controller
index.go
package controllers
import (
"beego_demo/lib/mysql"
"beego_demo/models"
"github.com/astaxie/beego"
)
type IndexController struct {
beego.Controller
}
// 定义响应结构
type IndexResp struct {
Id string
Name string
Age int8
Region models.Region
}
func (c *IndexController) Index() {
// 定义查询条件
region := models.Region{Name: "测试"}
// 按照name检索, 将region指针传入进去, 方法内会将查询结果赋值到该结构体
mysql.Read(®ion, "name")
// 返回json结构数据
c.Data["json"] = &IndexResp{"ID_0001", "GuanDS", 18, region}
c.ServeJSON()
}
region.go(controller包下)
package controllers
import (
"beego_demo/lib/mysql"
"beego_demo/models"
"encoding/json"
"github.com/astaxie/beego"
)
type RegionController struct {
beego.Controller
}
func (c *RegionController) List() {
// 定义返回结果 数组
var listRegion []models.Region
// 获取请求参数(parentCode) http://localhost:8089/region?parentCode=1101
listRegion, _ = models.ListRegion(c.GetString("parentCode"))
c.Data["json"] = &listRegion
c.ServeJSON()
}
func (c *RegionController) Create() {
// 定义入参对象
var region models.Region
// 将json参数转变成对象
_ = json.Unmarshal(c.Ctx.Input.RequestBody, ®ion)
// 插入到数据表
createRegion, err := mysql.Create(®ion)
println("执行结果: %d", createRegion)
result := make(map[string]string)
if err != nil {
result["code"] = "1"
result["message"] = "FAILED"
} else {
result["code"] = "0"
result["message"] = "SUCCESS"
}
c.Data["json"] = result
c.ServeJSON()
}
router.go
package routers
import (
"beego_demo/controllers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{})
// 解析请求/index, GET 路由到IndexController.Index()
beego.Router("/index", &controllers.IndexController{}, "get:Index")
// 解析请求/region/list, GET 路由到RegionController.List()
beego.Router("/region/list", &controllers.RegionController{}, "get:List")
// 解析请求/region/create, POST 路由到RegionController.Create()
beego.Router("/region/create", &controllers.RegionController{}, "post:Create")
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/186259e80e284058b847b9a972f74c64.png