You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
7.8 KiB

  1. # xorm
  2. [English](https://github.com/go-xorm/xorm/blob/master/README.md)
  3. xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便。
  4. [![CircleCI](https://circleci.com/gh/go-xorm/xorm/tree/master.svg?style=svg)](https://circleci.com/gh/go-xorm/xorm/tree/master) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-xorm/xorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
  5. # 注意
  6. 最新的版本有不兼容的更新,您必须使用 `engine.ShowSQL()``engine.Logger().SetLevel()` 来替代 `engine.ShowSQL = `, `engine.ShowInfo = ` 等等。
  7. ## 特性
  8. * 支持Struct和数据库表之间的灵活映射,并支持自动同步
  9. * 事务支持
  10. * 同时支持原始SQL语句和ORM操作的混合执行
  11. * 使用连写来简化调用
  12. * 支持使用Id, In, Where, Limit, Join, Having, Table, Sql, Cols等函数和结构体等方式作为条件
  13. * 支持级联加载Struct
  14. * 支持缓存
  15. * 支持根据数据库自动生成xorm的结构体
  16. * 支持记录版本(即乐观锁)
  17. * 内置SQL Builder支持
  18. ## 驱动支持
  19. 目前支持的Go数据库驱动和对应的数据库如下:
  20. * Mysql: [github.com/go-sql-driver/mysql](https://github.com/go-sql-driver/mysql)
  21. * MyMysql: [github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/godrv)
  22. * Postgres: [github.com/lib/pq](https://github.com/lib/pq)
  23. * Tidb: [github.com/pingcap/tidb](https://github.com/pingcap/tidb)
  24. * SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
  25. * MsSql: [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
  26. * MsSql: [github.com/lunny/godbc](https://github.com/lunny/godbc)
  27. * Oracle: [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (试验性支持)
  28. ## 更新日志
  29. * **v0.6.0**
  30. * 去除对 ql 的支持
  31. * 新增条件查询分析器 [github.com/go-xorm/builder](https://github.com/go-xorm/builder), 从因此 `Where, And, Or` 函数
  32. 将可以用 `builder.Cond` 作为条件组合
  33. * 新增 Sum, SumInt, SumInt64 和 NotIn 函数
  34. * Bug修正
  35. * **v0.5.0**
  36. * logging接口进行不兼容改变
  37. * Bug修正
  38. * **v0.4.5**
  39. * bug修正
  40. * extends 支持无限级
  41. * Delete Limit 支持
  42. * **v0.4.4**
  43. * Tidb 数据库支持
  44. * QL 试验性支持
  45. * sql.NullString支持
  46. * ForUpdate 支持
  47. * bug修正
  48. [更多更新日志...](https://github.com/go-xorm/manual-zh-CN/tree/master/chapter-16)
  49. ## 安装
  50. 推荐使用 [gopm](https://github.com/gpmgo/gopm) 进行安装:
  51. gopm get github.com/go-xorm/xorm
  52. 或者您也可以使用go工具进行安装:
  53. go get github.com/go-xorm/xorm
  54. ## 文档
  55. * [操作指南](http://xorm.io/docs)
  56. * [GoWalker代码文档](http://gowalker.org/github.com/go-xorm/xorm)
  57. * [Godoc代码文档](http://godoc.org/github.com/go-xorm/xorm)
  58. # 快速开始
  59. * 第一步创建引擎,driverName, dataSourceName和database/sql接口相同
  60. ```Go
  61. engine, err := xorm.NewEngine(driverName, dataSourceName)
  62. ```
  63. * 定义一个和表同步的结构体,并且自动同步结构体到数据库
  64. ```Go
  65. type User struct {
  66. Id int64
  67. Name string
  68. Salt string
  69. Age int
  70. Passwd string `xorm:"varchar(200)"`
  71. Created time.Time `xorm:"created"`
  72. Updated time.Time `xorm:"updated"`
  73. }
  74. err := engine.Sync2(new(User))
  75. ```
  76. * 最原始的也支持SQL语句查询,返回的结果类型为 []map[string][]byte
  77. ```Go
  78. results, err := engine.Query("select * from user")
  79. ```
  80. * 执行一个SQL语句
  81. ```Go
  82. affected, err := engine.Exec("update user set age = ? where name = ?", age, name)
  83. ```
  84. * 插入一条或者多条记录
  85. ```Go
  86. affected, err := engine.Insert(&user)
  87. // INSERT INTO struct () values ()
  88. affected, err := engine.Insert(&user1, &user2)
  89. // INSERT INTO struct1 () values ()
  90. // INSERT INTO struct2 () values ()
  91. affected, err := engine.Insert(&users)
  92. // INSERT INTO struct () values (),(),()
  93. affected, err := engine.Insert(&user1, &users)
  94. // INSERT INTO struct1 () values ()
  95. // INSERT INTO struct2 () values (),(),()
  96. ```
  97. * 查询单条记录
  98. ```Go
  99. has, err := engine.Get(&user)
  100. // SELECT * FROM user LIMIT 1
  101. has, err := engine.Where("name = ?", name).Desc("id").Get(&user)
  102. // SELECT * FROM user WHERE name = ? ORDER BY id DESC LIMIT 1
  103. ```
  104. * 查询多条记录,当然可以使用Join和extends来组合使用
  105. ```Go
  106. var users []User
  107. err := engine.Where("name = ?", name).And("age > 10").Limit(10, 0).Find(&users)
  108. // SELECT * FROM user WHERE name = ? AND age > 10 limit 0 offset 10
  109. type Detail struct {
  110. Id int64
  111. UserId int64 `xorm:"index"`
  112. }
  113. type UserDetail struct {
  114. User `xorm:"extends"`
  115. Detail `xorm:"extends"`
  116. }
  117. var users []UserDetail
  118. err := engine.Table("user").Select("user.*, detail.*")
  119. Join("INNER", "detail", "detail.user_id = user.id").
  120. Where("user.name = ?", name).Limit(10, 0).
  121. Find(&users)
  122. // SELECT user.*, detail.* FROM user INNER JOIN detail WHERE user.name = ? limit 0 offset 10
  123. ```
  124. * 根据条件遍历数据库,可以有两种方式: Iterate and Rows
  125. ```Go
  126. err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
  127. user := bean.(*User)
  128. return nil
  129. })
  130. // SELECT * FROM user
  131. rows, err := engine.Rows(&User{Name:name})
  132. // SELECT * FROM user
  133. defer rows.Close()
  134. bean := new(Struct)
  135. for rows.Next() {
  136. err = rows.Scan(bean)
  137. }
  138. ```
  139. * 更新数据,除非使用Cols,AllCols函数指明,默认只更新非空和非0的字段
  140. ```Go
  141. affected, err := engine.Id(1).Update(&user)
  142. // UPDATE user SET ... Where id = ?
  143. affected, err := engine.Update(&user, &User{Name:name})
  144. // UPDATE user SET ... Where name = ?
  145. var ids = []int64{1, 2, 3}
  146. affected, err := engine.In(ids).Update(&user)
  147. // UPDATE user SET ... Where id IN (?, ?, ?)
  148. // force update indicated columns by Cols
  149. affected, err := engine.Id(1).Cols("age").Update(&User{Name:name, Age: 12})
  150. // UPDATE user SET age = ?, updated=? Where id = ?
  151. // force NOT update indicated columns by Omit
  152. affected, err := engine.Id(1).Omit("name").Update(&User{Name:name, Age: 12})
  153. // UPDATE user SET age = ?, updated=? Where id = ?
  154. affected, err := engine.Id(1).AllCols().Update(&user)
  155. // UPDATE user SET name=?,age=?,salt=?,passwd=?,updated=? Where id = ?
  156. ```
  157. * 删除记录,需要注意,删除必须至少有一个条件,否则会报错。要清空数据库可以用EmptyTable
  158. ```Go
  159. affected, err := engine.Where(...).Delete(&user)
  160. // DELETE FROM user Where ...
  161. ```
  162. * 获取记录条数
  163. ```Go
  164. counts, err := engine.Count(&user)
  165. // SELECT count(*) AS total FROM user
  166. ```
  167. * 条件编辑器
  168. ```Go
  169. err := engine.Where(builder.NotIn("a", 1, 2).And(builder.In("b", "c", "d", "e"))).Find(&users)
  170. // SELECT id, name ... FROM user WHERE a NOT IN (?, ?) AND b IN (?, ?, ?)
  171. ```
  172. # 案例
  173. * [github.com/m3ng9i/qreader](https://github.com/m3ng9i/qreader)
  174. * [Wego](http://github.com/go-tango/wego)
  175. * [Docker.cn](https://docker.cn/)
  176. * [Gogs](http://try.gogits.org) - [github.com/gogits/gogs](http://github.com/gogits/gogs)
  177. * [Gowalker](http://gowalker.org) - [github.com/Unknwon/gowalker](http://github.com/Unknwon/gowalker)
  178. * [Gobuild.io](http://gobuild.io) - [github.com/shxsun/gobuild](http://github.com/shxsun/gobuild)
  179. * [Sudo China](http://sudochina.com) - [github.com/insionng/toropress](http://github.com/insionng/toropress)
  180. * [Godaily](http://godaily.org) - [github.com/govc/godaily](http://github.com/govc/godaily)
  181. * [YouGam](http://www.yougam.com/)
  182. * [GoCMS - github.com/zzboy/GoCMS](https://github.com/zzdboy/GoCMS)
  183. * [GoBBS - gobbs.domolo.com](http://gobbs.domolo.com/)
  184. * [go-blog](http://wangcheng.me) - [github.com/easykoo/go-blog](https://github.com/easykoo/go-blog)
  185. ## 讨论
  186. 请加入QQ群:280360085 进行讨论。
  187. ## 贡献
  188. 如果您也想为Xorm贡献您的力量,请查看 [CONTRIBUTING](https://github.com/go-xorm/xorm/blob/master/CONTRIBUTING.md)
  189. ## LICENSE
  190. BSD License
  191. [http://creativecommons.org/licenses/BSD/](http://creativecommons.org/licenses/BSD/)