欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Golang中SQLX庫(kù)的高級(jí)操作

 更新時(shí)間:2023年06月15日 10:23:10   作者:尋月隱君  
sqlx是Golang中的一個(gè)知名三方庫(kù),其為Go標(biāo)準(zhǔn)庫(kù)database/sql提供了一組擴(kuò)展支持,下面就來(lái)和大家分享一下SQLX庫(kù)的高級(jí)操作吧,希望對(duì)大家有所幫助

sqlx.In 介紹

sqlx is a package for Go which provides a set of extensions on top of the excellent built-in database/sql package.

Illustrated guide to SQLX:jmoiron.github.io/sqlx/

sqlx:github.com/jmoiron/sqlx

"In" Queries

Because database/sql does not inspect your query and it passes your arguments directly to the driver, it makes dealing with queries with IN clauses difficult:

SELECT * FROM users WHERE level IN (?);

When this gets prepared as a statement on the backend, the bindvar ? will only correspond to a single argument, but what is often desired is for that to be a variable number of arguments depending on the length of some slice, eg:

var levels = []int{4, 6, 7}rows, err := db.Query("SELECT * FROM users WHERE level IN (?);", levels)

This pattern is possible by first processing the query with sqlx.In:

var levels = []int{4, 6, 7}query, args, err := sqlx.In("SELECT * FROM users WHERE level IN (?);", levels) // sqlx.In returns queries with the `?` bindvar, we can rebind it for our backendquery = db.Rebind(query)rows, err := db.Query(query, args...)

What sqlx.In does is expand any bindvars in the query passed to it that correspond to a slice in the arguments to the length of that slice, and then append those slice elements to a new arglist. It does this with the ? bindvar only; you can use db.Rebind to get a query suitable for your backend.

普通批量插入數(shù)據(jù),不使用 sqlx.In

package main
?
import (
  "database/sql"
  "fmt"
  "strings"
  "time"
?
  _ "github.com/go-sql-driver/mysql" // 匿名導(dǎo)入 自動(dòng)執(zhí)行 init()
)
?
var db *sql.DB
?
func initMySQL() (err error) {
  //DSN (Data Source Name)
  dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test"
  // 注意:要初始化全局的 db 對(duì)象,不要新聲明一個(gè) db 變量
  db, err = sql.Open("mysql", dsn) // 只對(duì)格式進(jìn)行校驗(yàn),并不會(huì)真正連接數(shù)據(jù)庫(kù)
  if err != nil {
    return err
  }
?
  // Ping 驗(yàn)證與數(shù)據(jù)庫(kù)的連接是否仍處于活動(dòng)狀態(tài),并在必要時(shí)建立連接。
  err = db.Ping()
  if err != nil {
    fmt.Printf("connect to db failed, err: %v\n", err)
    return err
  }
  // 數(shù)值需要根據(jù)業(yè)務(wù)具體情況來(lái)確定
  db.SetConnMaxLifetime(time.Second * 10) // 設(shè)置可以重用連接的最長(zhǎng)時(shí)間
  db.SetConnMaxIdleTime(time.Second * 5) ?// 設(shè)置連接可能處于空閑狀態(tài)的最長(zhǎng)時(shí)間
  db.SetMaxOpenConns(200) ? ? ? ? ? ? ? ? // 設(shè)置與數(shù)據(jù)庫(kù)的最大打開連接數(shù)
  db.SetMaxIdleConns(10) ? ? ? ? ? ? ? ? ?//  設(shè)置空閑連接池中的最大連接數(shù)
  return nil
}
?
type User struct {
  Name string `db:"name"`
  Age ?int ? ?`db:"age"`
}
?
// BatchInsertUsers 批量插入數(shù)據(jù)
func BatchInsertUsers(users []*User) error {
  valueStrings := make([]string, 0, len(users)) ? ? // 占位符 slice
  valueArgs := make([]interface{}, 0, len(users)*2) // 插入值 slice
?
  for _, u := range users {
    valueStrings = append(valueStrings, "(?, ?)")
    valueArgs = append(valueArgs, u.Name, u.Age) // 占位符與插入值 一一對(duì)應(yīng)
  }
  // 拼接完整的SQL語(yǔ)句
  // Sprintf根據(jù)格式說(shuō)明符進(jìn)行格式化,并返回結(jié)果字符串。
  // Join將其第一個(gè)參數(shù)的元素連接起來(lái)以創(chuàng)建單個(gè)字符串。分隔字符串sep放置在結(jié)果字符串的元素之間。
  stmt := fmt.Sprintf("INSERT INTO user (name, age) VALUES %s", strings.Join(valueStrings, ","))
  // Exec執(zhí)行查詢而不返回任何行。參數(shù)用于查詢中的任何占位符參數(shù)。
  result, err := db.Exec(stmt, valueArgs...)
  if err != nil {
    fmt.Printf("Error inserting user into database: %v \n", err)
    return err
  }
  var rows_affected int64
  rows_affected, err = result.RowsAffected() // 返回受更新、插入或刪除影響的行數(shù)。并非每個(gè)數(shù)據(jù)庫(kù)或數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序都支持此功能。
  if err != nil {
    fmt.Printf("返回受更新、插入或刪除影響的行數(shù) failed, err: %v\n", err)
    return err
  }
  fmt.Println("受更新、插入或刪除影響的行數(shù): ", rows_affected)
  return nil
}
?
func main() {
  if err := initMySQL(); err != nil {
    fmt.Printf("connect to db failed, err: %v\n", err)
  }
  // 檢查完錯(cuò)誤之后執(zhí)行,確保 db 不為 nil
  // Close() 用來(lái)釋放數(shù)據(jù)庫(kù)連接相關(guān)的資源
  // Close 將關(guān)閉數(shù)據(jù)庫(kù)并阻止啟動(dòng)新查詢。關(guān)閉,然后等待服務(wù)器上已開始處理的所有查詢完成。
  defer db.Close()
?
  fmt.Println("connect to database success")
  // db.xx() 去使用數(shù)據(jù)庫(kù)操作...
?
  // 批量插入數(shù)據(jù)
  users := []*User{
    {Name: "劉備", Age: 25},
    {Name: "關(guān)羽", Age: 30},
    {Name: "張飛", Age: 28},
  }
  err := BatchInsertUsers(users)
  if err != nil {
    fmt.Printf("Failed to batch insert users: %v", err)
  }
}
?

