goweb-mysql connection

Database operations

Go language database / sql package defines a series of operations on the database. database / sql / driver
package is to be defined in the database-driven interface, these interfaces can be used sql package. But did not mention the Go language
for any official database-driven, so we need to import third-party database-driven. But we connect data
after most of the code libraries for database operations use sql package.

Database connection

Description Open function

  • DataSourceName parameter format:
    Database Username: Database Password @ [tcp (localhost: 3306) ] / database name
  • Open function may only verify its parameters, without creating a connection to the database. If you want to check the data
    source name is legitimate, Ping method returns the value should be called.
  • DB returned safe to go by multiple processes simultaneously, and will maintain its own idle connection pool.
    As a result, Open function is only called once. Rarely need to close DB
package utils

import (
    "database/sql"

    _ "github.com/go-sql-driver/mysql"
)

var (
    Db  *sql.DB
    err error
)

func init() {
    Db, err = sql.Open("mysql", "root:ygj1007502524@tcp(localhost:3306)/test")
    if err != nil {
        panic(err.Error())
    }
}

CRUD operations

Users create a table in the test database connection

CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(100) UNIQUE NOT NULL,
PASSWORD VARCHAR(100) NOT NULL,
email VARCHAR(100)
)

Users insert records into the table

package model

import (
    "fmt"
    "go_code/go-web/day01/web01_db/utils"
)

//User 结构体
type User struct {
    ID       int
    Username string
    Password string
    Email    string
}

//AddUser 添加User的方法一
func (user *User) AddUser() error {
    //1.写sql语句
    sqlStr := "insert into users(username,password,email) values(?,?,?)"
    //2.预编译
    inStmt, err := utils.Db.Prepare(sqlStr)
    if err != nil {
        fmt.Println("预编译出现异常:", err)
        return err
    }
    //3.执行
    _, err2 := inStmt.Exec("admin", "123456", "[email protected]")
    if err2 != nil {
        fmt.Println("执行出现异常:", err2)
        return err
    }
    return nil
}

//AddUser2 添加User的方法二
func (user *User) AddUser2() error {
    //1.写sql语句
    sqlStr := "insert into users(username,password,email) values(?,?,?)"
    //2.执行
    _, err := utils.Db.Exec(sqlStr, "admin2", "666666", "[email protected]")
    if err != nil {
        fmt.Println("执行出现异常:", err)
        return err
    }
    return nil
}

unit test

Brief introduction

As the name suggests, the test unit (unit test), is a kind of means provided to verify the correctness of the automated test
sample, a cell that is part of a modular program. Generally, a unit typically with a program
corresponding to a function or a method, this is not required. Go unit tests need to use the testing package
and go test command and test file has the following requirements
1) to be tested and test files source files must be in the same package
must end 2) test files to _test.go

  • Go though the test file _test.go prefix is not mandatory, but in general we are set
    for the file name of the file being tested, such as: To user.go test, the test file
    name we usually set to user_test. Go
    3) test file test function testXxx (t * testing.T)
  • Xxx which the first letter must be uppercase letters
  • Test.T function argument must be a pointer type (Benchmark test if the parameter is
    testing.B pointer type)

Test code:

package model

import (
    "fmt"
    "testing"
)

//TestMain函数可以在测试函数执行之前做一些其他操作
func TestMain(m *testing.M) {
    fmt.Println("测试开始:")
    //通过m.Run()来执行测试函数
    m.Run()
}

func TestUser(t *testing.T) {
    fmt.Println("开始测试User中的相关方法")
    //通过t.Run()来执行子测试函数
    t.Run("测试添加用户:", testAddUser)
}

//如果函数名不是以Test开头,那么该函数默认不执行,我们可以将它设置成为一个子测试函数
func testAddUser(t *testing.T) {
    fmt.Println("子测试函数执行:")
    user := &User{}
    //调用添加用户的方法
    user.AddUser()
    user.AddUser2()
}

Description testing package

If not, start with Test, then the default function name of a function when using the test go test command
a sub-test function when recognition is not executed, but we can set this function, you can function in other tests

We can also TestMain (m * testing.M) function before and after the test do some other
operations

a) The test file has TestMain function, execute the command will directly go test run TestMain
function, not directly run the test function, only the execution m.Run in TestMain function () only
performs test functions

b) If you want to see the detailed testing process, you can use the command go test -v

Gets a record

func (user *User) GetUserByID(userId int) (*User, error) {
//写 sql 语句
sqlStr := "select id , username , password , email from users where id = ?"
//执行 sql
row := utils.Db.QueryRow(sqlStr, userId)
//声明三个变量
var username string
var password string
var email string
//将各个字段中的值读到以上三个变量中
err := row.Scan(&userId, &username, &password, &email)
if err != nil {
return nil, err
}
//将三个变量的值赋给 User 结构体
u := &User{
ID: userId,
Username: username,
Password: password,
Email: email,
}
return u, nil
}

Get multiple records

func (user *User) GetUsers() ([]*User, error) {
//写 sql 语句
sqlStr := "select id , username , password , email from users"
//执行 sql
rows, err := utils.Db.Query(sqlStr)
if err != nil {
return nil, err
}
//定义一个 User 切片
var users []*User
//遍历
for rows.Next() {
//声明四个个变量
var userID int
var username string
var password string
var email string
//将各个字段中的值读到以上三个变量中
err := rows.Scan(&userID, &username, &password, &email)
if err != nil {
return nil, err
}
//将三个变量的值赋给 User 结构体
u := &User{
ID: userID,
Username: username,
Password: password,
Email: email,
}
//将 u 添加到 users 切片中
users = append(users, u)
}
return users, nil
}

Guess you like

Origin www.cnblogs.com/ygjzs/p/12048375.html