From 7165f7f2e53d87026d768f1f05b2c0bd77dac16b Mon Sep 17 00:00:00 2001 From: han-joker Date: Mon, 3 Apr 2023 20:33:19 +0800 Subject: [PATCH] first commit --- .gitignore | 42 +++++++++++++++ README.md | 1 + basic.go | 125 +++++++++++++++++++++++++++++++++++++++++++++ basic_test.go | 23 +++++++++ docker-compose.yml | 11 ++++ go.mod | 11 ++++ go.sum | 12 +++++ 7 files changed, 225 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 basic.go create mode 100644 basic_test.go create mode 100644 docker-compose.yml create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9cd587c --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +# Reference https://github.com/github/gitignore/blob/master/Go.gitignore +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +vendor/ + +# Go workspace file +go.work + +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# OS General +Thumbs.db +.DS_Store + +# project +*.cert +*.key +*.log +bin/ + +# Develop tools +.vscode/ +.idea/ +*.swp + +# volumes +data/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..18459b1 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# GORM 示例代码 \ No newline at end of file diff --git a/basic.go b/basic.go new file mode 100644 index 0000000..a404dfb --- /dev/null +++ b/basic.go @@ -0,0 +1,125 @@ +package gormExample + +import ( + "fmt" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "log" + "time" +) + +// 创建模型 +type Article struct { + // 嵌入基础模型 + gorm.Model + // 定义字段 + Subject string + Likes uint + Published bool + PublishTime time.Time + AuthorID uint +} + +func BasicUsage() { + // 定义DSN + dsn := "root:secret@tcp(127.0.0.1:3306)/gormExample?charset=utf8mb4&parseTime=True&loc=Local" + + // 连接服务器(池) + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + log.Fatal(err) + } + + // Open成功 + //fmt.Println(db) + + // 基于模型完成表结构(设计)的迁移(定义) + if err := db.AutoMigrate(&Article{}); err != nil { + log.Fatal(err) + } + fmt.Println("migrate success") +} + +var DB *gorm.DB + +func init() { + // 定义DSN + const dsn = "root:secret@tcp(127.0.0.1:3306)/gormExample?charset=utf8mb4&parseTime=True&loc=Local" + + // 连接服务器(池) + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + log.Fatal(err) + } + + DB = db +} + +func Create() { + // 构建Article类型数据 + article := &Article{ + Subject: "GORM 的 CRUD 基础操作", + Likes: 0, + Published: true, + PublishTime: time.Now(), + AuthorID: 42, + } + + // DB.Create 完成数据库的insert + if err := DB.Create(article).Error; err != nil { + log.Fatal(err) + } + + // print + fmt.Println(article) +} + +func Retrieve(id uint) { + // 初始化Article模型,零值 + article := &Article{} + + // DB.First() + if err := DB.First(article, id).Error; err != nil { + log.Fatal(err) + } + + // print + fmt.Println(article) +} + +func Update() { + // 获取需要更新的对象 + article := &Article{} + if err := DB.First(article, 1).Error; err != nil { + log.Fatal(err) + } + + // 更新对象字段 + article.AuthorID = 23 + article.Likes = 101 + article.Subject = "新的文章标题" + + // 存储,DB.Save() + if err := DB.Save(article).Error; err != nil { + log.Fatal(err) + } + + // print + fmt.Println(article) +} + +func Delete() { + // 获取模型对象 + article := &Article{} + if err := DB.First(article, 1).Error; err != nil { + log.Fatal(err) + } + + // DB.Delete() 删除 + if err := DB.Delete(article).Error; err != nil { + log.Fatal(err) + } + + // print + fmt.Println("article was deleted") +} diff --git a/basic_test.go b/basic_test.go new file mode 100644 index 0000000..a5a901b --- /dev/null +++ b/basic_test.go @@ -0,0 +1,23 @@ +package gormExample + +import "testing" + +func TestBasicUsage(t *testing.T) { + BasicUsage() +} + +func TestCreate(t *testing.T) { + Create() +} + +func TestRetrieve(t *testing.T) { + Retrieve(1) +} + +func TestUpdate(t *testing.T) { + Update() +} + +func TestDelete(t *testing.T) { + Delete() +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e9c5400 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + db: + container_name: gormExampleMySQL + image: mysql + environment: + MYSQL_ROOT_PASSWORD: secret + MYSQL_DATABASE: gormExample + ports: + - "3306:3306" + volumes: + - ./mysql/data:/var/lib/mysql diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dff5e1b --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module gormExample + +go 1.20 + +require ( + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + gorm.io/driver/mysql v1.4.7 // indirect + gorm.io/gorm v1.24.6 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..56268ac --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +gorm.io/driver/mysql v1.4.7 h1:rY46lkCspzGHn7+IYsNpSfEv9tA+SU4SkkB+GFX125Y= +gorm.io/driver/mysql v1.4.7/go.mod h1:SxzItlnT1cb6e1e4ZRpgJN2VYtcqJgqnHxWr4wsP8oc= +gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.24.6 h1:wy98aq9oFEetsc4CAbKD2SoBCdMzsbSIvSUUFJuHi5s= +gorm.io/gorm v1.24.6/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=