golang orm

2019-05-31 10:14:30 浏览数 (2)

代码语言:javascript复制
package main

import (
    "fmt"
    "github.com/astaxie/beego/orm"
    _"github.com/go-sql-driver/mysql"
)

type User struct{
    Id int
    Name string `orm:"size(100)"`
    Fuck string
    Admin int
}

func init() {
    // set default database
    orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/fuck?charset=utf8", 30)

    // register model
    orm.RegisterModel(new(User))

    // create table
    orm.RunSyncdb("default", false, true)
}
func main() {
    o :=orm.NewOrm()

    user:=User{Name:"slene"}
    id,err :=o.Insert(&user)
    fmt.Printf("ID:%d,ERR:%vn",id,err)

    user.Name="astaxie"
    num,err:=o.Update(&user)
    fmt.Printf("NUM:%d,ERR:%vn",num,err)

    // read one
    u := User{Id: user.Id}
    err = o.Read(&u)
    fmt.Printf("ERR: %vn", err)

    fmt.Println("------------------")
    var maps []orm.Params
    num, err = o.Raw("SELECT * FROM user").Values(&maps)
    for _,term := range maps{
        fmt.Println(term["id"],":",term["name"],term["fuck"])
    }

    //// delete
    //num, err = o.Delete(&u)
    //fmt.Printf("NUM: %d, ERR: %vn", num, err)
}
orm

0 人点赞