zdpgo_gorm

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2022 License: MIT Imports: 9 Imported by: 8

README

zdpgo_gorm

基于gorm二次封装的一个orm框架

版本历史

  • 版本0.1.0 2022年2月16日 基本功能

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseModel

type BaseModel struct {
	ID        uint64         `gorm:"primarykey"`
	CreatedAt time.Time      `gorm:"column:add_time"`    // 添加时间
	UpdatedAt time.Time      `gorm:"column:update_time"` // 修改时间
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at"`  // 删除时间
}

BaseModel 基础模型

type Gorm

type Gorm struct {
	DB    *gorm.DB // gorm操作数据库的核心对象
	SqlDb *sql.DB  // 执行SQL语句的连接对象
	// contains filtered or unexported fields
}

Gorm 核心对象

func New

func New(config GormConfig) *Gorm

New 创建gorm对象 @param config 配置对象

func (*Gorm) Create

func (g *Gorm) Create(model interface{}) error

Create 新增

func (*Gorm) CreateTable

func (g *Gorm) CreateTable(model interface{}) error

CreateTable 创建表格

func (*Gorm) CreateTableList

func (g *Gorm) CreateTableList(models ...interface{}) error

CreateTableList 创建多个表格

func (*Gorm) Delete

func (g *Gorm) Delete(model interface{}, id uint64) error

Delete 根据id删除数据

func (*Gorm) DeleteByIdLIst

func (g *Gorm) DeleteByIdLIst(model interface{}, ids ...uint64) error

DeleteByIdLIst 根据id列表删除数据

func (*Gorm) Exists

func (g *Gorm) Exists(model interface{}, key string, value interface{}) bool

Exists 判断数据是否已存在

func (*Gorm) Find

func (g *Gorm) Find(model interface{}, id uint64) error

Find 根据id查询数据

func (*Gorm) FindByIdList

func (g *Gorm) FindByIdList(model interface{}, ids ...uint64) error

FindByIdList 根据id列表查询

func (*Gorm) FindByMap

func (g *Gorm) FindByMap(model interface{}, condition map[string]interface{}) error

FindByMap 根据map条件查询

func (*Gorm) InitMysql

func (g *Gorm) InitMysql(config *MysqlConfig)

InitMysql 初始化Mysql数据库连接 @param config MySQL配置对象

func (*Gorm) Page

func (g *Gorm) Page(page, size int, model interface{})

Page 分页查询 @param page 第几页 @param size 每页数量 @param model 数据源,从哪个模型查。比如从&users查询,users是user表的所有数据。

func (*Gorm) Update

func (g *Gorm) Update(model interface{}) error

Update 更新数据

type GormConfig

type GormConfig struct {
	Debug       bool        // 是否为调试模式
	LogFilePath string      // 日志存放路径
	Mysql       MysqlConfig // mysql的配置信息
}

GormConfig 配置对象

type JsonList

type JsonList []string

JsonList json列表类型

func (*JsonList) Scan

func (g *JsonList) Scan(value interface{}) error

Scan 重写Scan方法,实现数据出库

func (JsonList) Value

func (g JsonList) Value() (driver.Value, error)

Value 重写Value方法,实现数据入库

type MysqlConfig

type MysqlConfig struct {
	Host            string `mapstructure:"host" json:"host"`                           // 主机地址
	Port            int    `mapstructure:"port" json:"port"`                           // 端口号
	Database        string `mapstructure:"db" json:"database"`                         // 数据库名称
	User            string `mapstructure:"user" json:"username"`                       // 用户名
	Password        string `mapstructure:"password" json:"password"`                   // 密码
	MaxIdleConn     uint   `mapstructure:"max_idle_conn" json:"max_idle_conn"`         // 设置空闲连接池中连接的最大数量
	MaxOpenConn     uint   `mapstructure:"max_open_conn" json:"max_open_conn"`         // 设置打开数据库连接的最大数量
	ConnMaxLifetime uint   `mapstructure:"conn_max_lifetime" json:"conn_max_lifetime"` // 设置了连接可复用的最大时间
}

MysqlConfig MySQL配置信息

Directories

Path Synopsis
examples
gorm/z01_hello command
gorm/z03_create command

Jump to

Keyboard shortcuts

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