diff --git a/blog/golang/golang-gorm.md b/blog/golang/golang-gorm.md new file mode 100644 index 0000000000000000000000000000000000000000..97eeb7dd85bbb99bb0931828a385b800762c82de --- /dev/null +++ b/blog/golang/golang-gorm.md @@ -0,0 +1,63 @@ +## Golang ORM库 gorm + +ORM:对象关系映射 Object Relational Mapping + +文档: + +- [https://gorm.io/zh_CN/docs/](https://gorm.io/zh_CN/docs/) + +安装 +``` +go get gorm.io/gorm + +go get gorm.io/driver/mysql +``` + +```go +package main + +import ( + "fmt" + + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +type Product struct { + gorm.Model + Code string + Price uint +} + +func main() { + dsn := "root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=True&loc=Local" + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + panic("failed to connect database") + } + + // 迁移 schema + db.AutoMigrate(&Product{}) + + // Create + db.Create(&Product{Code: "D42", Price: 100}) + + // Read + var product Product + db.First(&product, 1) // 根据整型主键查找 + fmt.Printf("product: %v\n", product) + + db.First(&product, "code = ?", "D42") // 查找 code 字段值为 D42 的记录 + fmt.Printf("product: %v\n", product) + + // Update - 将 product 的 price 更新为 200 + db.Model(&product).Update("Price", 200) + + // Update - 更新多个字段 + db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // 仅更新非零值字段 + db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"}) + + // Delete - 软删除 product + db.Delete(&product, 1) +} +``` \ No newline at end of file diff --git a/blog/golang/index.md b/blog/golang/index.md index 28d0b4dd9909fcddb5c475a462a7967dbdc9159d..1f7c6da30f0dc8b9f23f46407a364c715dc0eb3e 100644 --- a/blog/golang/index.md +++ b/blog/golang/index.md @@ -107,6 +107,8 @@ - [Golang操作MongoDB数据库](/blog/golang/golang-mongo.md) +- [Golang ORM库 gorm](/blog/golang/golang-gorm.md) + @@ -114,4 +116,4 @@ https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_sourc -https://www.bilibili.com/video/BV1zR4y1t7Wj?p=112&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da \ No newline at end of file +https://www.bilibili.com/video/BV1zR4y1t7Wj?p=113&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da \ No newline at end of file diff --git a/weekly/20221017.md b/weekly/1.md similarity index 61% rename from weekly/20221017.md rename to weekly/1.md index 1a42f169d80949c6ff57ae744170bad0a9d0baeb..72247fcb02b74500cdf940e3468db4b77be16fb7 100644 --- a/weekly/20221017.md +++ b/weekly/1.md @@ -1,4 +1,4 @@ -# 全栈爱好者技术周刊|20221017 +# 全栈爱好者技术周刊|第1期 ## 前端 @@ -24,4 +24,13 @@ Tauri:下一代桌面应用开发框架? 33 个 "不得不看" 的 Python 关键字总结! -- https://mp.weixin.qq.com/s/yMNyprGNMf4QEjYdG6wAbg \ No newline at end of file +- https://mp.weixin.qq.com/s/yMNyprGNMf4QEjYdG6wAbg + +SpringBoot+Vue实现微信扫码支付、退款功能 +- https://mp.weixin.qq.com/s/zH7zQI99smXyzxhbMeLONA + +Domain Admin:监测域名SSL证书,到期邮件提醒 +- https://mp.weixin.qq.com/s/batUjnNscGqYS5x5Vbn_Vw + +一文读懂字符编码 +- https://mp.weixin.qq.com/s/5pAgcjk_lFGrPhSUp2Na3Q \ No newline at end of file diff --git a/weekly/index.md b/weekly/index.md index 905756670e08e73abdf33849920c70394983908e..b09c768ee80efb9faa48e9ebdc76ebe281c2ed5a 100644 --- a/weekly/index.md +++ b/weekly/index.md @@ -1,5 +1,5 @@ # 全栈爱好者周刊 -[全栈爱好者周刊|20221017](/weekly/20221017.md) +[全栈爱好者周刊|20221017](/weekly/1.md)