博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用BEEGO建立一个基本的API框架
阅读量:6077 次
发布时间:2019-06-20

本文共 4357 字,大约阅读时间需要 14 分钟。

用BEE API命令生成框架。

然后自行更改MODELS,加入MYSQL支持ORM.

然后,自定义了字段的对应,表的名称等。

参考URL:

http://www.cnblogs.com/studyzy/p/6964612.html

main.go

package mainimport (	_ "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 = papihttpport = 5757runmode = devautorender = falsecopyrequestbody = trueEnableDocs = trueEnableAdmin = trueAdminAddr = "127.0.0.1"AdminPort = 5758

  models.go

package modelsimport (	"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 controllersimport (	"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.htmlpackage routersimport (	"pmlogapi/controllers"	"github.com/astaxie/beego")func init() {	ns := beego.NewNamespace("/v1",		beego.NSNamespace("/pmlog",			beego.NSInclude(				&controllers.pmLogController{},			),		),	)	beego.AddNamespace(ns)}

  

 

你可能感兴趣的文章
Eclipse遇到Initializing Java Tooling解决办法
查看>>
while((ch = getchar()) != '\n')
查看>>
好程序员web前端分享JS检查浏览器类型和版本
查看>>
Oracle DG 逻辑Standby数据同步性能优化
查看>>
exchange 2010 队列删除
查看>>
「翻译」逐步替换Sass
查看>>
H5实现全屏与F11全屏
查看>>
处理excel表的列
查看>>
C#数据采集类
查看>>
quicksort
查看>>
【BZOJ2019】nim
查看>>
四部曲
查看>>
LINUX内核调试过程
查看>>
【HDOJ】3553 Just a String
查看>>
Java 集合深入理解(7):ArrayList
查看>>
2019年春季学期第四周作业
查看>>
linux环境配置
查看>>
ASP.NET MVC中从前台页面视图(View)传递数据到后台控制器(Controller)方式
查看>>
lintcode:next permutation下一个排列
查看>>
一个想法(续二):换个角度思考如何解决IT企业招聘难的问题!
查看>>