Facebook 开源 Golang 实体框架 Ent 现已支持 TiDB
原文作者:Amit Shani,Ent Engineer
对于后端开发者来说,一款好用的框架能够大大提升应用的开发效率。为了降低开发者使用 TiDB 的门槛,方便开发者快速连接到 TiDB,我们也在和合作伙伴一起,逐步完善面向主流开发语言和框架的连接支持。
Hello World 应用示例
docker run -p 4000:4000 pingcap/tidb
git clone https://github.com/hedwigz/tidb-hello-world.git
go title="ent/schema/user.go"
func (User) Fields() []ent.Field {
return []ent.Field{
field.Time("created_at").
Default(time.Now),
field.String("name"),
field.Int("age"),
}
}
go title="main.go"
client, err := ent.Open("mysql", "root@tcp(localhost:4000)/test?parseTime=true")
if err != nil {
log.Fatalf("failed opening connection to TiDB: %v", err)
}
defer client.Close()
// Run the auto migration tool, with Atlas.
if err := client.Schema.Create(context.Background(), schema.WithAtlas(true)); err != nil {
log.Fatalf("failed printing schema changes: %v", err)
}
go title="main.go"
client.User.Create().
SetAge(30).
SetName("hedwigz").
SaveX(context.Background())
user := client.User.Query().FirstX(context.Background())
fmt.Printf("the user: %s is %d years old\n", user.Name, user.Age)
$ go run main.go
the user: hedwigz is 30 years old
版本说明
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/

随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 跟着总书记看火热的改革发展现场 7904054
- 2 育儿补贴来了!3岁前每娃每年3600元 7808391
- 3 哪些家庭可以领育儿补贴?怎么领 7714315
- 4 华北东北持续多雨 警惕次生灾害 7619018
- 5 国家明确!育儿补贴免征个税 7520529
- 6 2025百度热搜年中报告出炉 7425854
- 7 中方已在北京和东京向日方严正交涉 7328645
- 8 《南京照相馆》适合孩子看吗 7235052
- 9 揭“大熊猫被虐”谣言背后利益链 7141588
- 10 年收入12万以下基本无需缴税 7045601