mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 09:10:55 +00:00
update docs
This commit is contained in:
parent
37f8c6a04a
commit
ad2965bbf9
114
orm/README.md
114
orm/README.md
@ -1,12 +1,122 @@
|
||||
## beego orm
|
||||
# beego orm
|
||||
|
||||
a powerful orm framework
|
||||
|
||||
now, beta, unstable, may be changing some api make your app build failed.
|
||||
|
||||
**Driver Support:**
|
||||
|
||||
* MySQL: [github.com/go-sql-driver/mysql](https://github.com/go-sql-driver/mysql)
|
||||
|
||||
**Features:**
|
||||
|
||||
...
|
||||
|
||||
**Install:**
|
||||
|
||||
go get github.com/astaxie/beego/orm
|
||||
|
||||
## Quick Start
|
||||
|
||||
#### Simple Usage
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/orm"
|
||||
_ "github.com/go-sql-driver/mysql" // import your used driver
|
||||
)
|
||||
|
||||
// Model Struct
|
||||
type User struct {
|
||||
Id int `orm:"auto"`
|
||||
Name string `orm:"size(100)"`
|
||||
orm.Manager
|
||||
}
|
||||
|
||||
func init() {
|
||||
// register model
|
||||
orm.RegisterModel(new(User))
|
||||
|
||||
// set default database
|
||||
orm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8", 30)
|
||||
}
|
||||
|
||||
func main() {
|
||||
o := orm.NewOrm()
|
||||
|
||||
user := User{Name: "slene"}
|
||||
|
||||
// insert
|
||||
id, err := o.Insert(&user)
|
||||
|
||||
// update
|
||||
user.Name = "astaxie"
|
||||
num, err := o.Update(&user)
|
||||
|
||||
// read one
|
||||
u := User{Id: user.Id}
|
||||
err = o.Read(&u)
|
||||
|
||||
// delete
|
||||
num, err = o.Delete(&u)
|
||||
}
|
||||
```
|
||||
|
||||
#### Next with relation
|
||||
|
||||
```go
|
||||
type Post struct {
|
||||
Id int `orm:"auto"`
|
||||
Title string `orm:"size(100)"`
|
||||
User *User `orm:"rel(fk)"`
|
||||
orm.Manager
|
||||
}
|
||||
|
||||
var posts []*Post
|
||||
qs := o.QueryTable("post")
|
||||
num, err := qs.Filter("User__Name", "slene").All(&posts)
|
||||
```
|
||||
|
||||
#### Use Raw sql
|
||||
|
||||
```go
|
||||
var maps []Params
|
||||
num, err := o.Raw("SELECT id FROM user WHERE name = ?", "slene").Values(&maps)
|
||||
if num > 0 {
|
||||
fmt.Println(maps[0]["id"])
|
||||
}
|
||||
```
|
||||
|
||||
#### Transaction
|
||||
|
||||
```go
|
||||
o.Begin()
|
||||
...
|
||||
user := User{Name: "slene"}
|
||||
id, err := o.Insert(&user)
|
||||
if err != nil {
|
||||
o.Commit()
|
||||
} else {
|
||||
o.Rollback()
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Docs
|
||||
|
||||
more details and examples in docs and test
|
||||
|
||||
* [中文](docs/zh)
|
||||
* English
|
||||
|
||||
## TODO
|
||||
- some unrealized api
|
||||
- examples
|
||||
- docs
|
||||
- support sqlite
|
||||
- support postgres
|
||||
- support sqlite
|
||||
|
||||
##
|
||||
|
@ -1,62 +1,9 @@
|
||||
## beego orm 介绍
|
||||
|
||||
## 快速入门
|
||||
|
||||
```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() {
|
||||
o := orm.NewOrm()
|
||||
|
||||
user := User{Name: "slene"}
|
||||
|
||||
// 创建
|
||||
id, err := o.Insert(&user)
|
||||
fmt.Println(id, err)
|
||||
|
||||
// 更新
|
||||
user.Name = "astaxie"
|
||||
num, err := o.Update(&user)
|
||||
fmt.Println(num, err)
|
||||
|
||||
// 查询单个
|
||||
u := User{Id: user.Id}
|
||||
err = o.Read(&u)
|
||||
fmt.Println(u.Name, err)
|
||||
|
||||
// 删除
|
||||
num, err = o.Delete(&u)
|
||||
fmt.Println(num, err)
|
||||
}
|
||||
```
|
||||
|
||||
## 详细文档
|
||||
## 文档目录
|
||||
|
||||
1. [模型定义](Models.md)
|
||||
- [支持的 Field 类型](Models.md#Field Type)
|
||||
- [Field 设置参数](Models.md#Field Options)
|
||||
- [关系型 Field 设置](Models.md#Relation Field Options)
|
||||
- [支持的 Field 类型](Models.md#field-type)
|
||||
- [Field 设置参数](Models.md#field-options)
|
||||
- [关系型 Field 设置](Models.md#relation-field-options)
|
||||
2. Custom Fields
|
||||
3. [Orm 使用方法](Orm.md)
|
||||
- [Ormer 接口](Orm.md#Ormer)
|
||||
|
Loading…
Reference in New Issue
Block a user