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)
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 主键 index 索引 comment 注释
默认字段类型 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
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 RecordNotFound(e error) bool
- type Config
- type DB
- type Model
Constants ¶
This section is empty.
Variables ¶
var 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 LogLevel logger.LogLevel // 迁移模型时,不创建外键约束 DisableForeignKeyConstraintWhenMigrating bool // table name prefix TablePrefix string // mysql.Config // 设置为0,string类型默认longtext // 否则,string类型默认varchar(DefaultStringSize) DefaultStringSize uint }
Config mysql配置