介绍
今天就跟大家聊聊有关xorm怎么正确的在golang中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
简单的用法
package main import ( ,_ “;github.com/go-sql-driver/mysql" ,“;github.com/go-xorm/xorm" ,“log" ) ,//定义结构体(xorm支持双向映射) type User  struct { ,User_id int64 “xorm:“pk autoincr"”,//指定主键并自增 ,Name string  ' xorm:“unique" ',//唯一的 Balance float64 ,Time int64  ' xorm:“updated" ',//修改后自动更新时间 ,Creat_time int64  ' xorm:“created" ',//创建时间 ,//Version int  ' xorm:“version" ',//乐观锁 } ,//定义orm引擎 var x * xorm.Engine //创建orm引擎 func init (), { ,var err 错误 ,x, err =, xorm.NewEngine (“mysql",,“根:root@tcp (127.0.0.1:3306)/xorm ? charset=utf8") ,if err  !=, nil { ,log.Fatal(“数据库连接失败:“,,呃) ,} ,if err :=, x.Sync(新(用户));,err !=, nil { ,log.Fatal(“数据表同步失败:“,,呃) ,} } ,//增 字符串,func 插入(name balance float64), (int64, bool), { ,user :=,新(用户) user.Name =,名称 user.Balance =,平衡 ,影响,err :=, x.Insert(用户) ,if err  !=, nil { ,return 影响,假的 ,} ,return 影响,真的 } ,//删 func 德尔(id int64), { ,user :=,新(用户) ,x.Id (id) delete(用户) } ,//改 func 更新(id int64, user *用户),bool { ,影响,err :=, x.ID (id) .Update(用户) ,if err  !=, nil { ,log.Fatal(“错误:“,,呃) ,} ,if affected ==, 0, { return 假 ,} return 真正的 } ,//查 func getinfo (id int64), * User { ,user :=,,用户{User_id: id} ,是,_ :=, x.Get(用户) ,if ! is , { ,log.Fatal(“搜索结果不存在!“) ,} return 用户 }
在杜松子酒中的用法
package main import ( ,“;github.com/gin-gonic/gin" “。/models" ,“strconv" ) ,//添加操作 func 插入(c * gin.Context), { ,name :=, c.Query (“name") ,if name ==,,,, { ,c.JSON (200, gin.H {“msg":,“名字不得为空!“}) ,返回 ,} , ,money :=, c.Query (“money") ,if money ==,,,, { ,c.JSON (200, gin.H {“msg":“钱不得为空!“}) ,返回 ,} , ,平衡,_ :=, strconv.ParseFloat(钱,,64) ,//添加 ,user :=, models.User {} user.Name =,名称 user.Balance =,平衡 ,rel, err :=, models.X.Insert(用户), ,if rel ==, 0, | |, err !=, nil { ,c.JSON (200, gin.H {“msg":“添加错误,,,“err":,呃,,“rel":, rel}) ,}else { ,c.JSON (200, gin.H {“msg":“添加成功“}) ,} } ,//查询单个操作 func get (c * gin.Context), { ,id :=, c.Query (“id") ,if id ==,,,, { ,c.JSON (200, gin.H {“msg":“id不得为空!“}) ,返回 ,} ,//字符串转int64 ,id, _ :=, strconv.ParseInt (id, 10日,64) ,//查询1 ,//user :=,,用户{User_id: id} ,//rel err :=, models.X.Get(用户) ,//查询2 ,user :=,, models.User {} ,rel, err :=, models.X.Where (“user_id =, ?“,, ids) . get(用户) , ,if ! rel | |, err !=, nil { ,c.JSON (200, gin.H {“msg":“查询错误“}) ,}else { ,c.JSON (200, gin.H {“user":用户}) ,} } ,//查询多条操作 func 找到(c * gin.Context), { ,users :=, (map [int64] models.User) ,err :=, models.X.Find(和用户) ,if err  !=, nil { ,c.JSON (200, gin.H {“msg":犯错}) ,} ,c.JSON (200, gin.H {“msg":用户}) } ,//修改操作 func 更新(c * gin.Context), { ,id :=, c.Query (“id") ,if id ==,,,, { ,c.JSON (200, gin.H {“msg":“id1不得为空!“,,“id":, id}) ,返回 ,} ,id, _ :=, strconv.ParseInt (id, 10日,64), ,name :=, c.Query (“name") null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullxorm怎么正确的在golang中使用