ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### 创建时间Created Created可以让您在数据插入到数据库时自动将对应的字段设置为当前时间,需要在xorm标记中使用created标记,如下所示进行标记,对应的字段可以为time.Time或者自定义的time.Time或者int,int64等int类型。 ~~~ type User struct { Id int64 Name string CreatedAt time.Time `xorm:"created"` } ~~~ 或 ~~~ type JsonTime time.Time func (j JsonTime) MarshalJSON() ([]byte, error) { return []byte(`"`+time.Time(j).Format("2006-01-02 15:04:05")+`"`), nil } type User struct { Id int64 Name string CreatedAt JsonTime `xorm:"created"` } ~~~ 或 ~~~ type User struct { Id int64 Name string CreatedAt int64 `xorm:"created"` } ~~~ 在Insert()或InsertOne()方法被调用时,created标记的字段将会被自动更新为当前时间或者当前时间的秒数(对应为time.Unix()),如下所示: ~~~ var user User engine.Insert(&user) // INSERT user (created...) VALUES (?...) ~~~