func insertUser() { o := orm.NewOrm() //拿到句柄,操作数据库 //准备要插入的数据 user := models.User{} user.Name = "sdh01" beego.Info(user) id, err := o.Insert(&user)//这里需要传递指针变量 if err != nil { fmt.Println("insert err :", err) return } beego.Info("insert sucess id = ", id, user) }
Beego框架orm在执行o.Insert(*p)插入数据时候需要传入指针变量作为参数,原因是因为在插入成功后,会返回id给user。
如下是打印结果: