标签:bee tab mysqldb 日志 type ast error ppc beego
conf
appname = beegogorm httpport = 8080 runmode = dev mysqladmin="root" mysqlpwd="123456" mysqldb="itying"
models
core.go
package models import ( "github.com/astaxie/beego" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" ) var DB *gorm.DB var err error func init() { mysqladmin := beego.AppConfig.String("mysqladmin") mysqlpwd := beego.AppConfig.String("mysqlpwd") mysqldb := beego.AppConfig.String("mysqldb") //和数据库建立连接 // DB, err = gorm.Open("mysql", "root:123456@/beego?charset=utf8&parseTime=True&loc=Local") DB, err = gorm.Open("mysql", mysqladmin+":"+mysqlpwd+"@/"+mysqldb+"?charset=utf8&parseTime=True&loc=Local") DB.LogMode(true) //开启sql日志 if err != nil { beego.Error() } }
article.go
package models import ( _ "github.com/jinzhu/gorm" ) type Article struct { Id int `json:"id"` Title string `json:"title"` CateId string `json:"cate_id"` State int `json:"state"` ArticleCate ArticleCate `gorm:"foreignkey:Id;association_foreignkey:CateId"` } func (Article) TableName() string { return "article" }
articleCate.go
package models import ( _ "github.com/jinzhu/gorm" ) type ArticleCate struct { Id int `json:"id"` Title string `json:"title"` State int `json:"state"` Article []Article `gorm:"foreignkey:CateId;association_foreignkey:Id"` } func (ArticleCate) TableName() string { return "article_cate" }
标签:bee tab mysqldb 日志 type ast error ppc beego
原文地址:https://www.cnblogs.com/yzg-14/p/13418595.html