運(yùn)行

Code/go/mysql_demo via ?? v1.20.3 via ?? base 
? go run  main.go
connect to database success
受更新、插入或刪除影響的行數(shù):  3
?
Code/go/mysql_demo via ?? v1.20.3 via ?? base 

SQL 查詢結(jié)果

mysql> select * from user;  # 插入之前
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小喬   |   12 |
|  2 | 小喬   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
+----+--------+------+
11 rows in set (0.00 sec)
?
mysql> select * from user;  # 插入之后
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小喬   |   12 |
|  2 | 小喬   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 劉備   |   25 |
| 16 | 關(guān)羽   |   30 |
| 17 | 張飛   |   28 |
+----+--------+------+
14 rows in set (0.01 sec)

使用 sqlx.In 批量插入

package main
?
import (
  "database/sql/driver"
  "fmt"
  _ "github.com/go-sql-driver/mysql"
  "github.com/jmoiron/sqlx"
)
?
var db *sqlx.DB
?
func initDB() (err error) {
  dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"
  // 連接到數(shù)據(jù)庫(kù)并使用ping進(jìn)行驗(yàn)證。
  // 也可以使用 MustConnect MustConnect連接到數(shù)據(jù)庫(kù),并在出現(xiàn)錯(cuò)誤時(shí)恐慌 panic。
  db, err = sqlx.Connect("mysql", dsn)
  if err != nil {
    fmt.Printf("connect DB failed, err:%v\n", err)
    return
  }
  db.SetMaxOpenConns(20) // 設(shè)置數(shù)據(jù)庫(kù)的最大打開連接數(shù)。
  db.SetMaxIdleConns(10) // 設(shè)置空閑連接池中的最大連接數(shù)。
  return
}
?
type user struct {
  ID ? int ? ?`db:"id"`
  Age ?int ? ?`db:"age"`
  Name string `db:"name"`
}
?
func (u user) Value() (driver.Value, error) {
  return []interface{}{u.Name, u.Age}, nil
}
?
// BatchInsertUsersSqlxIn 使用sqlx.In幫我們拼接語(yǔ)句和參數(shù), 注意傳入的參數(shù)是[]interface{}
func BatchInsertUsersSqlxIn(users []interface{}) error {
  // In展開args中的切片值,返回修改后的查詢字符串和一個(gè)可以由數(shù)據(jù)庫(kù)執(zhí)行的新的arg列表。
  // “查詢”應(yīng)該使用“?”“bindVar。返回值使用' ?“bindVar。
  query, args, _ := sqlx.In(
    "INSERT INTO user (name, age) VALUES (?), (?), (?)",
    users..., // 如果arg實(shí)現(xiàn)了 driver.Valuer, sqlx.In 會(huì)通過(guò)調(diào)用 Value()來(lái)展開它
  )
  fmt.Println("query sql string: ", query) // 查看生成的querystring
  fmt.Println("args: ", args) ? ? ? ? ? ? ?// 查看生成的args
  // Exec執(zhí)行查詢而不返回任何行。參數(shù)用于查詢中的任何占位符參數(shù)。
  result, err := db.Exec(query, args...)
  var rows_affected int64
  rows_affected, err = result.RowsAffected() // 返回受更新、插入或刪除影響的行數(shù)。并非每個(gè)數(shù)據(jù)庫(kù)或數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序都支持此功能。
  if err != nil {
    fmt.Printf("返回受更新、插入或刪除影響的行數(shù) failed, err: %v\n", err)
    return err
  }
  fmt.Println("受更新、插入或刪除影響的行數(shù): ", rows_affected)
  return nil
}
?
?
func main() {
  if err := initDB(); err != nil {
    fmt.Printf("init DB failed, err:%v\n", err)
    return
  }
  fmt.Println("init DB succeeded")
  // 批量插入
  u1 := user{Name: "李白", Age: 16}
  u2 := user{Name: "杜甫", Age: 42}
  u3 := user{Name: "王維", Age: 29}
  users := []interface{}{u1, u2, u3}
  _ = BatchInsertUsersSqlxIn(users)
}
?

