设为首页 加入收藏

TOP

xorm -Exist方法实例(一)
2019-05-26 18:07:51 】 浏览:120
Tags:xorm -Exist 方法 实例

  • 判断某个记录是否存在可以使用Exist, 相比GetExist性能更好。

    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的区别

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇golang开发:(二)安装 Golang 下一篇defer, panic, recover使用总结