Golang beego/orm
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package main import ( "fmt" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) type User struct { Id int `orm: "auto" ` Name string `orm: "size(100)" ` } func init() { orm.RegisterModel(new(User)) orm.RegisterDataBase( "default" , "mysql" , "username:pwd@tcp(***.***.***.***:3306)/pomelo?charset=utf8" ,30) orm.RunSyncdb( "default" , false, true) } func main() { o := orm.NewOrm() user := User{ Name: "Michael" } id, err := o.Insert(&user) if err != nil { fmt.Println(err.Error()) return } fmt.Println( "id=" , id); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package main import ( "fmt" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) type User struct { Id int Name string Profile *Profile `orm: "rel(one)" ` // OneToOne relation Post []*Post `orm: "reverse(many)" ` // 设置一对多的反向关系 } type Profile struct { Id int Age int16 User *User `orm: "reverse(one)" ` // 设置一对一反向关系(可选) } type Post struct { Id int Title string User *User `orm: "rel(fk)" ` //设置一对多关系 Tags []*Tag `orm: "rel(m2m)" ` } type Tag struct { Id int Name string Posts []*Post `orm: "reverse(many)" ` } func init() { orm.RegisterModel(new(User), new(Post), new(Profile), new(Tag)) orm.RegisterDataBase( "default" , "mysql" , "username:pwd@tcp(xxx.xxx.xxx.xxx:3306)/pomelo?charset=utf8" ,30) orm.RunSyncdb( "default" , false, true) } func main() { o := orm.NewOrm() profile := new(Profile) profile.Age = 30 user := new(User) user.Profile = profile user.Name = "slene" fmt.Println(o.Insert(profile)) fmt.Println(o.Insert(user)) } |
log:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | create table ` user ` -- -------------------------------------------------- -- Table Structure for `main.User` -- -------------------------------------------------- CREATE TABLE IF NOT EXISTS ` user ` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY , ` name ` varchar (255) NOT NULL DEFAULT '' , `profile_id` integer NOT NULL UNIQUE ) ENGINE=InnoDB; create table `post` -- -------------------------------------------------- -- Table Structure for `main.Post` -- -------------------------------------------------- CREATE TABLE IF NOT EXISTS `post` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY , `title` varchar (255) NOT NULL DEFAULT '' , `user_id` integer NOT NULL ) ENGINE=InnoDB; create table `profile` -- -------------------------------------------------- -- Table Structure for `main.Profile` -- -------------------------------------------------- CREATE TABLE IF NOT EXISTS `profile` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY , `age` smallint NOT NULL DEFAULT 0 ) ENGINE=InnoDB; create table `tag` -- -------------------------------------------------- -- Table Structure for `main.Tag` -- -------------------------------------------------- CREATE TABLE IF NOT EXISTS `tag` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY , ` name ` varchar (255) NOT NULL DEFAULT '' ) ENGINE=InnoDB; create table `post_tags` -- -------------------------------------------------- -- Table Structure for `main.PostTags` -- -------------------------------------------------- CREATE TABLE IF NOT EXISTS `post_tags` ( `id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY , `post_id` integer NOT NULL , `tag_id` integer NOT NULL ) ENGINE=InnoDB; 1 <nil> 1 <nil> Process finished with exit code 0 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下