dbutil

package module
v0.0.0-...-593dca4 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2020 License: MIT Imports: 11 Imported by: 0

README

dbutil-mysql

go mysql orm

此库尚未在生产环境中使用!!!

依赖第三方库

1、https://github.com/go-sql-driver/mysql v1.5.0

使用指南

1.struct 定义规则

type UserModel struct {
    UserId     string    `json:"userId" db:"user_id" dbField:"true" dbType:"varchar"`
    Username   string    `json:"username" db:"username" dbField:"true" dbType:"varchar"`
    Realname   string    `json:"realname" db:"realname" dbField:"true" dbType:"varchar"`
    Password   string    `json:"password" db:"password" dbField:"true" dbType:"varchar"`
    Remark     string    `json:"remark" db:"remark" dbField:"true" dbType:"varchar"`
    CreateId   string    `json:"createId" db:"create_id" dbField:"true" dbType:"varchar"`
    CreateTime string    `json:"createTime" db:"create_time" dbField:"true" dbType:"varchar"`
    UpdateId   string    `json:"updateId" db:"update_id" dbField:"true" dbType:"varchar"`
    UpdateTime LocalTime `json:"updateTime" db:"update_time" dbField:"true" dbType:"datetime"`
    UserIds    string    `json:"userIds" db:"user_id" dbType:"varchar"`
}

// UserModel需要实现GetTableName方法,返回UserModel对应的数据库表名
func (user UserModel) GetTableName() string {
    return "sys_user"
}

tag说明:

json: struct字段对应前端字段名称
db: struct字段对应数据库表字段名称
dbType: 数据库表字段的数据类型
dbField: struct字段为数据库表字段时,配置为true

2.初始化连接池

InitPool("root", "root", "127.0.0.1", 3306, "demo", "utf8mb4")

3.新增操作

func TestInsertEngine_Insert(t *testing.T) {
    var user UserModel
    
    user.UserId = "123"
    user.Username = "test"
    user.Password = "123"
    user.Realname = "测试"
    user.CreateId = "123"
    user.CreateTime = "2020-07-09 23:38:38"

    err := NewInsertEngine(user).Insert()

    t.Log(err)
}

未完待续...

Documentation

Index

Constants

View Source
const FormatLayout = "2006-01-02 15:04:05"

FormatLayout 日期时间格式化格式

Variables

This section is empty.

Functions

func InitPool

func InitPool(username string, password string, ip string, port int, dbname string, charset string)

InitPool 初始化数据库链接池 username 用户名 [mysql] password 密码 [mysql] ip IP地址 [mysql] port 端口 [mysql] dbname 数据库名 [mysql] charset 编码 [mysql]

Types

type BatchInsertEngine

type BatchInsertEngine struct {
	// contains filtered or unexported fields
}

BatchInsertEngine struct

func NewBatchInsertEngine

func NewBatchInsertEngine(models []Model) *BatchInsertEngine

NewBatchInsertEngine 创建批量新增代理

func NewBatchInsertEngineDB

func NewBatchInsertEngineDB(models []Model, db *DB) *BatchInsertEngine

NewBatchInsertEngineDB 创建批量新增代理(事务)

func (*BatchInsertEngine) InsertMany

func (engine *BatchInsertEngine) InsertMany() error

InsertMany 批量新增

type BatchUpdateEngine

type BatchUpdateEngine struct {
	// contains filtered or unexported fields
}

BatchUpdateEngine struct

func NewBatchUpdateEngine

func NewBatchUpdateEngine(models []Model) *BatchUpdateEngine

NewBatchUpdateEngine 创建BatchUpdateEngine

func NewBatchUpdateEngineDB

func NewBatchUpdateEngineDB(models []Model, db *DB) *BatchUpdateEngine

NewBatchUpdateEngineDB 创建BatchUpdateEngine(事务)

func (*BatchUpdateEngine) ReplaceIntoMany

func (engine *BatchUpdateEngine) ReplaceIntoMany() error

ReplaceIntoMany 批量替换新增 根据主键或者唯一索引,存在则删除后新增,不存在则新增

type Condition

type Condition struct {
	ModelReflect
	// contains filtered or unexported fields
}

Condition 条件结构体

func InitCondition

func InitCondition(model Model) Condition

InitCondition 初始化条件结构体

func (*Condition) WhereEqZero

func (condition *Condition) WhereEqZero(jsonFields ...string) *Condition

WhereEqZero 等于0

func (*Condition) WhereEqs

func (condition *Condition) WhereEqs(jsonFields ...string) *Condition

WhereEqs 等于条件

func (*Condition) WhereGes

func (condition *Condition) WhereGes(jsonFields ...string) *Condition

WhereGes 大于等于条件

func (*Condition) WhereGts

func (condition *Condition) WhereGts(jsonFields ...string) *Condition

WhereGts 大于条件

func (*Condition) WhereIns

func (condition *Condition) WhereIns(jsonFields ...string) *Condition

WhereIns 包含条件

func (*Condition) WhereIsNotNull

func (condition *Condition) WhereIsNotNull(jsonFields ...string) *Condition

WhereIsNotNull 是null值

func (*Condition) WhereIsNull

func (condition *Condition) WhereIsNull(jsonFields ...string) *Condition

WhereIsNull 是null值

func (*Condition) WhereLeftLikes

func (condition *Condition) WhereLeftLikes(jsonFields ...string) *Condition

WhereLeftLikes 左模糊匹配条件

func (*Condition) WhereLes

