improved QuickStart.md
This commit is contained in:
parent
45b3f9775e
commit
df3bda568b
|
@ -3,7 +3,7 @@ xorm 快速入门
|
|||
|
||||
* [1.创建Orm引擎](#10)
|
||||
* [2.定义表结构体](#20)
|
||||
* [2.1.名称映射规则](#21)
|
||||
* [2.1.名称映射规则](#21)
|
||||
* [2.2.前缀映射规则和后缀映射规则](#22)
|
||||
* [2.3.使用Table和Tag改变名称映射](#23)
|
||||
* [2.4.Column属性定义](#24)
|
||||
|
@ -20,7 +20,7 @@ xorm 快速入门
|
|||
* [5.3.Get方法](#63)
|
||||
* [5.4.Find方法](#64)
|
||||
* [5.5.Iterate方法](#65)
|
||||
* [5.6.Count方法](#66)
|
||||
* [5.6.Count方法](#66)
|
||||
* [5.7.Rows方法](#67)
|
||||
* [6.更新数据](#70)
|
||||
* [6.1.乐观锁](#71)
|
||||
|
@ -45,7 +45,7 @@ xorm 快速入门
|
|||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/lunny/xorm"
|
||||
)
|
||||
)
|
||||
engine, err := xorm.NewEngine("mysql", "root:123@/test?charset=utf8")
|
||||
defer engine.Close()
|
||||
```
|
||||
|
@ -58,20 +58,20 @@ import (
|
|||
"github.com/lunny/xorm"
|
||||
)
|
||||
engine, err = xorm.NewEngine("sqlite3", "./test.db")
|
||||
defer engine.Close()
|
||||
defer engine.Close()
|
||||
```
|
||||
|
||||
一般如果只针对一个数据库进行操作,只需要创建一个Engine即可。Engine支持在多GoRutine下使用。
|
||||
|
||||
xorm当前支持五种驱动四个数据库如下:
|
||||
|
||||
* Mysql: [github.com/Go-SQL-Driver/MySQL](https://github.com/Go-SQL-Driver/MySQL)
|
||||
|
||||
* Mysql: [github.com/Go-SQL-Driver/MySQL](https://github.com/Go-SQL-Driver/MySQL)
|
||||
|
||||
* MyMysql: [github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/godrv)
|
||||
|
||||
* SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
|
||||
|
||||
* Postgres: [github.com/lib/pq](https://github.com/lib/pq)
|
||||
* Postgres: [github.com/lib/pq](https://github.com/lib/pq)
|
||||
|
||||
* MsSql: [github.com/lunny/godbc](https://githubcom/lunny/godbc)
|
||||
|
||||
|
@ -120,9 +120,9 @@ engine.SetMapper(SameMapper{})
|
|||
|
||||
同时需要注意的是:
|
||||
|
||||
* 如果你使用了别的命名规则映射方案,也可以自己实现一个IMapper。
|
||||
* 如果你使用了别的命名规则映射方案,也可以自己实现一个IMapper。
|
||||
* 表名称和字段名称的映射规则默认是相同的,当然也可以设置为不同,如:
|
||||
|
||||
|
||||
```Go
|
||||
engine.SetTableMapper(SameMapper{})
|
||||
engine.SetColumnMapper(SnakeMapper{})
|
||||
|
@ -356,7 +356,7 @@ affected, err := engine.Insert(user, &questions)
|
|||
```
|
||||
|
||||
这里需要注意以下几点:
|
||||
* 这里虽然支持同时插入,但这些插入并没有事务关系。因此有可能在中间插入出错后,后面的插入将不会继续。
|
||||
* 这里虽然支持同时插入,但这些插入并没有事务关系。因此有可能在中间插入出错后,后面的插入将不会继续。
|
||||
* 多条插入会自动生成`Insert into table values (),(),()`的语句,因此这样的语句有一个最大的记录数,根据经验测算在150条左右。大于150条后,生成的sql语句将太长可能导致执行失败。因此在插入大量数据时,目前需要自行分割成每150条插入一次。
|
||||
|
||||
<a name="60" id="60"></a>
|
||||
|
@ -504,7 +504,7 @@ has, err := engine.Get(user)
|
|||
1) 传入Slice用于返回数据
|
||||
|
||||
```Go
|
||||
everyone := make([]Userinfo, 0)
|
||||
everyone := make([]Userinfo, 0)
|
||||
err := engine.Find(&everyone)
|
||||
|
||||
pEveryOne := make([]*Userinfo, 0)
|
||||
|
@ -643,7 +643,7 @@ results, err := engine.Query(sql)
|
|||
|
||||
```Go
|
||||
sql = "update `userinfo` set username=? where id=?"
|
||||
res, err := engine.Exec(sql, "xiaolun", 1)
|
||||
res, err := engine.Exec(sql, "xiaolun", 1)
|
||||
```
|
||||
|
||||
<a name="110" id="110"></a>
|
||||
|
@ -678,9 +678,11 @@ if err != nil {
|
|||
err = session.Commit()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
* 注意如果您使用的是mysql,数据库引擎为innodb事务才有效,myisam引擎是不支持事务的。
|
||||
|
||||
<a name="120" id="120"></a>
|
||||
## 11.缓存
|
||||
|
||||
|
@ -715,7 +717,7 @@ engine.MapCacher(&user, nil)
|
|||
1. 当使用了`Distinct`,`Having`,`GroupBy`方法将不会使用缓存
|
||||
|
||||
2. 在`Get`或者`Find`时使用了`Cols`,`Omit`方法,则在开启缓存后此方法无效,系统仍旧会取出这个表中的所有字段。
|
||||
|
||||
|
||||
3. 在使用Exec方法执行了方法之后,可能会导致缓存与数据库不一致的地方。因此如果启用缓存,尽量避免使用Exec。如果必须使用,则需要在使用了Exec之后调用ClearCache手动做缓存清除的工作。比如:
|
||||
|
||||
```Go
|
||||
|
@ -752,14 +754,14 @@ xorm工具提供了xorm命令,能够帮助做很多事情。
|
|||
|
||||
<a name="160" id="160"></a>
|
||||
## 15.那些年我们踩过的坑
|
||||
* 怎么同时使用xorm的tag和json的tag?
|
||||
|
||||
答:使用空格
|
||||
|
||||
```Go
|
||||
type User struct {
|
||||
Name string `json:"name" xorm:"name"`
|
||||
}
|
||||
* 怎么同时使用xorm的tag和json的tag?
|
||||
|
||||
答:使用空格
|
||||
|
||||
```Go
|
||||
type User struct {
|
||||
Name string `json:"name" xorm:"name"`
|
||||
}
|
||||
```
|
||||
|
||||
* 我的struct里面包含bool类型,为什么它不能作为条件也没法用Update更新?
|
||||
|
|
Loading…
Reference in New Issue