码迷,mamicode.com
首页 > 其他好文 > 详细

golang xorm框架的使用

时间:2014-12-12 16:16:14      阅读:1562      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   ar   color   os   使用   sp   for   

1、创建engine

engine, err := xorm.NewEngine(driverName, dataSourceName)

上述代码创建了一个数据库引擎,可以在一个程序中创建多个engine。

2、查询方法:

2.1、支持sql查询:

  1、查询一个string类型的sql,返回[]map[string][]byte类型的切片(查询)

results, err := engine.Query("select * from user")

  2、执行一个string的sql,返回结果影响行数(增删改)

affected, err := engine.Exec("update user set .... where ...")

2.2、ORM方法:

  主要有七个方法和其他辅助的方法来操作数据库。

  1、插入一个或者多个数据:

affected, err := engine.Insert(&struct)
// INSERT INTO struct () values ()
affected, err := engine.Insert(&struct1, &struct2)
// INSERT INTO struct1 () values ()
// INSERT INTO struct2 () values ()
affected, err := engine.Insert(&sliceOfStruct)
// INSERT INTO struct () values (),(),()
affected, err := engine.Insert(&struct1, &sliceOfStruct2)
// INSERT INTO struct1 () values ()
// INSERT INTO struct2 () values (),(),()

  2、从数据库里面查询一条记录:

has, err := engine.Get(&user)
// SELECT * FROM user LIMIT 1

  3、从数据库中查询多条记录:

sliceOfStructs := new(Struct)
err := engine.Find(sliceOfStructs)
// SELECT * FROM user

  4、查询多条记录,然后每条记录进行处理,有两个方法,一个是iterator,另一个是raw:

err := engine.Iterate(...)
// SELECT * FROM user

raws, err := engine.Raws(...)
// SELECT * FROM user
bean := new(Struct)
for raws.Next() {
    err = raws.Scan(bean)
}

  5、更新一条或者多条记录:

affected, err := engine.Update(&user)
// UPDATE user SET ...

  6:删除一条或者多条记录,必须存在删除条件

affected, err := engine.Where(...).Delete(&user)
// DELETE FROM user Where ...

  7、查询记录条数:

counts, err := engine.Count(&user)
// SELECT count(*) AS total FROM user

2.3:条件:

  1、Id、In:

engine.Id(1).Get(&user) // for single primary key
// SELECT * FROM user WHERE id = 1
engine.Id(core.PK{1, 2}).Get(&user) // for composite primary keys
// SELECT * FROM user WHERE id1 = 1 AND id2 = 2
engine.In("id", 1, 2, 3).Find(&users)
// SELECT * FROM user WHERE id IN (1, 2, 3)
engine.In("id", []int{1, 2, 3})
// SELECT * FROM user WHERE id IN (1, 2, 3)

  2、 Where, And, Or

engine.Where().And().Or().Find()
// SELECT * FROM user WHERE (.. AND ..) OR ...

  3、OrderBy, Asc, Desc

engine.Asc().Desc().Find()
// SELECT * FROM user ORDER BY .. ASC, .. DESC
engine.OrderBy().Find()
// SELECT * FROM user ORDER BY ..

  4、 Limit, Top

engine.Limit().Find()
// SELECT * FROM user LIMIT .. OFFSET ..
engine.Top(5).Find()
// SELECT TOP 5 * FROM user // for mssql
// SELECT * FROM user LIMIT .. OFFSET 0 //for other databases

  5、Sql, 查询原生SQL

engine.Sql("select * from user").Find()

  6、Cols, Omit, Distinct

engine.Cols("col1, col2").Find()
// SELECT col1, col2 FROM user
engine.Cols("col1", "col2").Where().Update(user)
// UPDATE user set col1 = ?, col2 = ? Where ...
engine.Omit("col1").Find()
// SELECT col2, col3 FROM user
engine.Omit("col1").Insert()
// INSERT INTO table (non-col1) VALUES ()
engine.Distinct("col1").Find()
// SELECT DISTINCT col1 FROM user

  7、Join, GroupBy, Having

engine.GroupBy("name").Having("name=‘xlw‘").Find()
//SELECT * FROM user GROUP BY name HAVING name=‘xlw‘
engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find()
//SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id

 

golang xorm框架的使用

标签:des   style   blog   ar   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/guhao123/p/4159688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!