xorm -Exist方法实例
程序员文章站
2022-07-08 20:47:25
判断某个记录是否存在可以使用 , 相比 ,`Exist`性能更好。 go package main import ( "fmt" _ "github.com/go sql driver/mysql" "github.com/go xorm/xorm" "log" "time" ) var engin ......
-
判断某个记录是否存在可以使用
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更高。
-