码迷,mamicode.com
首页 > 其他好文 > 详细

GORM关联查询

时间:2020-08-02 16:11:46      阅读:85      评论:0      收藏:0      [点我收藏+]

标签: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"
}

  

  

 

GORM关联查询

标签:bee   tab   mysqldb   日志   type   ast   error   ppc   beego   

原文地址:https://www.cnblogs.com/yzg-14/p/13418595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!