From 31b672c5e9a91d27ca85a14986fd7e9e3ff3bf4c Mon Sep 17 00:00:00 2001 From: pengshiyu <1940607002@qq.com> Date: Thu, 13 Oct 2022 22:48:41 +0800 Subject: [PATCH] fix --- blog/golang/golang-mongo.md | 177 ++++++++++++++++++++++++++++++++++++ blog/golang/golang-mysql.md | 154 +++++++++++++++++++++++++++++++ blog/golang/index.md | 12 ++- weekly/20221017.md | 21 +++++ weekly/index.md | 5 + 5 files changed, 365 insertions(+), 4 deletions(-) create mode 100644 blog/golang/golang-mongo.md create mode 100644 weekly/20221017.md create mode 100644 weekly/index.md diff --git a/blog/golang/golang-mongo.md b/blog/golang/golang-mongo.md new file mode 100644 index 0000000..a612d58 --- /dev/null +++ b/blog/golang/golang-mongo.md @@ -0,0 +1,177 @@ +# Golang操作MongoDB数据库 + +## 下载安装MongoDB + +https://www.mongodb.com/try/download/community2 + +https://www.mongodb.com/download-center/community/releases + +连接客户端 + +```bash +# 打开客户端 +mongo + +# 创建数据库 +use go_db; + +# 创建集合 +db.createCollection('student'); +``` + +## 下载驱动 + +https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo + +``` +go get go.mongodb.org/mongo-driver/mongo +``` + +## 连接到mongodb数据库 + +```go +package main + +import ( + "context" + "log" + + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func main() { + // 设置客户端连接配置 + // db_url := "mongodb://root:123456@localhost:27017" + db_url := "mongodb://localhost:27017" + clientOptions := options.Client().ApplyURI(db_url) + + // 连接到mongo + client, _ := mongo.Connect(context.TODO(), clientOptions) + + // 连接检查 + err := client.Ping(context.TODO(), nil) + + if err != nil { + log.Fatal(err) + } +} +``` + +## BSON + +二进制编码的json + +类型D家族 +- D BSON文档 +- M 无序map +- A BSON数组 +- E D里面的一个元素 + +示例 +```go +package main + +import ( + "fmt" + + "go.mongodb.org/mongo-driver/bson" +) + +func main() { + d := bson.D{{"name", "age"}} + fmt.Printf("d: %v\n", d) + // d: [{name age}] +} + +``` + +## 添加文档 + +插入单条记录 + +```go +package main + +import ( + "context" + "fmt" + + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +type Student struct { + Name string + Age int +} + +func main() { + // 连接到mongo + db_url := "mongodb://localhost:27017" + clientOptions := options.Client().ApplyURI(db_url) + client, _ := mongo.Connect(context.TODO(), clientOptions) + + student := Student{ + Name: "Tom", + Age: 23, + } + + collection := client.Database("go_db").Collection("student") + + ior, _ := collection.InsertOne(context.TODO(), student) + fmt.Printf("ior.InsertedID: %v\n", ior.InsertedID) + // ior.InsertedID: ObjectID("634822c35881b85ab2aa138e") + +} + +``` + +插入多条记录 + +```go +package main + +import ( + "context" + "fmt" + + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +type Student struct { + Name string + Age int +} + +func main() { + // 连接到mongo + db_url := "mongodb://localhost:27017" + clientOptions := options.Client().ApplyURI(db_url) + client, _ := mongo.Connect(context.TODO(), clientOptions) + + student1 := Student{ + Name: "Steve", + Age: 24, + } + + student2 := Student{ + Name: "Jack", + Age: 25, + } + + students := []interface{}{student1, student2} + + collection := client.Database("go_db").Collection("student") + + imr, _ := collection.InsertMany(context.TODO(), students) + fmt.Printf("imr.InsertedIDs: %v\n", imr.InsertedIDs) + // imr.InsertedIDs: [ + // ObjectID("634823e52758c71e01e1e78f") + // ObjectID("634823e52758c71e01e1e790") + // ] + +} + +``` \ No newline at end of file diff --git a/blog/golang/golang-mysql.md b/blog/golang/golang-mysql.md index cf1e2a2..8c014ba 100644 --- a/blog/golang/golang-mysql.md +++ b/blog/golang/golang-mysql.md @@ -138,3 +138,157 @@ func main() { ``` + +## 查询操作 + +单行查询 + +```go +package main + +import ( + "database/sql" + "fmt" + + _ "github.com/go-sql-driver/mysql" +) + +type User struct { + id int + username string + password string +} + +func main() { + db_url := "root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=true" + + db, _ := sql.Open("mysql", db_url) + defer db.Close() + + // 查询单条数据 + var user User + + sql := "select * from user_tbl where id = ?" + + db.QueryRow(sql, 1).Scan(&user.id, &user.username, &user.password) + fmt.Printf("row: %v\n", user) + // row: {1 Tom 123456} +} + +``` + +查询多条数据 + +```go +package main + +import ( + "database/sql" + "fmt" + + _ "github.com/go-sql-driver/mysql" +) + +type User struct { + id int + username string + password string +} + +func main() { + db_url := "root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=true" + + db, _ := sql.Open("mysql", db_url) + defer db.Close() + + sql := "select * from user_tbl" + + rows, _ := db.Query(sql) + defer rows.Close() + + for rows.Next() { + var user User + + rows.Scan(&user.id, &user.username, &user.password) + fmt.Printf("row: %v\n", user) + // row: {1 Tom 123456} + // row: {2 Kite abcdef} + // row: {3 Jack jjyy} + } + +} + +``` + +## 更新操作 + +```go +package main + +import ( + "database/sql" + "fmt" + + _ "github.com/go-sql-driver/mysql" +) + +type User struct { + id int + username string + password string +} + +func main() { + db_url := "root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=true" + + db, _ := sql.Open("mysql", db_url) + defer db.Close() + + // 更新数据 + sql := "update user_tbl set username = ? where id = ?" + result, _ := db.Exec(sql, "Tom-1", 1) + + // 影响行数 + i, _ := result.RowsAffected() + fmt.Printf("i: %v\n", i) + // i: 1 +} + +``` + +## 删除数据 + +```go +package main + +import ( + "database/sql" + "fmt" + + _ "github.com/go-sql-driver/mysql" +) + +type User struct { + id int + username string + password string +} + +func main() { + db_url := "root:123456@tcp(127.0.0.1:3306)/go_db?charset=utf8mb4&parseTime=true" + + db, _ := sql.Open("mysql", db_url) + defer db.Close() + + // 删除数据 + sql := "delete from user_tbl where id = ?" + result, _ := db.Exec(sql, 3) + + // 影响行数 + i, _ := result.RowsAffected() + fmt.Printf("i: %v\n", i) + // i: 1 +} + +``` + diff --git a/blog/golang/index.md b/blog/golang/index.md index ff423e6..29c8e96 100644 --- a/blog/golang/index.md +++ b/blog/golang/index.md @@ -100,13 +100,17 @@ - [5.13、Golang标准库-math](/blog/golang/golang-standard-math.md) -[Golang操作MySQL数据库](/blog/golang/golang-mysql.md) -第六章 MySQL数据库 +### 第六章 Golang操作数据库 + +- [Golang操作MySQL数据库](/blog/golang/golang-mysql.md) + +- [Golang操作MongoDB数据库](/blog/golang/golang-mongo.md) -https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da +https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da + -https://www.bilibili.com/video/BV1zR4y1t7Wj/?p=103&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da \ No newline at end of file +https://www.bilibili.com/video/BV1zR4y1t7Wj/?p=109&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da \ No newline at end of file diff --git a/weekly/20221017.md b/weekly/20221017.md new file mode 100644 index 0000000..5eab1bb --- /dev/null +++ b/weekly/20221017.md @@ -0,0 +1,21 @@ +# 全栈爱好者周刊|20221017 + +## 前端 + +30 个 Javascript 知识点总结,总有你不会的! + +- https://mp.weixin.qq.com/s/_UTPeZiLQIeFpTycMQXz2g +- https://juejin.cn/post/7145036326373425159 + +现代 CSS 颜色指南 + +- https://mp.weixin.qq.com/s/ZnfbAvjRGUz0f-oUomKd1g + +教科书级图解 CSS Grid 布局,收藏了当字典用 +- https://mp.weixin.qq.com/s/WNvT3TO6HmlNSEorHwuB4Q + + +## 后端 + +万字长文:带你走进shell世界 +- https://mp.weixin.qq.com/s/gtIWO3ItxyLdQeVorXtShQ diff --git a/weekly/index.md b/weekly/index.md new file mode 100644 index 0000000..9057566 --- /dev/null +++ b/weekly/index.md @@ -0,0 +1,5 @@ +# 全栈爱好者周刊 + +[全栈爱好者周刊|20221017](/weekly/20221017.md) + + -- GitLab