運(yùn)行

Code/go/sqlx_demo via ?? v1.20.3 via ?? base 
? go run main.go
init DB succeeded
query sql string:  INSERT INTO user (name, age) VALUES (?, ?), (?, ?), (?, ?)
args:  [李白 16 杜甫 42 王維 29]
受更新、插入或刪除影響的行數(shù):  3
?
Code/go/sqlx_demo via ?? v1.20.3 via ?? base 

SQL 查詢結(jié)果

mysql> select * from user;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小喬   |   12 |
|  2 | 小喬   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 劉備   |   25 |
| 16 | 關(guān)羽   |   30 |
| 17 | 張飛   |   28 |
+----+--------+------+
14 rows in set (0.01 sec)
?
mysql> select * from user;  # 插入之后
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小喬   |   12 |
|  2 | 小喬   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 劉備   |   25 |
| 16 | 關(guān)羽   |   30 |
| 17 | 張飛   |   28 |
| 18 | 李白   |   16 |
| 19 | 杜甫   |   42 |
| 20 | 王維   |   29 |
+----+--------+------+
17 rows in set (0.00 sec)
?
mysql>

使用 NamedExec 批量插入

package main
?
import (
  "database/sql/driver"
  "fmt"
  _ "github.com/go-sql-driver/mysql"
  "github.com/jmoiron/sqlx"
)
?
var db *sqlx.DB
?
func initDB() (err error) {
  dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"
  // 連接到數(shù)據(jù)庫(kù)并使用ping進(jìn)行驗(yàn)證。
  // 也可以使用 MustConnect MustConnect連接到數(shù)據(jù)庫(kù),并在出現(xiàn)錯(cuò)誤時(shí)恐慌 panic。
  db, err = sqlx.Connect("mysql", dsn)
  if err != nil {
    fmt.Printf("connect DB failed, err:%v\n", err)
    return
  }
  db.SetMaxOpenConns(20) // 設(shè)置數(shù)據(jù)庫(kù)的最大打開連接數(shù)。
  db.SetMaxIdleConns(10) // 設(shè)置空閑連接池中的最大連接數(shù)。
  return
}
?
type user struct {
  ID ? int ? ?`db:"id"`
  Age ?int ? ?`db:"age"`
  Name string `db:"name"`
}
?
func (u user) Value() (driver.Value, error) {
  return []interface{}{u.Name, u.Age}, nil
}
?
?
// BatchInsertUsersNamedExec NamedExec 批量插入
func BatchInsertUsersNamedExec(users []*user) error {
  // 任何命名的占位符參數(shù)都將被arg中的字段替換。
  result, err := db.NamedExec("INSERT INTO user (name, age) VALUES (:name, :age)", users)
  var rows_affected int64
  rows_affected, err = result.RowsAffected() // 返回受更新、插入或刪除影響的行數(shù)。并非每個(gè)數(shù)據(jù)庫(kù)或數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序都支持此功能。
  if err != nil {
    fmt.Printf("返回受更新、插入或刪除影響的行數(shù) failed, err: %v\n", err)
    return err
  }
  fmt.Println("BatchInsertUsersNamedExec 受插入影響的行數(shù): ", rows_affected)
  return nil
}
?
func main() {
  if err := initDB(); err != nil {
    fmt.Printf("init DB failed, err:%v\n", err)
    return
  }
  fmt.Println("init DB succeeded")
  // 批量插入
  u1 := user{Name: "褒姒", Age: 16}
  u2 := user{Name: "貂蟬", Age: 42}
  u3 := user{Name: "飛燕", Age: 29}
  // NamedExec
  users := []*user{&u1, &u2, &u3}
  _ = BatchInsertUsersNamedExec(users)
}
?

