improved readme

This commit is contained in:
Lunny Xiao 2013-05-12 14:04:14 +08:00
parent 3d65e0edb7
commit db3ab86e8c
2 changed files with 57 additions and 50 deletions

View File

@ -39,7 +39,7 @@ Drivers for Go's sql package which currently support database/sql includes:
1.Create a database engine just like sql.Open, commonly you just need create once.
```Go
```
import (
_ "github.com/Go-SQL-Driver/MySQL"
"github.com/lunny/xorm"
@ -49,7 +49,7 @@ engine := xorm.Create("mysql", "root:123@/test?charset=utf8")
or
```Go
```
import (
_ "github.com/mattn/go-sqlite3"
"github.com/lunny/xorm"
@ -57,9 +57,15 @@ import (
engine = xorm.Create("sqlite3", "./test.db")
```
1.1.If you want to show all generated SQL
```
engine.ShowSQL = true
```
2.Define a struct
```Go
```
type User struct {
Id int
Name string
@ -67,9 +73,11 @@ type User struct {
}
```
2.1.More mapping rules, please see [Mapping Rules](#-8)
3.When you set up your program, you can use CreateTables to create database tables.
```Go
```
err := engine.CreateTables(&User{})
// or err := engine.Map(&User{}, &Article{})
// err = engine.CreateAll()
@ -77,13 +85,13 @@ err := engine.CreateTables(&User{})
4.then, insert an struct to table
```Go
```
id, err := engine.Insert(&User{Name:"lunny"})
```
or if you want to update records
```Go
```
user := User{Name:"xlw"}
rows, err := engine.Update(&user, &User{Id:1})
// or rows, err := engine.Where("id = ?", 1).Update(&user)
@ -92,7 +100,7 @@ rows, err := engine.Update(&user, &User{Id:1})
5.Fetch a single object by user
```Go
```
var user = User{Id:27}
err := engine.Get(&user)
// or err := engine.Id(27).Get(&user)
@ -103,42 +111,42 @@ err := engine.Get(&user)
6.Fetch multipe objects, use Find
```Go
```
var everyone []Userinfo
err := engine.Find(&everyone)
```
6.1 also you can use Where, Limit
```Go
```
var allusers []Userinfo
err := engine.Where("id > ?", "3").Limit(10,20).Find(&allusers) //Get id>3 limit 10 offset 20
```
6.2 or you can use a struct query
```Go
```
var tenusers []Userinfo
err := engine.Limit(10).Find(&tenusers, &Userinfo{Name:"xlw"}) //Get All Name="xlw" limit 10 offset 0
```
6.3 or In function
```Go
```
var tenusers []Userinfo
err := engine.In("id", 1, 3, 5).Find(&tenusers) //Get All id in (1, 3, 5)
```
7.Delete
```Go
```
err := engine.Delete(&User{Id:1})
// or err := engine.Id(1).Delete(&User{})
```
8.Count
```Go
```
total, err := engine.Count(&User{Name:"xlw"})
```
@ -147,14 +155,14 @@ Of course, SQL execution is also provided.
1.if select then use Query
```Go
```
sql := "select * from userinfo"
results, err := engine.Query(sql)
```
2.if insert, update or delete then use Exec
```Go
```
sql = "update userinfo set username=? where id=?"
res, err := engine.Exec(sql, "xiaolun", 1)
```
@ -162,7 +170,7 @@ res, err := engine.Exec(sql, "xiaolun", 1)
##Deep Use
for deep usage, you should create a session, this func will create a database connection immediatelly
```Go
```
session, err := engine.MakeSession()
defer session.Close()
if err != nil {
@ -172,7 +180,7 @@ if err != nil {
1.Fetch a single object by where
```Go
```
var user Userinfo
session.Where("id=?", 27).Get(&user)
@ -185,7 +193,7 @@ session.Where("name = ? and age < ?", "john", 88).Get(&user4) // even more compl
2.Fetch multiple objects
```Go
```
var allusers []Userinfo
err := session.Where("id > ?", "3").Limit(10,20).Find(&allusers) //Get id>3 limit 10 offset 20
@ -198,7 +206,7 @@ err := session.Find(&everyone)
3.Transaction
```Go
```
// add Begin() before any action
session.Begin()
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
@ -229,7 +237,7 @@ if err != nil {
4.Mixed Transaction
```Go
```
// add Begin() before any action
session.Begin()
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
@ -295,7 +303,7 @@ Another is use field tag, field tag support the below keywords which split with
For Example
```Go
```
type Userinfo struct {
Uid int `xorm:"id pk not null autoincr"`
Username string
@ -312,7 +320,7 @@ Please visit [GoWalker](http://gowalker.org/github.com/lunny/xorm)
Use space.
```Go
```
type User struct {
Name string `json:"name" xorm:"name"`
}

View File

@ -37,17 +37,17 @@ xorm是一个Go语言的ORM库. 通过它可以使数据库操作非常简便。
1.创建数据库引擎这个函数的参数和sql.Open相同但不会立即创建连接 (例如: mysql)
```Go
```
import (
_ "github.com/Go-SQL-Driver/MySQL"
"github.com/lunny/xorm"
)
)
engine := xorm.Create("mysql", "root:123@/test?charset=utf8")
```
or
```Go
```
import (
_ "github.com/mattn/go-sqlite3"
"github.com/lunny/xorm"
@ -57,13 +57,13 @@ engine = xorm.Create("sqlite3", "./test.db")
1.1.默认将不会显示自动生成的SQL语句如果要显示则需要设置
```Go
```
engine.ShowSQL = true
```
2.所有的ORM操作都针对一个或多个结构体一个结构体对应一张表定义一个结构体如下
```Go
```
type User struct {
Id int
Name string
@ -71,23 +71,23 @@ type User struct {
}
```
2.1 详细映射规则,请查看[mapping][mapping]
2.1 详细映射规则,请查看[映射规则](#-8)
3.在程序初始化时,可能会需要创建表
```Go
```
err := engine.CreateTables(&User{})
```
4.然后,可以将一个结构体作为一条记录插入到表中。
```Go
```
id, err := engine.Insert(&User{Name:"lunny"})
```
或者执行更新操作:
```Go
```
user := User{Name:"xlw"}
rows, err := engine.Update(&user, &User{Id:1})
// rows, err := engine.Where("id = ?", 1).Update(&user)
@ -96,53 +96,52 @@ rows, err := engine.Update(&user, &User{Id:1})
5.获取单个对象可以用Get方法
```Go
```
var user = User{Id:27}
err := engine.Get(&user)
// or err := engine.Id(27).Get(&user)
var user = User{Name:"xlw"}
err := engine.Get(&user)
```
6.获取多个对象可以用Find方法
```Go
```
var everyone []Userinfo
err := engine.Find(&everyone)
```
6.1 你也可以使用Where和Limit方法设定条件和查询数量
```Go
```
var allusers []Userinfo
err := engine.Where("id > ?", "3").Limit(10,20).Find(&allusers) //Get id>3 limit 10 offset 20
```
6.2 用一个结构体作为查询条件也是允许的
```Go
```
var tenusers []Userinfo
err := engine.Limit(10).Find(&tenusers, &Userinfo{Name:"xlw"}) //Get All Name="xlw" limit 10 offset 0
```
6.3 也可以调用In函数
```Go
```
var tenusers []Userinfo
err := engine.In("id", 1, 3, 5).Find(&tenusers) //Get All id in (1, 3, 5)
```
7.Delete方法
```Go
```
err := engine.Delete(&User{Id:1})
// or err := engine.Id(1).Delete(&User{})
```
8.Count方法
```Go
```
total, err := engine.Count(&User{Name:"xlw"})
```
@ -151,14 +150,14 @@ total, err := engine.Count(&User{Name:"xlw"})
如果执行Select请用Query()
```Go
```
sql := "select * from userinfo"
results, err := engine.Query(sql)
```
如果执行Insert Update Delete 等操作请用Exec()
```Go
```
sql = "update userinfo set username=? where id=?"
res, err := engine.Exec(sql, "xiaolun", 1)
```
@ -166,7 +165,7 @@ res, err := engine.Exec(sql, "xiaolun", 1)
##高级用法
更高级的用法我们必须要使用session对象session对象在创建时会立刻创建一个数据库连接。
```Go
```
session, err := engine.MakeSession()
defer session.Close()
if err != nil {
@ -176,7 +175,7 @@ if err != nil {
1.session对象同样也可以查询
```Go
```
var user Userinfo
session.Where("id=?", 27).Get(&user)
@ -189,7 +188,7 @@ session.Where("name = ? and age < ?", "john", 88).Get(&user4) // even more compl
2.获取多个对象
```Go
```
var allusers []Userinfo
err := session.Where("id > ?", "3").Limit(10,20).Find(&allusers) //Get id>3 limit 10 offset 20
@ -202,7 +201,7 @@ err := session.Find(&everyone)
3.事务处理
```Go
```
// add Begin() before any action
session.Begin()
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
@ -233,7 +232,7 @@ if err != nil {
4.混合型事务这个事务中既有直接的SQL语句又有ORM方法
```Go
```
// add Begin() before any action
session.Begin()
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
@ -262,7 +261,7 @@ if err != nil {
}
```
##[mapping]映射规则
## 映射规则
1.Struct 和 Struct 的field名字应该为Pascal式命名默认的映射规则将转换成用下划线连接的命名规则这个映射是自动进行的当然你可以通过修改Engine的成员Mapper来改变它。
例如:
@ -299,7 +298,7 @@ UserInfo中的成员UserName将会自动对应名为user_name的字段。
</table>
例如:
```Go
```
type Userinfo struct {
Uid int `xorm:"id pk not null autoincr"`
Username string
@ -317,7 +316,7 @@ type Userinfo struct {
答案:使用空格分开
```Go
```
type User struct {
Name string `json:"name" xorm:"name"`
}