db

package module
v0.0.0-...-fbae199 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

自定义表名

模型中自定义表名设置

type Tabler interface {
TableName() string
}

// TableName 会将 User 的表名重写为 `user_new_name`
func (User) TableName() string {
return "user_new_name"
}

列名设置

type User struct {
  ID        uint      // 列名是 `id`
  Name      string    // 列名是 `name`
  Birthday  time.Time // 列名是 `birthday`
  CreatedAt time.Time // 列名是 `created_at`
}

type User struct {
ID int64     `gorm:"column:user_id"`         // 将列名设为 `user_id`
Name string `gorm:"column:user_name"` // 将列名设为 `user_name`
Birthday      time.Time     `gorm:"column:user_birthday"` // 将列名设为 `user_birthday`
}

数据行创建和更新时间设置

type User struct {
  CreatedAt time.Time // 在创建时,如果该字段值为零值,则使用当前时间填充
  UpdatedAt int       // 在创建时该字段值为零值或者在更新时,使用当前时间戳秒数填充
  Updated   int64 `gorm:"autoUpdateTime:nano"` // 使用时间戳填纳秒数充更新时间
  Updated   int64 `gorm:"autoUpdateTime:milli"` // 使用时间戳毫秒数填充更新时间
  Created   int64 `gorm:"autoCreateTime"`      // 使用时间戳秒数填充创建时间
}

结构体字段嵌入

type Goods struct {
    Name  string
    Price 
}


Documentation

Index

Constants

View Source
const (
	DefaultMaxOpenConn        = 1000
	DefaultMaxIdleConn        = 100
	DefaultConnMaxLifeSecond  = 30 * time.Minute
	DefaultLogName            = "gorm"
	DefaultSlowLogMillisecond = 200
	DefaultClient             = "default-mysql-client"
	ReadClient                = "read-mysql"
	WriteClient               = "write-mysql"
	TxClient                  = "tx-mysql"
)

Variables

View Source
var (
	MysqltdLogger stdLogger
)

Functions

func CloseMysqlClient

func CloseMysqlClient(clientName string) error

func InitMysqlClient

func InitMysqlClient(clientName, username, password, host, dbName string) error

func InitMysqlClientWithOptions

func InitMysqlClientWithOptions(clientName, username, password, host, dbName string, options ...Option) error

Types

type DB

type DB struct {
	*gorm.DB
	ClientName string
	Username   string

	Host   string
	DBName string
	// contains filtered or unexported fields
}

func GetMysqlClient

func GetMysqlClient(clientName string) *DB

type Option

type Option func(*option)

func WithConnMaxLifeSecond

func WithConnMaxLifeSecond(connMaxLifeTime time.Duration) Option

func WithEnableSqlLog

func WithEnableSqlLog(enableSqlLog bool) Option

func WithLogName

func WithLogName(logName string) Option

func WithMaxIdleConn

func WithMaxIdleConn(maxIdleConn int) Option

func WithMaxOpenConn

func WithMaxOpenConn(maxOpenConn int) Option

func WithPrepareStmt

func WithPrepareStmt(prepareStmt bool) Option

func WithSlowLogMillisecond

func WithSlowLogMillisecond(slowLogMillisecond int64) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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