運(yùn)行

Code/go/sqlx_demo via ?? v1.20.3 via ?? base 
? go run main.go
init DB succeeded
BatchInsertUsersNamedExec 受插入影響的行數(shù):  3
?
Code/go/sqlx_demo via ?? v1.20.3 via ?? base 

SQL 查詢結(jié)果

mysql> select * from user;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小喬   |   12 |
|  2 | 小喬   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 劉備   |   25 |
| 16 | 關(guān)羽   |   30 |
| 17 | 張飛   |   28 |
| 18 | 李白   |   16 |
| 19 | 杜甫   |   42 |
| 20 | 王維   |   29 |
+----+--------+------+
17 rows in set (0.00 sec)
?
mysql> select * from user;  # 插入之后
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小喬   |   12 |
|  2 | 小喬   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 劉備   |   25 |
| 16 | 關(guān)羽   |   30 |
| 17 | 張飛   |   28 |
| 18 | 李白   |   16 |
| 19 | 杜甫   |   42 |
| 20 | 王維   |   29 |
| 21 | 褒姒   |   16 |
| 22 | 貂蟬   |   42 |
| 23 | 飛燕   |   29 |
+----+--------+------+
20 rows in set (0.00 sec)
?
mysql>

sqlx 高級(jí)操作之 IN 查詢

查詢 ID 在指定集合中的數(shù)據(jù)

package main
?
import (
  "database/sql/driver"
  "fmt"
  _ "github.com/go-sql-driver/mysql"
  "github.com/jmoiron/sqlx"
)
?
var db *sqlx.DB
?
func initDB() (err error) {
  dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"
  // 連接到數(shù)據(jù)庫(kù)并使用ping進(jìn)行驗(yàn)證。
  // 也可以使用 MustConnect MustConnect連接到數(shù)據(jù)庫(kù),并在出現(xiàn)錯(cuò)誤時(shí)恐慌 panic。
  db, err = sqlx.Connect("mysql", dsn)
  if err != nil {
    fmt.Printf("connect DB failed, err:%v\n", err)
    return
  }
  db.SetMaxOpenConns(20) // 設(shè)置數(shù)據(jù)庫(kù)的最大打開連接數(shù)。
  db.SetMaxIdleConns(10) // 設(shè)置空閑連接池中的最大連接數(shù)。
  return
}
?
type user struct {
  ID ? int ? ?`db:"id"`
  Age ?int ? ?`db:"age"`
  Name string `db:"name"`
}
?
// QueryByIDs 查詢 ID 在指定集合中的數(shù)據(jù)
func QueryByIDs(ids []int) (users []user, err error) {
  // In 展開args中的切片值,返回修改后的查詢字符串和一個(gè)可以由數(shù)據(jù)庫(kù)執(zhí)行的新的arg列表。
  // “查詢”應(yīng)該使用“?”“bindVar。返回值使用' ?“bindVar
  query, args, err := sqlx.In("SELECT name, age FROM user WHERE id IN (?)", ids)
  if err != nil {
    return nil, err
  }
  // Rebind 將查詢從 QUESTION 轉(zhuǎn)換為DB驅(qū)動(dòng)程序的 bindvar 類型。
  query = db.Rebind(query)
  // Select 使用此數(shù)據(jù)庫(kù)。任何占位符參數(shù)都將被提供的參數(shù)替換。
  err = db.Select(&users, query, args...)
  if err != nil {
    return nil, err
  }
  return users, nil
}
?
func main() {
  if err := initDB(); err != nil {
    fmt.Printf("init DB failed, err:%v\n", err)
    return
  }
  fmt.Println("init DB succeeded")
  // IN 查詢
  users, err := QueryByIDs([]int{1, 15, 21, 2}) ?// 默認(rèn)按照主鍵順序排列
  if err != nil {
    fmt.Printf("query error: %v\n", err)
    return
  }
  fmt.Printf("query successful result users %v\n", users)
  for _, user := range users {
    fmt.Printf("user: %#v\n", user)
  }
}
?

