xorm是一个Go语言ORM库,其文档:http://xorm.topgoer.com/
var user User
engine.Id(1).Get(&user)
select * from user where id = 1
engine.Where('user_name = ? and pwd = ?', 'davie', '123').Get(&admin)
// 或
engine.Where('user_name = ?','davie').Where('pwd = ?','123').Get(&admin)
select * from user where user_name = 'davie' and pwd = '123'
result := User
err := engine.Where("student_name = ?", OrgName).Get(&result)
results := make([]User, 0)
err := engine.Where("student_name = ?", OrgName).Find(&results)
engine.Count(&user)
var user
engine.Insert(&user)
// user为切片
user := []User{user1,user2}
engine.Insert(&user)
user:= students.table1{
age: 12,
}
engine.Where("name = ?","new").Update(&user)
engine.Where("name = ?","new").Delete(&user)
在有需要的批量操作数据时,需用到事务。在xorm中,事务操作和Session联系在一起。一共三个步骤,分别为:创建session对象,Begin事务开始执行,Commit提交事务。中间为具体的数据库操作。
// 创建事务
session := engine.NewSession()
defer session.Close()
// 事务开始
err := session.Begin()
if err != nil {
return err
}
// 事务相关操作
_, err := dao.Students.DeleteStudents(session, Name)
if err != nil {
// 事务回滚
session.Rollback()
return err
}
事务提交
err = session.Commit()
if err != nil{
return err
}
func (*dStudents) DeleteStudents(session *xorm.Session, Name string) (int64, error) {
result := students.table1{
DeletedAt: time.Now().UnixMilli(),
}
return session.Table("table1").Where("student_name = ?", OrgName).Where("deleted_at = ?", 0).Update(&result)
}
Go语言学习教程:xorm表基本操作及高级操作:
http://www.taodudu.cc/news/show-732070.html?action=onClick
Go-MySQL(四)XORM:
https://blog.csdn.net/weixin_41922289/article/details/116282272