beego之自动创建表
龙行 Golang 2020-3-5 951 0评论
目前beego支持三种数据引擎MySQL:github.com/go-sql-driver/mysql
PostgreSQL:github.com/lib/pq
Sqlite3:github.com/mattn/go-sqlite3
使用之前必须要先go get 然后import加载这个包 记得前面要加 "_"
我用的版本是默认没有安装orm的所以首先要安装ormgo get github.com/astaxie/beego/orm
接下来模拟创建一个User表package models
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"time"
)
type User struct {
Id int
Name string
Pass string
Sex int
CreateTime time.Time
Status int
HeadImg string
Article []*Article `orm:"reverse(many)"`
}
func init(){
//注册模型
orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/golang")
orm.RegisterModel(new(User)) //映射模型
orm.RunSyncdb("default",false,true) //第一格参数数据库别名一般为default
// , 第二个参数是否开启创建表
//第三格参数 是否更新表
}
评论一下
赞助站长
赞助站长X
版权申明:此文如未标注转载均为本站原创,自由转载请表明出处《龙行博客》。
本文网址:https://www.liaotaoo.cn/330.html