func (condition *Condition) WhereLes(jsonFields ...string) *Condition

WhereLes 小于等于条件

func (*Condition) WhereLikes

func (condition *Condition) WhereLikes(jsonField ...string) *Condition

WhereLikes 模糊匹配条件

func (*Condition) WhereLts

func (condition *Condition) WhereLts(jsonFields ...string) *Condition

WhereLts 小于条件

func (*Condition) WhereNeqZero

func (condition *Condition) WhereNeqZero(jsonFields ...string) *Condition

WhereNeqZero 不等于0

func (*Condition) WhereNes

func (condition *Condition) WhereNes(jsonFields ...string) *Condition

WhereNes 不等于条件

func (*Condition) WhereRightLikes

func (condition *Condition) WhereRightLikes(jsonFields ...string) *Condition

WhereRightLikes 右模糊匹配条件

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB 数据库链接

func (*DB) DoTransaction

func (db *DB) DoTransaction(dbOperates func() error) (bool, error)

DoTransaction 事务操作

func (*DB) SelectBySql

func (db *DB) SelectBySql(selectSql string, params ...interface{}) ([]map[string]interface{}, error)

SelectBySql 查询记录

func (*DB) SelectCountBySql

func (db *DB) SelectCountBySql(countSql string, params ...interface{}) (int, error)

SelectCountBySql 查询记录数

func (*DB) SelectOneBySql

func (db *DB) SelectOneBySql(selectSql string, params ...interface{}) (map[string]interface{}, error)

SelectOneBySql 查询一行记录

type DeleteEngine

type DeleteEngine struct {
	Condition
	// contains filtered or unexported fields
}

DeleteEngine struct

func NewDeleteEngine

func NewDeleteEngine(model Model) *DeleteEngine

NewDeleteEngine 创建DeleteEngine

func NewDeleteEngineDB

func NewDeleteEngineDB(model Model, db *DB) *DeleteEngine

NewDeleteEngineDB 创建DeleteEngine(事务)

func (*DeleteEngine) Delete

func (engine *DeleteEngine) Delete() (int, error)

Delete 删除

type InsertEngine

type InsertEngine struct {
	ModelReflect
	// contains filtered or unexported fields
}

InsertEngine struct

func NewInsertEngine

func NewInsertEngine(model Model) *InsertEngine

NewInsertEngine 创建InsertEngine

func NewInsertEngineDB

func NewInsertEngineDB(model Model, db *DB) *InsertEngine

NewInsertEngineDB 创建InsertEngine(事务)

func (*InsertEngine) Insert

func (insertEngine *InsertEngine) Insert() error

Insert 新增

type LocalTime

type LocalTime time.Time

LocalTime 自定义时间类型

func (LocalTime) MarshalJSON

func (localTime LocalTime) MarshalJSON() ([]byte, error)

MarshalJSON marshal json方法

func (LocalTime) String

func (localTime LocalTime) String() string

String 字符串方法

func (*LocalTime) UnmarshalJSON

func (localTime *LocalTime) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON unmarshal json方法

func (LocalTime) Value

func (localTime LocalTime) Value() (driver.Value, error)

Value 插入mysql使用

type Model

type Model interface {
	// 返回数据库表名
	GetTableName() string
}

Model 接口

type ModelReflect

type ModelReflect struct {
	// contains filtered or unexported fields
}

ModelReflect 实体

func InitModelReflect

func InitModelReflect(model Model) ModelReflect

InitModelReflect 初始化ModelReflect

type SelectEngine

type SelectEngine struct {
	Condition
	// contains filtered or unexported fields
}

SelectEngine 查询代理

func NewSelectEngine

func NewSelectEngine(model Model) *SelectEngine

NewSelectEngine 创建查询代理

func NewSelectEngineDB

func NewSelectEngineDB(model Model, db *DB) *SelectEngine

NewSelectEngineDB 事务

func (*SelectEngine) Limit

func (selectEngine *SelectEngine) Limit(currentPage int, pageSize int) *SelectEngine

Limit 分页设置

func (*SelectEngine) OrderByAsc

func (selectEngine *SelectEngine) OrderByAsc(jsonFields ...string) *SelectEngine

OrderByAsc 正序排序

func (*SelectEngine) OrderByDesc

func (selectEngine *SelectEngine) OrderByDesc(jsonFields ...string) *SelectEngine

OrderByDesc 倒序排序

func (*SelectEngine) SelectAll

func (selectEngine *SelectEngine) SelectAll() ([]map[string]interface{}, error)

SelectAll 根据条件查询所有记录

func (*SelectEngine) SelectOne

func (selectEngine *SelectEngine) SelectOne() (map[string]interface{}, error)

SelectOne 查询一行记录

func (*SelectEngine) SelectPage

func (selectEngine *SelectEngine) SelectPage() ([]map[string]interface{}, int, error)

SelectPage 根据条件分页查询

type UpdateEngine

type UpdateEngine struct {
	Condition
	// contains filtered or unexported fields
}

UpdateEngine 更新Engine

func NewUpdateEngine

func NewUpdateEngine(model Model) *UpdateEngine

NewUpdateEngine 创建InsertEngine

func NewUpdateEngineDB

func NewUpdateEngineDB(model Model, db *DB) *UpdateEngine

NewUpdateEngineDB 创建UpdateEngine(事务)

func (*UpdateEngine) Update

func (engine *UpdateEngine) Update() (int, error)

Update 更新

Jump to

Keyboard shortcuts

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