運(yùn)行

Code/go/sqlx_demo via ?? v1.20.3 via ?? base 
? go run main.go
init DB succeeded
query successful result users [{0 12 小喬} {0 22 小喬} {0 25 劉備} {0 16 褒姒}]
user: main.user{ID:0, Age:12, Name:"小喬"}
user: main.user{ID:0, Age:22, Name:"小喬"}
user: main.user{ID:0, Age:25, Name:"劉備"}
user: main.user{ID:0, Age:16, Name:"褒姒"}
?
Code/go/sqlx_demo via ?? v1.20.3 via ?? base 

SQL 查詢結(jié)果

mysql> select * from user;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 小喬   |   12 |
|  2 | 小喬   |   22 |
|  5 | 昭君   |   18 |
|  6 | 黛玉   |   16 |
|  8 | 李煜   |   26 |
|  9 | Alice  |   25 |
| 10 | Bob    |   30 |
| 11 | Carol  |   28 |
| 12 | Alice1 |   25 |
| 13 | Bob1   |   30 |
| 14 | Carol1 |   28 |
| 15 | 劉備   |   25 |
| 16 | 關(guān)羽   |   30 |
| 17 | 張飛   |   28 |
| 18 | 李白   |   16 |
| 19 | 杜甫   |   42 |
| 20 | 王維   |   29 |
| 21 | 褒姒   |   16 |
| 22 | 貂蟬   |   42 |
| 23 | 飛燕   |   29 |
+----+--------+------+
20 rows in set (0.00 sec)
?
mysql>

查詢結(jié)果默認(rèn)按照主鍵順序排列

自定義查詢結(jié)果順序

  • 使用代碼排序
  • 使用 MySQL 排序 FIND_IN_SET

sqlx 高級(jí)操作之 FIND_IN_SET

