2013-07-31 14:11:35 +00:00
|
|
|
## beego orm 介绍
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
## 快速入门
|
2013-07-31 14:11:35 +00:00
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 最简单的两个表的定义
|
|
|
|
type User struct {
|
|
|
|
Id int `orm:"auto"`
|
|
|
|
Name string `orm:"size(100)"`
|
|
|
|
orm.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// 将表定义注册到 orm 里
|
|
|
|
orm.RegisterModel(new(User))
|
|
|
|
|
|
|
|
// 链接参数设置
|
|
|
|
orm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8", 30)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
orm.BootStrap() // 确保在所有 RegisterModel 之后执行
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2013-08-01 07:52:05 +00:00
|
|
|
user := User{Name: "slene"}
|
2013-08-01 01:23:44 +00:00
|
|
|
|
|
|
|
// 创建
|
2013-08-01 07:52:05 +00:00
|
|
|
id, err := o.Insert(&user)
|
2013-08-01 01:23:44 +00:00
|
|
|
fmt.Println(id, err)
|
|
|
|
|
|
|
|
// 更新
|
|
|
|
user.Name = "astaxie"
|
2013-08-01 07:52:05 +00:00
|
|
|
num, err := o.Update(&user)
|
2013-08-01 01:23:44 +00:00
|
|
|
fmt.Println(num, err)
|
|
|
|
|
|
|
|
// 查询单个
|
2013-08-01 07:52:05 +00:00
|
|
|
u := User{Id: user.Id}
|
|
|
|
err = o.Read(&u)
|
|
|
|
fmt.Println(u.Name, err)
|
2013-08-01 01:23:44 +00:00
|
|
|
|
|
|
|
// 删除
|
2013-08-01 07:52:05 +00:00
|
|
|
num, err = o.Delete(&u)
|
2013-08-01 01:23:44 +00:00
|
|
|
fmt.Println(num, err)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## 详细文档
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
|
|
1. [Model Definition](Models.md)
|
|
|
|
2. Custom Fields
|
|
|
|
3. [Orm](Orm.md)
|
|
|
|
4. [Object](Object.md)
|
|
|
|
5. [Query](Query.md)
|
|
|
|
6. Condition
|
|
|
|
7. Raw
|
|
|
|
8. Transaction
|
|
|
|
9. Faq
|