在新建項目成功之后我們可以做一個簡單的動態增刪查改。
1.在models文件夾下新建models.go,根據模型新建表
package models
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"time"
)
//定義User實體
type User struct {
Id int64
Name string
Created time.Time `orm:"index"`
Age int64 `orm:"index"`
LastUpdate time.Time `orm:"index"`
High int64
Weight int64
Introduce string `orm:"size(500)"`
Phone string
Mail string
Adress string
work string
}
func RegisterDB() {
//注冊
orm.RegisterModel(new(User))
//注冊驅動
//orm.RegisterDriver("mysql", orm.db_mysql)
//注冊默認數據庫
orm.RegisterDataBase("default", "mysql", "root:123456@/myApp?charset=utf8", 30)
}
2.修改main.go,調用models.go中數據庫注冊
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"myApp/models"
_ "myApp/routers"
)
//注冊數據模型
func init() {
//注冊數據庫
models.RegisterDB()
}
func main() {
//開啟調試模式,打印到控制台
orm.Debug = true
//自動建表
orm.RunSyncdb("default", false, true)
beego.Run()
}
3.啟動項目,自動新建表


4.
