Beego ORM MySQL

Source: Internet
Author: User

The ROM in the Beego framework supports MySQL

The MVC pattern is used in the project, and the usage method is summarized.

In Models

Package Modelsimport (//Use Beego orm Prerequisites    "Github.com/astaxie/beego/orm"    //database Prerequisites for use_"Github.com/go-sql-driver/mysql" //import your used driver) Type Blogloginstruct{Id Int64 NamestringPWDstringWechatidstringWechatinfostringCreatetimestringLastloginipstringLastlogintimestring}func Registerdb () {//Register ModelOrm. Registermodel (New(bloglogin))//registering the default databaseOrm. RegisterDatabase ("default","MySQL","Username: [Email Protected]/databasename?charset=utf8")//password is empty format

When the model creates a type, the method is called in Registerdb to create the table

    // ORM. RUNSYNCDB ("Default", False, True)

Create a table structure case

Type Userstruct{IdintNamestring Profile*profile ' ORM:"rel (one)"`//Onetoone RelationPost []*post ' ORM:"reverse (many)"`//set a one-to-many inverse relationship}type Profilestruct{IdintAge int16 User*user ' ORM:"reverse (one)"`//set a one-to-one reverse relationship (optional)}type Poststruct{IdintTitlestringUser*user ' ORM:"rel (FK)"`//set up a one-to-many relationshipTags []*tag ' ORM:"rel (on/off)"'}type Tagstruct{IdintNamestringPosts []*post ' ORM:"reverse (many)"`}

Then initialize in main, create the table

Package Mainimport (//call Models in Registerdb method registration    "Blog/models"    //set up routing, prerequisites_"blog/routers"    //Beego Controller Usage Prerequisites    "Github.com/astaxie/beego"    //turn on debug default    "Github.com/astaxie/beego/orm") Func init () {models. REGISTERDB ()}func main () {orm. Debug=trueBeego. Run ()}

Then use it in the controller.

Package Controllersimport (//using the type Bloglogin in model    "Blog/models"    //print out the database and find out the results    "FMT"    //Beego Controller Prerequisites    "Github.com/astaxie/beego"    //using the Query method in ORM    "Github.com/astaxie/beego/orm") Type Adminlogincontrollerstruct{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 OneLogin: =models. Bloglogin{name:name, pwd:pwd}//read by default based on primary key query, below I set for angry name and pwd queryERR: = O.read (&login,"Name","PWD")    ifErr! =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}

Here are some of the standard SQL uses:

    //InsertID, 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)

Details from official website: https://beego.me/docs/mvc/model/orm.md

Beego ORM MySQL

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.