Beego Framework ORM Module

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Reference Beego Original: Http://beego.me/docs/mvc/model/orm.md#main.go

Beego ORM is a powerful Go language ORM framework, and the ORM module deals mainly with M (models) in MVC.

First, create a project with the Bee tool named: Beegoorm, modified files are Beegoorm/main.go and Beegoorm/models/models.go (only 2 files)

Beegoorm/models/models.go

Package Modelsimport ("Github.com/astaxie/beego/orm") Type Userstruct{IdintNamestring Profile*profile ' ORM:"rel (one)"`//Onetoone Relation}type Profilestruct{IdintAge int16 User*user ' ORM:"reverse (one)"`//Set inverse relationship (optional)}func init () {//you need to register the defined model in initOrm. Registermodel (New(User),New(Profile))}

Beegoorm/main.go

Package Mainimport ("Beegoorm/models"    "FMT"    "Github.com/astaxie/beego"    "Github.com/astaxie/beego/orm"    _ "beegoorm/routers"    _ "Github.com/go-sql-driver/mysql") Func init () {//Registration DriverOrm. Registerdriver ("MySQL", Orm. Dr_mysql)//registering the default database//my MySQL root user password is Tom, and I'm going to set up the data table in the database named Test.//Note: The first parameter here must be set to "default" (because I now have only one database), otherwise the compilation error says: Must have a registered DB alias for defaultOrm. RegisterDatabase ("default","MySQL","Root:tom@/test?charset=utf8")}func Main () {//Turn on ORM Debug mode: recommended to open during development, need to close when releaseOrm. Debug =true    //Auto-Build tableOrm. RUNSYNCDB ("default",false,true)    //Create a Ormer objecto: =ORM. Neworm () o.using ("default") Perfile:=New(models. Profile) Perfile. Age= -User:=New(models. User) user. Name="Tom"user. profile=Perfile//InsertO.insert (perfile) o.insert (user) O.insert (perfile) o.insert (user) O.insert (perfile) o.insert (user) //UpdateUser. Name ="Hezhixiong"num, err:=o.update (user) fmt. Printf ("NUM:%d, ERR:%v\n", num, err)//DeleteO.delete (&models. User{id:2}) Beego. Run ()}

The results of database building tables and data additions and deletions are as follows:
Database name: Test
Data sheet: User,profile
User Table Profile Table

        

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.