好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

GORM操作MySQL数据库-连接数据库以及对表的操作

root:123456@tcp(127.0.0.1:3306)/more?charset=utf8mb4&parseTime=True&loc=Local " db, err : = gorm.Open(mysql.Open(dsn), & gorm.Config{}) if err != nil{ return }

账号:密码@tcp(127.0.0.1:3306)/库名

二、迁移表

 package main

import (
     "  gorm.io/driver/mysql  " 
    "  gorm.io/gorm  " 
    "  time  "  
)

type User   struct   {
   ID   int  
   Name   string  
   CreatedTime time.Time
}
func main() {
   dsn : =  "  root:123456@tcp(127.0.0.1:3306)/more?charset=utf8mb4&parseTime=True&loc=Local  "  
   db, err : = gorm.Open(mysql.Open(dsn), & gorm.Config{})
     if  err !=  nil{
        return  
   }
   db.AutoMigrate( & User{})
} 

 关于表名: GORM 将 struct name 复数 snake_cases 为表名,对于 struct  User ,其表名是 users 约定俗成的,

测试一下

 
type User   struct   {
    ID   int  
    Name   string  
    CreatedTime time.Time
}
  type UserInfo      struct {
    ID int
    Info string     
}  

func main() {
    dsn : =  "  root:123456@tcp(127.0.0.1:3306)/more?charset=utf8mb4&parseTime=True&loc=Local  "  
    db, err : = gorm.Open(mysql.Open(dsn), & gorm.Config{})
      if  err !=  nil{
          return  
    }
    db.AutoMigrate( & User{})
    db.AutoMigrate( & UserInfo{})

} 

自定义表名:实现Tabler接口

例:

 
type User   struct   {
    ID   int  
    Name   string  
    CreatedTime time.Time
}
  type UserInfo      struct {
    ID int
    Info string
}

func (UserInfo) TableName() string {
    return "infos"     
}  

func main() {
    dsn : =  "  root:123456@tcp(127.0.0.1:3306)/more?charset=utf8mb4&parseTime=True&loc=Local  "  
    db, err : = gorm.Open(mysql.Open(dsn), & gorm.Config{})
      if  err !=  nil{
          return  
    }
    db.AutoMigrate( & User{})
    db.AutoMigrate( & UserInfo{})

} 

 

 

 还有一种方法自定义表名:

 
type User   struct   {
    ID   int  
    Name   string  
    CreatedTime time.Time
}
type UserInfo   struct   {
    ID   int  
    Info   string  
}

func (UserInfo) TableName()   string   {
      return   "  infos  "  
}

func main() {
    dsn : =  "  root:123456@tcp(127.0.0.1:3306)/more?charset=utf8mb4&parseTime=True&loc=Local  "  
    db, err : = gorm.Open(mysql.Open(dsn), & gorm.Config{})
      if  err !=  nil{
          return  
    }
      //  db.AutoMigrate(&User{})
      //  db.AutoMigrate(&UserInfo{}) 
      db.Table("info_table").AutoMigrate(&     UserInfo{})  

} 

 

 

 

关于字段名:

type User struct {
ID uint // column name is `id`
Name string // column name is `name`
Birthday time.Time // column name is `birthday`
CreatedAt time.Time // column name is `created_at`
}
当然你也可以自定义
type Animal struct {
AnimalID int64 `gorm:"column:beast_id"` // set name to `beast_id`
Birthday time.Time `gorm:"column:day_of_the_beast"` // set name to `day_of_the_beast`
Age int64 `gorm:"column:age_of_the_beast"` // set name to `age_of_the_beast`
}
看起来无论表名还是字段名,GORM默认的规则都是比较合适的,命名比较规范的话,使用默认的即可。

 

GORM操作MySQL数据库-连接数据库以及对表的操作

标签:使用   ted   cas   rate   ring   mamicode   more   http   hda   

查看更多关于GORM操作MySQL数据库-连接数据库以及对表的操作的详细内容...

  阅读:36次