package main
?
import (
  "database/sql/driver"
  "fmt"
  _ "github.com/go-sql-driver/mysql"
  "github.com/jmoiron/sqlx"
  "strings"
)
?
var db *sqlx.DB
?
func initDB() (err error) {
  dsn := "root:12345678@tcp(127.0.0.1:3306)/sql_test?charset=utf8mb4&parseTime=True"
  // 連接到數(shù)據(jù)庫(kù)并使用ping進(jìn)行驗(yàn)證。
  // 也可以使用 MustConnect MustConnect連接到數(shù)據(jù)庫(kù),并在出現(xiàn)錯(cuò)誤時(shí)恐慌 panic。
  db, err = sqlx.Connect("mysql", dsn)
  if err != nil {
    fmt.Printf("connect DB failed, err:%v\n", err)
    return
  }
  db.SetMaxOpenConns(20) // 設(shè)置數(shù)據(jù)庫(kù)的最大打開連接數(shù)。
  db.SetMaxIdleConns(10) // 設(shè)置空閑連接池中的最大連接數(shù)。
  return
}
?
type user struct {
  ID ? int ? ?`db:"id"`
  Age ?int ? ?`db:"age"`
  Name string `db:"name"`
}
?
// QueryByIDs  查詢 ID 在指定集合中的數(shù)據(jù)
func QueryByIDs(ids []int) (users []user, err error) {
  // In 展開args中的切片值,返回修改后的查詢字符串和一個(gè)可以由數(shù)據(jù)庫(kù)執(zhí)行的新的arg列表。
  // “查詢”應(yīng)該使用“?”“bindVar。返回值使用' ?“bindVar
  query, args, err := sqlx.In("SELECT name, age FROM user WHERE id IN (?)", ids)
  if err != nil {
    return nil, err
  }
  // Rebind 將查詢從 QUESTION 轉(zhuǎn)換為DB驅(qū)動(dòng)程序的 bindvar 類型。
  query = db.Rebind(query)
  // Select 使用此數(shù)據(jù)庫(kù)。任何占位符參數(shù)都將被提供的參數(shù)替換。
  err = db.Select(&users, query, args...)
  if err != nil {
    return nil, err
  }
  return users, nil
}
?
// QueryAndOrderByIDs 根據(jù) ID 在指定集合中和指定順序查詢
func QueryAndOrderByIDs(ids []int) (users []user, err error) {
  // 創(chuàng)建一個(gè)字符串切片,大小為ids的長(zhǎng)度
  strIDs := make([]string, 0, len(ids))
  // 將ids轉(zhuǎn)換為字符串類型
  for _, id := range ids {
    // Sprintf根據(jù)格式說(shuō)明符進(jìn)行格式化,并返回結(jié)果字符串。
    strIDs = append(strIDs, fmt.Sprintf("%d", id))
  }
  // In展開args中的切片值,返回修改后的查詢字符串和一個(gè)可以由數(shù)據(jù)庫(kù)執(zhí)行的新的arg列表。“查詢”應(yīng)該使用“?”“bindVar。返回值使用' ?“bindVar。
  query, args, err := sqlx.In("SELECT name, age FROM user WHERE id IN (?) ORDER BY FIND_IN_SET(id, ?)", ids, strings.Join(strIDs, ","))
  if err != nil {
    return
  }
?
  // Rebind 將查詢從QUESTION轉(zhuǎn)換為DB驅(qū)動(dòng)程序的bindvar類型。
  query = db.Rebind(query)
  // 執(zhí)行查詢 Select 使用此數(shù)據(jù)庫(kù)。任何占位符參數(shù)都將被提供的參數(shù)替換。
  err = db.Select(&users, query, args...)
  return
}
?
func main() {
  if err := initDB(); err != nil {
    fmt.Printf("init DB failed, err:%v\n", err)
    return
  }
  fmt.Println("init DB succeeded")
  // IN 查詢
  users, err := QueryByIDs([]int{1, 15, 21, 2})
  if err != nil {
    fmt.Printf("query error: %v\n", err)
    return
  }
  fmt.Printf("query successful result users %v\n", users)
  for _, user := range users {
    fmt.Printf("user: %#v\n", user)
  }
?
  fmt.Println("**************")
  // FIND_IN_SET
  users, err = QueryAndOrderByIDs([]int{1, 15, 21, 2})
  if err != nil {
    fmt.Printf("query error: %v\n", err)
    return
  }
  fmt.Printf("query successful result users %v\n", users)
  for _, user := range users {
    fmt.Printf("user: %#v\n", user)
  }
}
?

運(yùn)行

Code/go/sqlx_demo via ?? v1.20.3 via ?? base 
? go run main.go
init DB succeeded
query successful result users [{0 12 小喬} {0 22 小喬} {0 25 劉備} {0 16 褒姒}]
user: main.user{ID:0, Age:12, Name:"小喬"}
user: main.user{ID:0, Age:22, Name:"小喬"}
user: main.user{ID:0, Age:25, Name:"劉備"}
user: main.user{ID:0, Age:16, Name:"褒姒"}
**************
query successful result users [{0 12 小喬} {0 25 劉備} {0 16 褒姒} {0 22 小喬}]
user: main.user{ID:0, Age:12, Name:"小喬"}  # FIND_IN_SET 按照指定順序查詢
user: main.user{ID:0, Age:25, Name:"劉備"}
user: main.user{ID:0, Age:16, Name:"褒姒"}
user: main.user{ID:0, Age:22, Name:"小喬"}
?
Code/go/sqlx_demo via ?? v1.20.3 via ?? base 

