Created
June 21, 2017 04:46
-
-
Save leopard627/a47f48c6d79579fbbd0bf7eb2f4d197b to your computer and use it in GitHub Desktop.
go orm expl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"beegoorm/models" | |
"fmt" | |
"github.com/astaxie/beego" | |
"github.com/astaxie/beego/orm" | |
_ "beegoorm/routers" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
func init() { | |
// 注册驱动 | |
orm.RegisterDriver("mysql", orm.DR_MySQL) | |
// 注册默认数据库 | |
// 我的mysql的root用户密码为tom,打算把数据表建立在名为test数据库里 | |
// 备注:此处第一个参数必须设置为“default”(因为我现在只有一个数据库),否则编译报错说:必须有一个注册DB的别名为 default | |
orm.RegisterDataBase("default", "mysql", "root:tom@/test?charset=utf8") | |
} | |
func main() { | |
// 开启 orm 调试模式:开发过程中建议打开,release时需要关闭 | |
orm.Debug = true | |
// 自动建表 | |
orm.RunSyncdb("default", false, true) | |
// 创建一个 ormer 对象 | |
o := orm.NewOrm() | |
o.Using("default") | |
perfile := new(models.Profile) | |
perfile.Age = 30 | |
user := new(models.User) | |
user.Name = "tom" | |
user.Profile = perfile | |
// insert | |
o.Insert(perfile) | |
o.Insert(user) | |
o.Insert(perfile) | |
o.Insert(user) | |
o.Insert(perfile) | |
o.Insert(user) | |
// update | |
user.Name = "hezhixiong" | |
num, err := o.Update(user) | |
fmt.Printf("NUM: %d, ERR: %v\n", num, err) | |
// delete | |
o.Delete(&models.User{Id: 2}) | |
beego.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment