gorm

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(dbHost, dbPort, dbName, username, password string) (db *gorm.DB, err error)

Open 建立数据库连接并返回连接对象 db。

Types

type Model

type Model struct {
	ID        uint  `gorm:"primary_key" json:"id"`
	CreatedAt Time  `json:"createdAt"`
	UpdatedAt Time  `json:"updatedAt"`
	DeletedAt *Time `sql:"index" json:"-"`
}

Model 用来替代 gorm.Model,时间字段的类型均为 Time,而非 time.Time。

type Time

type Time struct {
	time.Time
}

Time 继承 time.Time 的所有方法,并重写 MarshalJSON 方法, Time 进行 json 序列化时,会转为字符串 "2006-01-02 15:04:05" 的格式。

func (Time) MarshalJSON

func (t Time) MarshalJSON() (data []byte, err error)

MarshalJSON 将 Time 类型转为 "2006-01-02 15:04:05" 格式。

func (*Time) Scan

func (t *Time) Scan(v interface{}) error

Scan 在 gorm 进行读操作将数据库时间值转换为 Time 结构体字段时用到, 如果 Time 没有此方法,则会报错:无法将 time.Time 类型赋值到 Time 类型。

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value 在 gorm 进行写操作将 Time 结构体字段转为数据库时间值时用到, 如果 Time 没有此方法,则会报错:不支持 Time 类型。

Jump to

Keyboard shortcuts

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