Gen Guides | GORM - The fantastic ORM library for Golang, aims to be developer friendly.


本站和网页 https://gorm.io/gen/index.html 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

Gen Guides | GORM - The fantastic ORM library for Golang, aims to be developer friendly.
GORM
DocsGenCommunityAPIContribute
English
English
简体中文
Deutsch
Español
bahasa Indonesia
Italiano
日本語
Русский
French
한국어
Gen Guides
GEN GuidesGEN: Friendly & Safer GORM powered by Code Generation.
Overview
Idiomatic & Reusable API from Dynamic Raw SQL
100% Type-safe DAO API without interface{}
Database To Struct follows GORM conventions
GORM under the hood, supports all features, plugins, DBMS that GORM supports
Installgo get -u gorm.io/gen
Quick startIt is quite straightforward to use gen for your application, here is how it works:
1. Write the configuration in golang
package mainimport "gorm.io/gen"// Dynamic SQLtype Querier interface { // SELECT * FROM @@table WHERE name = @name{{if role !=""}} AND role = @role{{end}} FilterWithNameAndRole(name, role string) ([]gen.T, error)}func main() { g := gen.NewGenerator(gen.Config{ OutPath: "../query", Mode: gen.WithoutContext|gen.WithDefaultQuery|gen.WithQueryInterface, // generate mode }) // gormdb, _ := gorm.Open(mysql.Open("root:@(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local")) g.UseDB(gormdb) // reuse your gorm db // Generate basic type-safe DAO API for struct `model.User` following conventions g.ApplyBasic(model.User{}) // Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company` g.ApplyInterface(func(Querier){}, model.User{}, model.Company{}) // Generate the code g.Execute()}
2. Generate Code
go run main.go
3. Use the generated code in your project
import "your_project/query"func main() { // Basic DAO API user, err := query.User.Where(u.Name.Eq("modi")).First() // Dynamic SQL API users, err := query.User.FilterWithNameAndRole("modi", "admin")}
Last updated: 2022-12-15
Next
Platinum Sponsors
Become a Sponsor!
Gold Sponsors
Become a Sponsor!
Platinum Sponsors
Become a Sponsor!
Gold Sponsors
Become a Sponsor!
Contents
GEN GuidesOverviewInstallQuick start
Improve this page
Back to Top
Getting StartedOverviewDynamic SQLOverviewAnnotationDAOOverviewCreateQueryUpdateDeleteTransactionAssociationsDatabase To StructsOverviewGen ToolTutorialsGORM Clause
2013~2022 Jinzhu
Documentation licensed under CC BY 4.0.
感谢 七牛云 对 CDN 的赞助,无闻 对域名 gorm.cn 的捐赠
浙ICP备2020033190号-1
Home
DocsGenCommunityAPIContribute
Getting StartedOverviewDynamic SQLOverviewAnnotationDAOOverviewCreateQueryUpdateDeleteTransactionAssociationsDatabase To StructsOverviewGen ToolTutorialsGORM Clause
English
English
简体中文
Deutsch
Español
bahasa Indonesia
Italiano
日本語
Русский
French
한국어