xorm -Exist方法实例
-
判断某个记录是否存在可以使用
Exist
, 相比Get
,Exist
性能更好。package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/xorm" "log" "time" ) var engine *xorm.Engine type User struct { Name string `xorm:"varchar(25) 'name'"` Id int `xorm:"pk 'id' autoincr"` CreatedAt time.Time `xorm:"created"` } func main() { var err error engine, err = xorm.NewEngine("mysql", "root:123456@/test") if err != nil { log.Fatal(err) return } err = engine.CreateTables(User{}) if err != nil { log.Fatal(err) return } u := make([]User, 5) u[0].Name = "abcd" u[1].Name = "acbd" u[2].Name = "dbac" u[3].Name = "cbda" u[4].Name = "bdca" _, err = engine.Insert(u) if err != nil { log.Fatal(err) return } /* has, err := testEngine.Exist(new(RecordExist)) // SELECT * FROM record_exist LIMIT 1、 */ uu := new(User) uu.Name = "abcd" has, err := engine.Exist(uu) if err != nil { log.Fatal(err) return } if has { fmt.Println("uu ",uu.Name, uu.Id, uu.CreatedAt) } /* has, err = testEngine.Exist(&RecordExist{ Name: "test1", }) // SELECT * FROM record_exist WHERE name = ? LIMIT 1 */ uu0 := &User{Name: "abcd"} has, err = engine.Exist(uu0) if err != nil { log.Fatal(err) return } if has { fmt.Println("uu0 ",uu0.Name, uu0.Id, uu0.CreatedAt) } /* has, err = testEngine.Where("name = ?", "test1").Exist(&RecordExist{}) // SELECT * FROM record_exist WHERE name = ? LIMIT 1 */ uu1 := &User{Name: "abcd"} has, err = engine.Where("id =?", 1).Exist(uu1) if err != nil { log.Fatal(err) return } if has { fmt.Println("uu1 ",uu1.Name, uu1.Id, uu1.CreatedAt) } /* has, err = testEngine.SQL("select * from record_exist where name = ?", "test1").Exist() // select * from record_exist where name = ? */ uu2 := new(User) has, err = engine.SQL("select * from user where id =?", 2).Exist(uu2) if err != nil { log.Fatal(err) return } if has { fmt.Println("uu2 ",uu2.Name, uu2.Id, uu2.CreatedAt) } /* has, err = testEngine.Table("record_exist").Exist() // SELECT * FROM record_exist LIMIT 1 */ uu3 := new(User) has, err = engine.Table(User{}).Exist(uu3) if err != nil { log.Fatal(err) return } if has { fmt.Println("uu3 ",uu3.Name, uu3.Id, uu3.CreatedAt) } /* has, err = testEngine.Table("record_exist").Where("name = ?", "test1").Exist() // SELECT * FROM record_exist WHERE name = ? LIMIT 1 */ uu4 := new(User) has, err = engine.Table(User{}).Where("name =?", "abcd").Exist(uu4) if err != nil { log.Fatal(err) return } if has { fmt.Println("uu4 ",uu4.Name, uu4.Id, uu4.CreatedAt) } } /* 输出: uu abcd 0 0001-01-01 00:00:00 +0000 UTC uu0 abcd 0 0001-01-01 00:00:00 +0000 UTC uu1 abcd 0 0001-01-01 00:00:00 +0000 UTC uu2 0 0001-01-01 00:00:00 +0000 UTC uu3 0 0001-01-01 00:00:00 +0000 UTC uu4 0 0001-01-01 00:00:00 +0000 UTC 可以发现体存在的情况下,Exist方法不会将查到的实体赋值给参数 */
-
与Get的区别
-
Get与Exist方法返回值都为bool和error,如果查询到实体存在,则Get方法会将查到的实体赋值给参数
user := &User{Id:1} has,err := testEngine.Get(user) // 执行结束后,user会被赋值为数据库中Id为1的实体 has,err = testEngine.Exist(user) // user中仍然是初始声明的user,不做改变
-
如果你的需求是:判断某条记录是否存在,若存在,则返回这条记录。
-
建议直接使用Get方法。
-
如果仅仅判断某条记录是否存在,则使用Exist方法,Exist的执行效率要比Get更高。
-