4.3 调用Gorm

4.3.1 简介

4.3.2 配置

Gorm配置说明4.3 调用Gorm - 图1

  1. [jupiter.mysql.test]
  2. connMaxLifetime = "300s"
  3. debug = true
  4. dsn = "root:test@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local&readTimeout=1s&timeout=1s&writeTimeout=3s"
  5. level = "panic"
  6. maxIdleConns = 50
  7. maxOpenConns = 100

4.3.3 用法

  1. type User struct {
  2. Id int `gorm:"not null" json:"id"`
  3. Name string `gorm:"not null" json:"name"`
  4. }
  5. func main() {
  6. gormDB := gorm.StdConfig("test").Build()
  7. models := []interface{}{
  8. &User{},
  9. }
  10. gormDB.SingularTable(true)
  11. gormDB.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(models...)
  12. gormDB.Create(&User{
  13. Name: "jupiter",
  14. })
  15. var user User
  16. gormDB.Where("id = 1").Find(&user)
  17. xlog.Info("user info",xlog.String("name",user.Name))
  18. }

运行指令go run main.go --config=config.toml,可以得到以下结果 image