注意:開發(fā)中,使用代碼排序還是使用 SQL FIND_IN_SET 查詢排序,需要根據(jù)開發(fā)實(shí)際情況來(lái)使用。

官方示例

package main
?
import (
 ? ?"database/sql"
 ? ?"fmt"
 ? ?"log"
 ? ?_ "github.com/lib/pq"
 ? ?"github.com/jmoiron/sqlx"
)
?
var schema = `
CREATE TABLE person (
 ? ?first_name text,
 ? ?last_name text,
 ? ?email text
);
?
CREATE TABLE place (
 ? ?country text,
 ? ?city text NULL,
 ? ?telcode integer
)`
?
type Person struct {
 ? ?FirstName string `db:"first_name"`
 ? ?LastName ?string `db:"last_name"`
 ? ?Email ? ? string
}
?
type Place struct {
 ? ?Country string
 ? ?City ? ?sql.NullString
 ? ?TelCode int
}
?
func main() {
 ? ?// this Pings the database trying to connect
 ? ?// use sqlx.Open() for sql.Open() semantics
 ? ?db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
 ? ?if err != nil {
 ? ? ? ?log.Fatalln(err)
 ?  }
?
 ? ?// exec the schema or fail; multi-statement Exec behavior varies between
 ? ?// database drivers;  pq will exec them all, sqlite3 won't, ymmv
 ? ?db.MustExec(schema)
 ? ?tx := db.MustBegin()
 ? ?tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "jmoiron@jmoiron.net")
 ? ?tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "johndoeDNE@gmail.net")
 ? ?tx.MustExec("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1")
 ? ?tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852")
 ? ?tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Singapore", "65")
 ? ?// Named queries can use structs, so if you have an existing struct (i.e. person := &Person{}) that you have populated, you can pass it in as &person
 ? ?tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "jane.citzen@example.com"})
 ? ?tx.Commit()
?
 ? ?// Query the database, storing results in a []Person (wrapped in []interface{})
 ? ?people := []Person{}
 ? ?db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
 ? ?jason, john := people[0], people[1]
?
 ? ?fmt.Printf("%#v\n%#v", jason, john)
 ? ?// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
 ? ?// Person{FirstName:"John", LastName:"Doe", Email:"johndoeDNE@gmail.net"}
?
 ? ?// You can also get a single result, a la QueryRow
 ? ?jason = Person{}
 ? ?err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
 ? ?fmt.Printf("%#v\n", jason)
 ? ?// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
?
 ? ?// if you have null fields and use SELECT *, you must use sql.Null* in your struct
 ? ?places := []Place{}
 ? ?err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
 ? ?if err != nil {
 ? ? ? ?fmt.Println(err)
 ? ? ? ?return
 ?  }
 ? ?usa, singsing, honkers := places[0], places[1], places[2]
 ? ?fmt.Printf("%#v\n%#v\n%#v\n", usa, singsing, honkers)
 ? ?// Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
 ? ?// Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
 ? ?// Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
?
 ? ?// Loop through rows using only one struct
 ? ?place := Place{}
 ? ?rows, err := db.Queryx("SELECT * FROM place")
 ? ?for rows.Next() {
 ? ? ? ?err := rows.StructScan(&place)
 ? ? ? ?if err != nil {
 ? ? ? ? ? ?log.Fatalln(err)
 ? ? ?  } 
 ? ? ? ?fmt.Printf("%#v\n", place)
 ?  }
 ? ?// Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
 ? ?// Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
 ? ?// Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
?
 ? ?// Named queries, using `:name` as the bindvar.  Automatic bindvar support
 ? ?// which takes into account the dbtype based on the driverName on sqlx.Open/Connect
 ? ?_, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`, 
 ? ? ? ?map[string]interface{}{
 ? ? ? ? ? ?"first": "Bin",
 ? ? ? ? ? ?"last": "Smuth",
 ? ? ? ? ? ?"email": "bensmith@allblacks.nz",
 ?  })
?
 ? ?// Selects Mr. Smith from the database
 ? ?rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{"fn": "Bin"})
?
 ? ?// Named queries can also use structs.  Their bind names follow the same rules
 ? ?// as the name -> db mapping, so struct fields are lowercased and the `db` tag
 ? ?// is taken into consideration.
 ? ?rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)
 ? ?// batch insert
 ? ?// batch insert with structs
 ? ?personStructs := []Person{
 ? ? ?  {FirstName: "Ardie", LastName: "Savea", Email: "asavea@ab.co.nz"},
 ? ? ?  {FirstName: "Sonny Bill", LastName: "Williams", Email: "sbw@ab.co.nz"},
 ? ? ?  {FirstName: "Ngani", LastName: "Laumape", Email: "nlaumape@ab.co.nz"},
 ?  }
?
 ? ?_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
 ? ? ? ?VALUES (:first_name, :last_name, :email)`, personStructs)
