gorm

package
v0.0.0-...-5ef37d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 13 Imported by: 0

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

https://gorm.io/docs/transactions.html

更新操作 https://gorm.io/docs/update.html

Index

Constants

This section is empty.

Variables

View Source
var (
	// 必须先调用 Init 初始化
	// 数据库对象
	D *gorm.DB
	// 数据库操作
	Crud *crud
)

Functions

func Init

func Init(db *gorm.DB)

Init 初始化数据库实例

func InitMySQLEnv

func InitMySQLEnv() error

InitMySQLEnv 使用环境变量初始化mysql数据库操作

func NewLogger

func NewLogger(c Config) logger.Interface

NewLogger 创建logger

func NewMysqlDB

func NewMysqlDB(c Config) (*gorm.DB, error)

NewMysqlDB 创建mysql db

func NewMysqlDBLife

func NewMysqlDBLife(c Config, t time.Duration) (*gorm.DB, error)

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

func NewMysqlDialector(c Config) gorm.Dialector

NewMysqlDialector 创建mysql dialector

func OrderCheck

func OrderCheck(fields []string, s string) bool

OrderCheck 排序字段检查

func RecordNotFound

func RecordNotFound(e error) bool

RecordNotFound 检查查询函数错误是否为 ErrRecordNotFound

Types

type Association

type Association = gorm.Association

关联模式

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

type DB = gorm.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)

type Model

type Model = gorm.Model

基础模型

Directories

Path Synopsis
开放gorm.io/gorm/logger包的logger
开放gorm.io/gorm/logger包的logger

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL