GithubHelp home page GithubHelp logo

querydb's Introduction

querydb

这是一个针对 go mysql 查询的查询构造器,支持主从配置,支持读写分离。

配置范例:

//配置集合
    master := &querydb.Config{
        Username:        "root",
        Password:        "mysql",
        Host:            "127.0.0.1",
        Port:            "33061",
        Charset:         "utf8mb4",
        Database:        "ott",
        MaxLifetime: 120,
        MaxIdleConns:    200,
        MaxOpenConns:    800,
    }
    slave1 := &querydb.Config{
        Username:        "root",
        Password:        "mysql",
        Host:            "127.0.0.1",
        Port:            "33061",
        Charset:         "utf8mb4",
        Database:        "ott",
        MaxLifetime: 120,
        MaxIdleConns:    200,
        MaxOpenConns:    800,
    }
    slave2 := &querydb.Config{
        Username:        "root",
        Password:        "mysql",
        Host:            "127.0.0.1",
        Port:            "33061",
        Charset:         "utf8mb4",
        Database:        "ott",
        MaxLifetime: 120,
        MaxIdleConns:    200,
        MaxOpenConns:    800,
    }
    master.SetSlave(slave1)
    master.SetSlave(slave2)
    instance := querydb.Default()
    instance.SetConfig("test", master)
    db := instance.Write("test") //主库
    db := instance.Read("test") //从库

    type user struct {
        Id int `json:"json"`
        Name string `json:"name"`
    }
    var result []user
    db.Table("user").Rows().ToStruct(&result)
    fmt.Println("result:", result)

查询数据

//查询单条数据
//返回[]string
arr, err := db.Table("user").Where("id", 1).Row().ToArray()


//返回map[string][string
mp, err := db.Table("user").Where("id", 1).Row().ToMap()


type user struct {
    Id int `json:"id"`
    Name string `json:"name"`
}

//返回结构体
var result user
err := db.Table("user").Where("id", 1).First().ToStruct(&result)




//查询多条数据
//返回[][]string
arr, err := db.Table("user").Where("id", 1).Get().ToArray()

//返回[]map[string][string
mp, err := db.Table("user").Where("id", 1).Get().ToMap()

type user struct {
    Id int `json:"id"`
    Name string `json:"name"`
}
//返回结构体
var result []user
err := db.Table("user").Where("id", 1).Get().ToStruct(&result)

插入数据

//通过结构体插入
type user struct {
	Id int `json:"-"`   //tag中包含`-`属性的时候,插入时会自动过滤
	Name string `json:"name"`
}

a1 := new(user)
a1.Name = "张三"

//插入单条
db.Table("user").Insert(a1)

//插入多条
a1 := new(user)
a1.Name = "张三"

a2 := new(user)
a2.Name = "李四"
users := []user{*a1, *a2}
db.Table("user").MultiInsert(users)

//通过map方式插入
user := make(map[string]string)
user["name"] = "张三"
db.Table("user").Insert(user)

更新数据

data := make(map[string]interface{})
data["name"] = "李四"

db.Table("user").Where("id", 1).Update(data)

删除数据

db.Table("user").Where("id", 1).Delete()

事务

开启事务需要用主库

db.Begin()

db.Table("user").Where("id", 1).Delete()

db.Commit()

db.RollBack()

querydb's People

Contributors

abulo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.