Documentation ¶
Overview ¶
插入操作 https://gorm.io/docs/create.html
删除操作 https://gorm.io/docs/delete.html
https://gorm.io/gen/ https://gorm.io/gen/gen_tool.html go install gorm.io/gen/tools/gentool@latest -dsn data source name -modelPkgName generated model code's package name -outPath specify a directory for output (default "./dao/query") -tables specify tables want to generated from, default all tables. 逗号分隔 -onlyModel only generate models (without query file)
gentool -onlyModel ^ -dsn "user:pwd@tcp(localhost:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" ^ -tables "t_account,t_country,t_timezone" ^ -modelPkgName model ^ -outPath ./types/model
https://gorm.io/ https://gorm.io/docs/
https://gorm.io/docs/models.html By default, GORM uses ID as primary key, pluralizes struct name to snake_cases as table name, snake_case as column name, and uses CreatedAt, UpdatedAt to track creating/updating time 使用TableName()方法自定义表名
模型tag `gorm:""` 键值用:连接,字段用;连接 column 列名 type column data type serializer 序列器 json/gob/unixtime primaryKey 主键 unique 唯一 index 索引 comment 注释
type 指定int type:int(32) 指定bigint type:bigint 默认字段类型 int8 tinyint int bigint uint bigint string longtext bool tinyint datatypes.JSON json time.Time datetime gorm.DeletedAt datetime
查询操作 https://gorm.io/docs/query.html https://gorm.io/docs/advanced_query.html
https://gorm.io/docs/sql_builder.html
https://gorm.io/docs/security.html
Index ¶
- Variables
- func Init(db *gorm.DB)
- func InitMySQL(c Config) error
- func InitMySQLEnv() error
- func NewLogger(c Config) logger.Interface
- func NewMysqlDB(c Config) (*gorm.DB, error)
- func NewMysqlDBLife(c Config, t time.Duration) (*gorm.DB, error)
- func NewMysqlDialector(c Config) gorm.Dialector
- func OrderCheck(fields []string, s string) bool
- func RecordNotFound(e error) bool
- type Association
- type Config
- type DB
- type Model
Constants ¶
This section is empty.
Variables ¶
var ( // 必须先调用 Init 初始化 // 数据库对象 D *gorm.DB // 数据库操作 Crud *crud )
Functions ¶
func InitMySQL ¶
InitMySQL 初始化mysql数据库操作 https://gorm.io/docs/connecting_to_the_database.html https://github.com/go-sql-driver/mysql
func NewMysqlDBLife ¶
NewMysqlDBLife 创建mysql db sets the maximum amount of time a connection may be reused. If t <= 0, connections are not closed due to a connection's age.
func NewMysqlDialector ¶
NewMysqlDialector 创建mysql dialector
func RecordNotFound ¶
RecordNotFound 检查查询函数错误是否为 ErrRecordNotFound
Types ¶
type Config ¶
type Config struct { Host string `env:"MYSQL_HOST"` Port string `env:"MYSQL_PORT"` User string `env:"MYSQL_USER"` Pass string `env:"MYSQL_PASS"` Database string `env:"MYSQL_DATABASE"` // gorm.Config // 自定义logger Logger logger.Interface // 使用默认logger时,指定log level,默认为info LogLevel logger.LogLevel // 迁移模型时,不创建外键约束 DisableForeignKeyConstraintWhenMigrating bool // table name prefix TablePrefix string // mysql.Config // 设置为0,string类型默认longtext // 否则,string类型默认varchar(DefaultStringSize) DefaultStringSize uint }
Config mysql配置
type DB ¶
数据库对象 Association() 操作关联数据
Select() 指定字段 查询 Select("name, age") Select("name", "age") Select([]string{"name", "age"}) 如果两个字段名相同,使用后面的一个
Order() 指定排序字段 可能发生 sql 注入 id# 这样会注释掉语句之后的部分 SELECT * FROM `users` ORDER BY id# LIMIT 1
Model() 指定模型指针 使用同一个指针,子句不需要重新写 db := D.Model(&User{}) db.Where("id=1").Count(&total) db.Find(&users) 否则子句需要重新写 db := D db.Model(&User{}).Where("id=1").Count(&total) db.Where("id=1").Find(&users)