?
 ? ?// batch insert with maps
 ? ?personMaps := []map[string]interface{}{
 ? ? ?  {"first_name": "Ardie", "last_name": "Savea", "email": "asavea@ab.co.nz"},
 ? ? ?  {"first_name": "Sonny Bill", "last_name": "Williams", "email": "sbw@ab.co.nz"},
 ? ? ?  {"first_name": "Ngani", "last_name": "Laumape", "email": "nlaumape@ab.co.nz"},
 ?  }
?
 ? ?_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
 ? ? ? ?VALUES (:first_name, :last_name, :email)`, personMaps)
}

更多示例請(qǐng)參考官方文檔:https://github.com/jmoiron/sqlx/blob/master/sqlx_test.go

以上就是詳解Golang中SQLX庫(kù)的高級(jí)操作的詳細(xì)內(nèi)容,更多關(guān)于Golang SQLX的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 一文帶你掌握Go語(yǔ)言I/O操作中的io.Reader和io.Writer

    一文帶你掌握Go語(yǔ)言I/O操作中的io.Reader和io.Writer

    在?Go?語(yǔ)言中,io.Reader?和?io.Writer?是兩個(gè)非常重要的接口,它們?cè)谠S多標(biāo)準(zhǔn)庫(kù)中都扮演著關(guān)鍵角色,下面就跟隨小編一起學(xué)習(xí)一下它們的使用吧
    2025-01-01
  • Go Uber靜態(tài)分析工具NilAway使用初體驗(yàn)

    Go Uber靜態(tài)分析工具NilAway使用初體驗(yàn)

    這篇文章主要介紹了Go Uber靜態(tài)分析工具NilAway使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • golang redigo發(fā)布訂閱使用的方法

    golang redigo發(fā)布訂閱使用的方法

    本文主要介紹了golang redigo發(fā)布訂閱使用的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • GO語(yǔ)言字符串常用操作小結(jié)

    GO語(yǔ)言字符串常用操作小結(jié)

    本文主要介紹了GO語(yǔ)言字符串常用操作小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Go 代碼生成工具詳解

    Go 代碼生成工具詳解

    這篇文章主要介紹了Go 代碼生成工具詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 手把手教你導(dǎo)入Go語(yǔ)言第三方庫(kù)

    手把手教你導(dǎo)入Go語(yǔ)言第三方庫(kù)

    本文主要介紹了手把手教你導(dǎo)入Go語(yǔ)言第三方庫(kù),通過(guò)導(dǎo)入gin包來(lái)深入學(xué)習(xí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • golang Gorm與數(shù)據(jù)庫(kù)完整性約束詳解

    golang Gorm與數(shù)據(jù)庫(kù)完整性約束詳解

    這篇文章主要介紹了golang Gorm與數(shù)據(jù)庫(kù)完整性約束詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Go語(yǔ)言的管道Channel用法實(shí)例

    Go語(yǔ)言的管道Channel用法實(shí)例

    這篇文章主要介紹了Go語(yǔ)言的管道Channel用法,實(shí)例分析了Go語(yǔ)言中管道的原理與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • 如何使用go-zero開發(fā)線上項(xiàng)目

    如何使用go-zero開發(fā)線上項(xiàng)目

    這篇文章主要介紹了如何使用go-zero開發(fā)線上項(xiàng)目,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Golang實(shí)現(xiàn)字符串倒序的幾種解決方案

    Golang實(shí)現(xiàn)字符串倒序的幾種解決方案

    給定一個(gè)字符串,按單詞將該字符串逆序是我們大家在開發(fā)中可能會(huì)遇到的一個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于Golang如何實(shí)現(xiàn)字符串倒序的幾種解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-10-10

最新評(píng)論