4

5.session的使用

 1 year ago
source link: https://www.kancloud.cn/fizz/gorose-2/1135842
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

5.session的使用

session作原生查询

直接返回结果集

// 原生sql
res,err := DB().Query("select * from users where uid>? limit 2", 1)
fmt.Println(res)
affected_rows,err := DB().Execute("delete from users where uid=?", 1)
fmt.Println(affected_rows, err)

也可以绑定查询结果到给定对象

session := engin.NewSession()

var u = make(map[string]interface{})
session := engin.NewSession()
// 这里Bind()是为了存放结果的, 如果你使用的是NewOrm()初始化,则可以直接使用 NewOrm().Table().Query()
_,err := session.Bind(&u).Query("select * from users where uid=? limit 2", 1)
session := engin.NewSession()

session.Execute("insert into users(name,age) values(?,?)(?,?)", "gorose",18,"fizzday",19)
session.Execute("update users set name=? where uid=?","gorose",1)
session.Execute("delete from users where uid=?", 1)

// 事务, 用法跟orm的事务类似
session.Transaction()

session.LastSql()

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK