package dbclient
import (
"database/sql/driver"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"reflect"
)
var db *sqlx.DB
func InitDB() (err error) {
dsn := "root:123456@tcp(127.0.0.1:3306)/nacos?charset=utf8mb4&parseTime=True"
db, err = sqlx.Connect("mysql", dsn)
if err != nil {
fmt.Println("dsn 格式有误")
return err
}
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(5)
fmt.Println("db connect success")
return
}
func SelectOne() {
sqlStr := "select username,role from roles where username = ?"
var r role
err := db.Get(&r, sqlStr, "nacos")
if err != nil {
fmt.Println("db get failed,", err)
return
}
fmt.Printf("%v\n", r)
}
func SelectMore() {
sqlStr := "select username,role from roles"
var rs []role
err := db.Select(&rs, sqlStr)
if err != nil {
fmt.Println("db get failed,", err)
return
}
fmt.Printf("%v\n", rs)
}
func Update() {
sqlStr := "update roles set role = ? where username =?"
exec, err := db.Exec(sqlStr, "wwwww", "heheheh")
if err != nil {
fmt.Printf("update is failed,err:%v\n", err)
return
}
fmt.Println(exec.RowsAffected())
}
func Insert() {
sqlStr := "insert into roles values(?,?)"
exec, err := db.Exec(sqlStr, "wwwww", "heheheh")
if err != nil {
fmt.Printf("update is failed,err:%v\n", err)
return
}
fmt.Println(exec.RowsAffected())
}
func NamedExec() {
sqlStr := "insert into roles values(:username,:role)"
r := role{
"zhangsan",
"admin",
}
exec, err := db.NamedExec(sqlStr, r)
if err != nil {
fmt.Printf("update is failed,err:%v\n", err)
return
}
fmt.Println(exec.RowsAffected())
}
func NamedQuery() {
sqlStr := "select * from roles"
rows, err := db.NamedQuery(sqlStr, role{})
if err != nil {
fmt.Println("NamedQuery filed,err:", err)
return
}
for rows.Next() {
var r role
rows.StructScan(&r)
fmt.Println(r)
}
}
func TxExec() {
tx, err := db.Begin()
if err != nil {
fmt.Println("tx begin failed,err:", err)
return
}
sqlStr := "update roles set role = ? where username =?"
_, err = tx.Exec(sqlStr, "wwww11w", "heheheh")
if err != nil {
fmt.Printf("update is failed,err:%v\n", err)
return
tx.Rollback()
}
tx.Commit()
}
type role struct {
Username string `db:"username"`
Role string `db:"role"`
}
func (r *role) Value() (driver.Value, error) {
return []interface{}{r.Username, r.Role}, nil
}
func BatchInsert(rs []interface{}) error {
query, args, err := sqlx.In("insert into roles values(?),(?)", rs...)
if err != nil {
fmt.Println("sqlx.in failed", err)
return err
}
fmt.Println(query)
fmt.Println(args)
db.Exec(query, args)
return err
}
func NamedExecInsert() error {
var rs = make([]*role, 2)
rs = append(rs, &role{
"lisi",
"admin",
}, &role{
"lisi1",
"admin1",
})
exec, err := db.NamedExec("insert into roles values (:username,:role)", rs)
if err != nil {
fmt.Println(err)
}
fmt.Println(exec.RowsAffected())
return err
}
func QueryIn(usernames []string) (rs []role, err error) {
query, args, err := sqlx.In("select * from roles where username in (?)", usernames)
if err != nil {
fmt.Println("sqlx.in failed,err", err)
return nil, err
}
fmt.Println("query:", query)
query = db.Rebind(query)
fmt.Println(reflect.TypeOf(args))
err = db.Select(&rs, query, args...)
if err != nil {
fmt.Println("select failed", err)
return rs, err
}
fmt.Println(rs)
return rs, err
}
|