xorm 的基本操作

 
  
package main
 
  
import (
    "fmt"
 
  
    _ "github.com/go-sql-driver/mysql"
    "github.com/go-xorm/xorm"
)
 
  
type Person struct {
    Id   int64
    Name string
}
 
  
func main() {
    x, engErr := xorm.NewEngine("mysql", "root:12345678@/test?charset=utf8")
    fmt.Printf("eng error : %v\n", engErr)

 
  
    fmt.Println("++++++++++ Build entities to tables")
    x.Sync(new(Person
))
 
  
    fmt.Println("++++++++ Insert Person")
    p := Person{Name: "lucy"}
 
  
    insertNum, insertErr := x.Insert(&p)
    fmt.Printf("insert num : %d , insert person id : %d , insert err : %v\n", insertNum, p.Id, insertErr)
 
  
    fmt.Println("++++++++ Update Person")
    p.Name = "maven"
    updateNum, updateErr := x.Update(p)
    fmt.Printf("lucy change name to maven :: num : %d , err : %v\n", updateNum, updateErr)
 
  
    fmt.Println("++++++++ Delete Person")
    x.Delete(&p)
    var dbPerson Person
    lastGetHas, lastGetErr := x.Id(p.Id).Get(dbPerson)
    fmt.Printf("last get has : %d , last get err : %v\n", lastGetHas, lastGetErr)
    if lastGetHas == false {
        fmt.Println("person deleted")
    } else {
        fmt.Println("person don't deleted")
    }
}
 
  
 
 

你可能感兴趣的:(xorm,go)