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的学习资料真的很少,成系统的项目学习资料就更少了,什么时候支持安卓啊!

 

posted @ 2014-07-29 11:17  开心编码源  阅读(17973)  评论(0编辑  收藏  举报