# gorm-plus **Repository Path**: hlhutu/gorm-plus ## Basic Information - **Project Name**: gorm-plus - **Description**: gorm的扩展,半自动的orm框架 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-05-20 - **Last Updated**: 2025-05-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # gorm的扩展 ## 准备工作 假设 `ReportGame` 是一个数据库Model 一个结构体继承 `gormp.Model[ReportGame]` ```go type ReportGame struct {// model Id int `gorm:"column:id;primaryKey"` Name string `gorm:"name"` Address string `gorm:"address"` } type reportGamesModel struct {// 相当于dao层对象 gormp.Model[ReportGame] } // 实例化 reportGamesModelInst := reportGamesInst = reportGamesModel{ Model: gormp.NewModel(ReportGame{}),// 传入Model } ``` ## 默认的增删改查函数 ```go // 比如查询全部 datas := reportGamesModelInst.ListAll() ``` sql: `select * from report_games` ## 自动识别条件 会把传入的条件解析为 where 条件进行查询 ```go // 根据Name查询 datas := reportGamesModelInst.List(&ReportGame{Name: "BCBM"}) ``` sql: `select * from report_games where name="BCBM"` ## 允许null值传递 只会识别到非空(零值)的字段,空字段不会识别。 ```go // 根据Name查询 datas := reportGamesModelInst.List(&ReportGame{Id: 0, Name: "BCBM", Address: ""}) ``` sql: `select * from report_games where name="BCBM"` 不会识别到`0`和`""` 如果想要 `select * from report_games where name="BCBM" and id=0 and address=''` 那么model中请用null包里面的类型`null.Int`、`null.String`等 ```go type ReportGame struct {// model Id null.Int `gorm:"column:id;primaryKey"` Name null.String `gorm:"name"` Address null.String `gorm:"address"` } ``` 然后这样使用 ```go // 多条件查询 datas := reportGamesModelInst.List(&ReportGame{Id: null.NewInt(0, true)}) ``` ## 动态条件 ## 复杂条件 ## 自动事务 ## 支持原生gorm ## 代码生成器 code-generator