用BEE API命令生成框架。
然后自行更改MODELS,加入MYSQL支持ORM.
然后,自定义了字段的对应,表的名称等。
参考URL:
http://www.cnblogs.com/studyzy/p/6964612.html
main.go
package main import ( _ "papi/routers" "github.com/astaxie/beego" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) func init() { orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDataBase("default", "mysql", "user:pwd@tcp(1ip2:3306)/DB?charset=utf8") orm.SetMaxIdleConns("default", 1000) orm.SetMaxOpenConns("default", 2000) } func main() { orm.Debug = true if beego.BConfig.RunMode == "dev" { beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" } beego.Run() }
conf/app.conf
appname = papi httpport = 5757 runmode = dev autorender = false copyrequestbody = true EnableDocs = true EnableAdmin = true AdminAddr = "127.0.0.1" AdminPort = 5758
models.go
package models import ( "fmt" "time" "github.com/astaxie/beego/orm" ) type pmLog struct { Id int `orm:"column(id)"` DeployName string `orm:"column(deployName)"` AppName string `orm:"column(appName)"` SiteName string `orm:"column(siteName)"` IpAddress string `orm:"column(ipAddress)"` EnvType string `orm:"column(envType)"` UserName string `orm:"column(userName)"` OperationType string `orm:"column(operationType)"` OperationNo int `orm:"column(operationNo)"` LogContent string `orm:"column(logContent)"` LogDateTime time.Time `orm:"auto_now_add;type(datetime);column(logDateTime)"` } func (u *pmLog) TableName() string { return "pmlog" } func init() { orm.RegisterModel(new(pmLog)) } func GetAllpmLog() []*pmLog { o := orm.NewOrm() o.Using("default") var pmlogs []*pmLog q := o.QueryTable("pmlog") q.All(&pmlogs) return pmlogs } func GetpmLogById(id int) pmLog { u := pmLog{Id: id} o := orm.NewOrm() o.Using("default") err := o.Read(&u) if err == orm.ErrNoRows { fmt.Println("no result") } else if err == orm.ErrMissPK { fmt.Println("can't find PK") } return u } func AddpmLog(pmlog *pmLog) int { o := orm.NewOrm() o.Using("default") o.Insert(pmlog) return pmlog.Id } func UpdatepmLog(pmlog *pmLog) { o := orm.NewOrm() o.Using("default") o.Update(pmlog) } func DeletepmLog(id int) { o := orm.NewOrm() o.Using("default") o.Delete(&pmLog{Id: id}) }
controllers.go
package controllers import ( "pmlogapi/models" "encoding/json" "github.com/astaxie/beego" ) type pmLogController struct { beego.Controller } // @Title 获取所有部署日志 // @Description 返回所有的部署日志 // @Success 200 {object} models.pmLog // @router / [get] func (u *pmLogController) GetAll() { ss := models.GetAllpmLog() u.Data["json"] = ss u.ServeJSON() } // @Title 获取一条日志 // @Description 返回某条日志 // @Param id path int true "The key for staticblock" // @Success 200 {object} models.pmLog // @router /:id [get] func (u *pmLogController) GetById() { id, _ := u.GetInt(":id") s := models.GetpmLogById(id) u.Data["json"] = s u.ServeJSON() } // @Title 创建一条日志 // @Description 创建日志的描述 // @Param body body models.pmLog true "body for content" // @Success 200 {int} models.pmLog.Id // @Failure 403 body is empty // @router / [post] func (u *pmLogController) Post() { var s models.pmLog json.Unmarshal(u.Ctx.Input.RequestBody, &s) uid := models.AddpmLog(&s) u.Data["json"] = uid u.ServeJSON() } // @Title 修改日志 // @Description 修改日志的内容 // @Param body body models.pmLog true "body for pmlog content" // @Success 200 {int} models.pmLog // @Failure 403 body is empty // @router / [put] func (u *pmLogController) Update() { var s models.pmLog json.Unmarshal(u.Ctx.Input.RequestBody, &s) models.UpdatepmLog(&s) u.Data["json"] = s u.ServeJSON() } // @Title 删除日志 // @Description 删除指定的日志 // @Param id path int true "The key for staticblock" // @Success 200 {object} models.pmLog // @router /:id [delete] func (u *pmLogController) Delete() { id, _ := u.GetInt(":id") models.DeletepmLog(id) u.Data["json"] = true u.ServeJSON() }
router.go
// @APIVersion 1.0.0 // @Title beego Test API // @Description beego has a very cool tools to autogenerate documents for your API // @Contact astaxie@gmail.com // @TermsOfServiceUrl http://beego.me/ // @License Apache 2.0 // @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html package routers import ( "pmlogapi/controllers" "github.com/astaxie/beego" ) func init() { ns := beego.NewNamespace("/v1", beego.NSNamespace("/pmlog", beego.NSInclude( &controllers.pmLogController{}, ), ), ) beego.AddNamespace(ns) }