first commit

main
han-joker 2 years ago
commit 7165f7f2e5

42
.gitignore vendored

@ -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/

@ -0,0 +1 @@
# GORM 示例代码

@ -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")
}

@ -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()
}

@ -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

@ -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
)

@ -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=
Loading…
Cancel
Save