mysql

package
v0.6.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2021 License: MIT Imports: 5 Imported by: 0

README

github.com/gkzy/gow/lib/mysql

使用方法:

单数据库使用时

// 初始化链接
func init(){
    config:=&DBConfig{}
    mysql.InitDefaultDB(config)
}

// 按gorm方式使用
mysql.GetORM()
....

多数据库时

// 初始化链接
func init() {
    // 配置多个DBConfig
    configs:=make([]*DBConfig,0)
    mysql.InitDB(configs)
}

//按gorm方式使用
db:=mysql.GetORMByName("user")
....
一个demo
type User struct {
    UID      int64  `gorm:"column:uid"`
    Nickname string `gorm:"column:nickname"`
}

func (*User) TableName() string {
    return "tbl_user"
}

func init(){
    config := &mysql.DBConfig{
        Name:     "gkzy",
        User:     "root",
        Password: "123456",
        Host:     "127.0.0.1",
        Port:     6606,
    }

    err := mysql.InitDefaultDB(config)
    if err != nil {
       fmt.Println(err)
    }

}

//MysqlDB gorm风格的数据库操作

func MysqlDB() {
    user := make([]*User, 0)
    db := GetORM()
    err = db.Model(user).Find(&user).Error
    if err != nil {
       fmt.Println(err)
    }
    .....
    ...

}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetORM

func GetORM() *gorm.DB

GetORM GetORM

func GetORMByName

func GetORMByName(name string) *gorm.DB

GetORMByName get orm by name

func InitDB

func InitDB(list []*DBConfig) (err error)

InitDB init multiple db

func InitDefaultDB

func InitDefaultDB(db *DBConfig) (err error)

InitDefaultDB init single db

Types

type DBConfig

type DBConfig struct {
	Name            string //库名
	User            string //登录名
	Password        string //密码
	Host            string //主机
	Port            int    //port
	Debug           bool   //是否debug
	DisablePrepared bool   //是否disable prepared
}

DBConfig mysql配置文件

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL