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 习近平将发表二〇二六年新年贺词 7904141
- 2 2026年国补政策来了 7808738
- 3 东部战区:开火!开火!全部命中! 7712893
- 4 2026年这些民生政策将惠及百姓 7616985
- 5 小学食堂米线过期2.5小时被罚5万 7519709
- 6 解放军喊话驱离台军 原声曝光 7428214
- 7 为博流量直播踩烈士陵墓?绝不姑息 7327605
- 8 每月最高800元!多地发放养老消费券 7238391
- 9 数字人民币升级 1月1日起将计付利息 7141831
- 10 2026年1月1日起 一批新规将施行 7040675








PingCAP
