beego orm mysql


beego框架中的rom支持mysql

項目中使用到mvc模式,總結下使用方式;

models中

package models

import (
    //使用beego orm 必備
    "github.com/astaxie/beego/orm"
    //使用的數據庫 必備
    _ "github.com/go-sql-driver/mysql" // import your used driver
)

type BlogLogin struct {
    Id            int64
    Name          string
    Pwd           string
    WechatId      string
    WechatInfo    string
    CreateTime    string
    LastLoginIp   string
    LastLoginTime string
}

func RegisterDB() {

    //注冊 model
    orm.RegisterModel(new(BlogLogin))
    //注冊默認數據庫
    orm.RegisterDataBase("default", "mysql", "username:password@/databasename?charset=utf8") //密碼為空格式

當model創建了一個type ,在RegisterDB中調用該方法創建表

    //orm.RunSyncdb("default", false, true)

創建表結構案例

type User struct {
    Id          int
    Name        string
    Profile     *Profile   `orm:"rel(one)"` // OneToOne relation
    Post        []*Post `orm:"reverse(many)"` // 設置一對多的反向關系
}

type Profile struct {
    Id          int
    Age         int16
    User        *User   `orm:"reverse(one)"` // 設置一對一反向關系(可選)
}

type Post struct {
    Id    int
    Title string
    User  *User  `orm:"rel(fk)"`    //設置一對多關系
    Tags  []*Tag `orm:"rel(m2m)"`
}

type Tag struct {
    Id    int
    Name  string
    Posts []*Post `orm:"reverse(many)"`
}

然后main中初始化,創建表

package main

import (
    //調用models中registerDB方法注冊
    "blog/models"
    //設置路由,必備
    _ "blog/routers"
    //beego控制器使用必備
    "github.com/astaxie/beego"
    //開啟調試默認
    "github.com/astaxie/beego/orm"
)

func init() {
    models.RegisterDB()

}

func main() {
    orm.Debug = true
    beego.Run()
}

接下來在controller中使用

package controllers

import (
    //使用model中的類型BlogLogin
    "blog/models"
    //打印數據庫查出來的結果
    "fmt"
    //beego控制器必備
    "github.com/astaxie/beego"
    //使用orm 中的查詢方法
    "github.com/astaxie/beego/orm"
)

type AdminLoginController struct {
    beego.Controller
}

func (this *AdminLoginController) Get() {
    this.TplName = "AdminLogin.html"
}

func (this *AdminLoginController) Post() {

    name := this.Input().Get("name")
    pwd := this.Input().Get("pwd")

    o := orm.NewOrm()
    // read one
    login := models.BlogLogin{Name: name, Pwd: pwd}
    //read默認根據主鍵查詢,下面我設置的為跟怒name 和pwd 查詢
    err := o.Read(&login, "Name", "Pwd")
    if err != nil {
        fmt.Printf("ERR: %v\n", err)
        this.Redirect("/adminlogin.html", 301)
        return
    }
    fmt.Printf("Data: %v\n", login)
this.Redirect("/admin.html", 301) return }

下面為一些標准的sql使用:

    // insert
    id, err := o.Insert(&user)
    fmt.Printf("ID: %d, ERR: %v\n", id, err)

    // update
    //user.Name = "astaxie"
    //num, err := o.Update(&user)
    //fmt.Printf("NUM: %d, ERR: %v\n", num, err)

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

    // delete
    //num, err = o.Delete(&u)
    //fmt.Printf("NUM: %d, ERR: %v\n", num, err)

詳情來自官網:https://beego.me/docs/mvc/model/orm.md


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM