mysql

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

mysql

封装了gorm 的使用,简化了连接配置

gorm支持原生的sql语句查询,故兼容官方库的sql查询。

func TestInit(t *testing.T) {
	conf := Conf{
		Addr:               "localhost:3306",
		Password:           "root",
		User:               "root",
		DbName:             "test",
		Timeout:            0,
		WriteTimeout:       0,
		ReadTimeout:        0,
		MaxOpenConnections: 0,
		MaxIdleConnections: 0,
		MaxLifetime:        0,
	}
	init, err := Init(SetConf(conf))
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Println(init)
}

OutPut:

&{0xc0001e2360 <nil> 0 0xc00029e000 1}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(opts ...Option) (*gorm.DB, error)

Init 初始化数据库连接

Types

type Conf

type Conf struct {
	Addr               string `yaml:"addr"`                 //连接地址,类似:localhost:3306
	Password           string `yaml:"password"`             //数据库密码
	User               string `yaml:"user"`                 //用户名
	DbName             string `yaml:"db_name"`              //数据库名称
	Timeout            int    `yaml:"timeout"`              //单位:秒钟 默认:3
	WriteTimeout       int    `yaml:"write_timeout"`        //单位:秒钟 默认:3
	ReadTimeout        int    `yaml:"read_timeout"`         //单位:秒钟 默认:3
	MaxOpenConnections int    `yaml:"max_open_connections"` //设置打开数据库连接的最大数量 默认:0(不限制)。
	MaxIdleConnections int    `yaml:"max_idle_connections"` //用于设置连接池中空闲连接的最大数量 默认:2
	MaxLifetime        int    `yaml:"max_lifetime"`         //单位:秒钟 设置了连接可复用的最大时间 默认:3s。
}

Conf mysql的配置字段

func (Conf) DSN

func (c Conf) DSN() string

type Model

type Model struct {
	Id        uint64         `gorm:"primarykey" json:"id"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}

type ModelDict

type ModelDict map[string]interface{}

func NewDict

func NewDict() ModelDict

NewDict 用来保存Json的原始数据,类似`{"name":"123","age":18}`

func (*ModelDict) Scan

func (d *ModelDict) Scan(v interface{}) error

func (ModelDict) Value

func (d ModelDict) Value() (driver.Value, error)

type Option

type Option func(c *_Mysql)

func SetConf

func SetConf(c Conf) Option

func SetIoWriter

func SetIoWriter(w io.Writer) Option

SetIoWriter 设置日志写入器

Jump to

Keyboard shortcuts

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