beego 使用orm鏈接以及創建mysql數據庫


1.0  這方面的資料在網站上確實很少

2.0  在用bee工具創建一個go項目后,接下來我們有2件事要做了,當然之前一只覺得GO的IDE實在不知道選着那個,因為在Mac電腦上開發,又不支持文件創建所以有點麻煩

    最終還是確定用sublime來開發。sublime本身集合了命令行插件這樣開發起來就不用在幾個命令行窗口跳轉

3.0  安裝好sublime后用快捷鍵進入sublime pagecontrol  或按shift+command+p 打開 輸入GOSUBLIME:rungocommand  這樣就可以Mac 命令創建文件 在這個平台上快速創建了

[ `ls` | done: 130.738533ms ]
    app
    conf
    controllers
    main.go
    models
    routers
    static
    tests
    views
[ `cd views` | done ]
[ /website/apple/apps/src/app/ ] # 
[ `ls` | done: 207.172909ms ]
    category.html
    index.html
    index.tpl
[ `open index.html` | done: 380.637835ms ]
[ `open -e index.html` | done: 526.056184ms ]
[ `open -a index.html` | done: 1.356006514s ]
    Unable to find application named 'index.html'
    
    exit status 1
[ `sublime index.html` | done: 142.359053ms ]
    /bin/bash: sublime: command not found
    
    exit status 127
[ `ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime` | done: 205.678885ms ]
[ `sublime index.html` | done: 361.883007ms ]
[ /website/apple/apps/src/app/views/ ] # 

 

3.0  好了,開始說beego 使用orm鏈接以及創建mysql數據庫

    3.1 直接上代碼分3個文件mode.go,main.go,home.go這樣做是為了實現業務分離 ,先來home.go是mvc中controller

package controllers

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (this *MainController) Get() {
    //定義首頁模板
    this.TplNames = "index.html"
}

  3.2 mode.go 主要是負責數據庫處理(mvc中的mode層)

package models

import (
    "github.com/astaxie/beego/orm"
    "time"
)

type Store struct {
    Id              int64
    Title           string
    Created         time.Time `orm:"index"`
    Views           int64     `orm:"index"`
    TopicTime       time.Time `orm:"index"`
    TopicCount      int64
    TopicLastUserId int64
}

type Customer struct {
    Id              int64
    Uid             int64
    Title           string
    Content         string `orm:"size(5000)"`
    Attachment      string
    Created         time.Time `orm:"index"`
    Updated         time.Time `orm:"index"`
    Views           int64     `orm:"index"`
    Author          string
    ReplyTime       time.Time `orm:"index"`
    ReplyCount      int64
    ReplyLastUserId int64
}

func RegisterDB() {
    //注冊 model
    orm.RegisterModel(new(Store), new(Customer))
    //注冊驅動
    orm.RegisterDriver("mysql", orm.DR_MySQL)
    //注冊默認數據庫
    orm.RegisterDataBase("default", "mysql", "root:@/app?charset=utf8")//密碼為空格式

}

  3.3 再一個就是main.go  負責在運行時連接數據庫根據模型創建數據庫表

package main

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

//引入數據模型
func init() {
    // 注冊數據庫
    models.RegisterDB()
}

func main() {
    // 開啟 ORM 調試模式
    orm.Debug = true
    // 自動建表
    orm.RunSyncdb("default", false, true)
    // 運行時
    beego.Run()
}

  3.4 吐槽一下,感覺go的學習資料真的很少,成系統的項目學習資料就更少了,什么時候支持安卓啊!

 


免責聲明!

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



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