Beego using ORM links and creating MySQL databases

Source: Internet
Author: User

1.0 The information on this is really very small on the website

2.0 After creating a Go project with the Bee tool, we have 2 things to do next, of course, before one thought go IDE really did not know to choose that, because on the MAC computer development, does not support file creation so a little trouble

Ultimately, it is OK to use sublime to develop. The sublime itself is a collection of command-line plug-ins so you don't have to jump in a few command-line windows.

3.0 after installing the sublime, use the shortcut key to enter sublime Pagecontrol or press Shift+command+p to open the input gosublime:rungocommand so you can create a file on the MAC command quickly created on this platform

[' ls ' | Done: the. 738533ms] App conf controllers Main.go models RoutersStatictests views[' CD views '|Done ] [ /website/apple/apps/src/app/] # [' ls '| Done207. 172909ms] category.html index.html index.tpl[' Open index.html '| Done380. 637835ms] [' Open-E index.html ' | Done526. 056184ms] [' Open-A index.html ' | Done1. 356006514s] Unable to find application named'index.html'Exit Status1[' Sublime index.html '| Done142. 359053ms]/bin/Bash:sublime:command not found exit status127[' Ln-s/applications/sublime\ text\2. App/contents/sharedsupport/bin/subl/usr/local/bin/sublime ' | Done205. 678885ms] [' Sublime index.html '| Done361. 883007ms] [ /website/apple/apps/src/app/views/] #

3.0 Okay, start talking. Beego using ORM links and creating MySQL databases

3.1 Directly on the code divided into 3 files mode.go,main.go,home.go This is to achieve business separation, first Home.go is the controller in MVC

Package Controllersimport (    "github.com/astaxie/beego"   struct  {    beego. Controller}func ( this *Maincontroller) Get () {    // define homepage Template     This " index.html " }

  3.2 Mode.go is primarily responsible for database processing (the mode layer in MVC)

Package Modelsimport ("Github.com/astaxie/beego/orm"    " Time") Type Storestruct{Id Int64 TitlestringCreated time. Time ' ORM:"Index"' views Int64 ' ORM:"Index"' Topictime time. Time ' ORM:"Index"' topiccount int64 topiclastuserid int64}type Customerstruct{Id Int64 Uid int64 TitlestringContentstring' ORM:"size (.)"' AttachmentstringCreated time. Time ' ORM:"Index"' Updated time. Time ' ORM:"Index"' views Int64 ' ORM:"Index"' Authorstringreplytime time. Time ' ORM:"Index"' replycount Int64 replylastuserid int64}func Registerdb () {//Register ModelOrm. Registermodel (New(Store),New(Customer))//Registration DriverOrm. Registerdriver ("MySQL", Orm. Dr_mysql)//registering the default databaseOrm. RegisterDatabase ("default","MySQL","Root:@/app?charset=utf8")//password is empty format}

  3.3 Another one is main.go. Connect to database at run time database tables created from the model

Package Mainimport ("App/models"    _ "app/routers"    "Github.com/astaxie/beego"    "Github.com/astaxie/beego/orm"    _"Github.com/go-sql-driver/mysql")//introducing a data Modelfunc init () {//Register Databasemodels. REGISTERDB ()}func main () {//turn on ORM debug modeOrm. Debug =true    //Auto-Build tableOrm. RUNSYNCDB ("default",false,true)    //Run-timeBeego. Run ()}

  3.4 Spit trough, feel that go learning material is really very little, into the system of project learning materials less, when to support Android Ah!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.