首页 > 代码库 > golang xorm框架的使用

golang xorm框架的使用

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 userraws, err := engine.Raws(...)// SELECT * FROM userbean := 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 = 1engine.Id(core.PK{1, 2}).Get(&user) // for composite primary keys// SELECT * FROM user WHERE id1 = 1 AND id2 = 2engine.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, .. DESCengine.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 userengine.Cols("col1", "col2").Where().Update(user)// UPDATE user set col1 = ?, col2 = ? Where ...engine.Omit("col1").Find()// SELECT col2, col3 FROM userengine.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框架的使用