dbutil

package
v0.0.0-...-d6ebf68 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchInsert

func BatchInsert(db *gorm.DB, list interface{}) error

使用了大量反射, 性能可能差

批量插入 每次默认插入 1000 条 , 1000 条是一个保守值

这个方法的作用是 防止 mysql 1390 错误 (预处理语句占位符太多) occur mysql 1390 error when list size is too big .

db (*gorm.Db) 需要先调用 model 参数, 要不然 gorm 不知道 list 的类型!

func BatchInsertPage

func BatchInsertPage(db *gorm.DB, list interface{}, pageSize int) error

使用了大量反射, 性能可能差

批量插入 每次默认插入 1000 条 , 1000 条是一个保守值

这个方法的作用是 防止 mysql 1390 错误 (预处理语句占位符太多) occur mysql 1390 error when list size is too big .

db (*gorm.Db) 需要先调用 model 参数, 要不然 gorm 不知道 list 的类型!

Types

type PageEntity

type PageEntity struct {
	// 总条数
	Total int64 `json:"total"`
	// 分页集合 , 如果需要用结果, 需要自己转换
	List interface{} `json:"list"`
}

func GetOrmPage

func GetOrmPage(queryDb *gorm.DB, query *domain.PageQuery, list interface{}, order string, fields string) *PageEntity

获取 orm 分页, 使用 gorm 而不用 sql

queryDb:  gorm 对象 ,必须先执行  db.Model 指定 类型,否则会导致类型判断错误! 查询不出数据 !
query:   分页查询对象
list:    容器, 必须是指针类型, 例如 &[]entity.UserIndexGroup{}
order:  排序字段 例如 "ID DESC"
fields:  自定义字段 ,多个用 `,` 分割 , 例如 "ID,name , age ", 如果为空, 则返回 全部字段.

func GetSqlPage

func GetSqlPage(db *gorm.DB, listContainer interface{}, sql string, query *domain.PageQuery, params []interface{},
	fields string) *PageEntity

原生 sql 分页

db: gorm 指针对象
listContainer: 是 数组容器  参数示例 &[]entity.OaDaily{} ,
sql: 要分页的 sql 语句
query: 分页对象
param:  执行sql中的 预处理参数,类型为 数组
fields: 自定义字段 ,多个用 `,` 分割 , 例如 "ID,name , age ", 如果为空, 则返回 全部字段.

Jump to

Keyboard shortcuts

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