Discover Packages
github.com/xvrzhao/utils
db
gorm
package
Version:
v0.11.0
Opens a new window with list of versions in this module.
Published: Jan 14, 2021
License: MIT
Opens a new window with license information.
Imports: 6
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
Documentation
Documentation
¶
func Open(dbHost, dbPort, dbName, username, password string ) (db *gorm .DB , err error )
Open 建立数据库连接并返回连接对象 db。
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。
Time 继承 time.Time 的所有方法,并重写 MarshalJSON 方法,
Time 进行 json 序列化时,会转为字符串 "2006-01-02 15:04:05" 的格式。
MarshalJSON 将 Time 类型转为 "2006-01-02 15:04:05" 格式。
Scan 在 gorm 进行读操作将数据库时间值转换为 Time 结构体字段时用到,
如果 Time 没有此方法,则会报错:无法将 time.Time 类型赋值到 Time 类型。
Value 在 gorm 进行写操作将 Time 结构体字段转为数据库时间值时用到,
如果 Time 没有此方法,则会报错:不支持 Time 类型。
Source Files
¶
Click to show internal directories.
Click to hide internal directories.