package models
import (
"fmt"
"github.com/beego/beego/v2/client/orm"
_ "github.com/go-sql-driver/mysql"
)
type Page struct {
Id int
Website string
Email string
}
func init() {
orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/page?charset=utf8")
orm.RegisterModel(new(Page))
//orm.RunSyncdb("default", false, true)
}
func GetPage() Page {
//res := Page{Website: "弹力笔记", Email: "admin@elasticnotes.com"}
//return res
o := orm.NewOrm()
p := Page{Id: 1}
err := o.Read(&p)
if err != nil {
fmt.Println(err)
}
return p
}
// 修改
func UpdatePage() {
p := Page{Id: 1, Website: "佳能", Email: "修改了email"}
o := orm.NewOrm()
o.Update(&p) // p中不存在的column会被置空
//o.Update(&p, "Email") // 只想修改某个字段
}