xxdb

package
v0.2.56 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MulanPSL-2.0 Imports: 15 Imported by: 0

Documentation

Overview

@author fenglei

@author fenglei

@author fenglei

@author fenglei

BaseDao 通用DAO基础类
提供常用的CRUD操作方法,减少重复代码
使用方法:
	schoolInfoDao := xxdb.NewMySQLBaseDao[model.SchoolPersonInfo](global.GVA_DB)

@author fenglei

@author fenglei

@author fenglei

@author fenglei

@author fenglei

Index

Constants

View Source
const (
	DefaultDialTimeout  = 10 * time.Second //连接超时
	DefaultReadTimeout  = 5 * time.Second  //读超时
	DefaultWriteTimeout = 5 * time.Second  //写超时
	DefaultPoolSize     = 10               //连接池大小
	DefaultMinIdleConns = 5                //最小空闲连接
	DefaultMaxRetries   = 3                //重试次数
)

默认配置常量

Variables

View Source
var DBDefaultPageSize = 10

设置缺省分页

Functions

func CloseMongo added in v0.1.5

func CloseMongo(mongo *mongo.Client) error

func CloseMySQL

func CloseMySQL(db *gorm.DB) error

func CloseRedis added in v0.1.6

func CloseRedis(redis *redis.Client) error

func CloseRedisCluster added in v0.2.11

func CloseRedisCluster(cluster *redis.ClusterClient) error

func GenMySQL

func GenMySQL(dns string, tables ...string) error

func GormError

func GormError(err error) bool

func GormToMap

func GormToMap(v any) map[string]interface{}

func GormWhere

func GormWhere(wheres map[string]map[string]interface{}) func(db *gorm.DB) *gorm.DB

GormWhere 构建 GORM 查询条 GormWhere 构建 GORM 查询条件

支持的条件 Key:

| Key       | 操作符              | 说明                    |
|-----------|---------------------|------------------------|
| AND       | =                   | 等于                   |
| OR        | =                   | 或条件                 |
| NOT       | !=                  | 非条件                 |
| LIKE      | LIKE 'x%'           | 右模糊                 |
| LIKE_ALL  | LIKE '%x%'          | 全模糊                 |
| GT        | >                   | 大于                   |
| GTE       | >=                  | 大于等于               |
| LT        | <                   | 小于                   |
| LTE       | <=                  | 小于等于               |
| NE        | <>                  | 不等于                 |
| BETWEEN   | BETWEEN ? AND ?     | 范围查询(值为[]any)    |

示例:

wheres := map[string]map[string]interface{}{
    "AND":     {"status": 1},
    "GT":      {"age": 18},
    "NE":      {"type": 0},
    "BETWEEN": {"created_at": []any{startTime, endTime}},
}
db.Scopes(GormWhere(wheres)).Find(&list)

func MongoPage added in v0.1.5

func MongoPage(p *Pages) (findOptions options.FindOptions)

func NewMongo added in v0.1.5

func NewMongo(opts *MongoOptions) (*mongo.Client, error)

NewMySQL create a new gorm db instance with the given options.

func NewMySQL

func NewMySQL(opts *MySQLOptions) (*gorm.DB, error)

NewMySQL create a new gorm db instance with the given options.

func NewRedis added in v0.1.5

func NewRedis(opts *RedisOptions) (*redis.Client, error)

NewRedis 创建 Redis 客户端,支持完整的连接配置

func NewRedisCluster added in v0.2.11

func NewRedisCluster(opts *RedisOptions) (*redis.ClusterClient, error)

NewRedisCluster 创建 Redis 集群客户端(用于集群环境)

func Paginate added in v0.1.5

func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB

分页封装

func PaginatePage added in v0.1.5

func PaginatePage(p *Pages) func(db *gorm.DB) *gorm.DB

参考 https://www.cnblogs.com/leijiangsheng/p/16533014.html

Types

type MongoOptions added in v0.1.5

type MongoOptions struct {
	DSN         string
	MaxPoolSize int
}

type MySQLBaseDao added in v0.1.52

type MySQLBaseDao[T any] struct {
	// contains filtered or unexported fields
}

MySQLBaseDao 通用DAO基础结构

func NewMySQLBaseDao added in v0.1.52

func NewMySQLBaseDao[T any](db *gorm.DB) *MySQLBaseDao[T]

NewBaseDao 创建BaseDao实例

func (*MySQLBaseDao[T]) Count added in v0.1.52

func (e *MySQLBaseDao[T]) Count(wheres map[string]map[string]interface{}) int64

Count 统计数量

func (*MySQLBaseDao[T]) Create added in v0.2.23

func (d *MySQLBaseDao[T]) Create(obj *T) error

Create 创建记录

func (*MySQLBaseDao[T]) DeleteId added in v0.1.52

func (e *MySQLBaseDao[T]) DeleteId(id interface{}) error

Delete 删除

func (*MySQLBaseDao[T]) DeleteKeyId added in v0.1.52

func (e *MySQLBaseDao[T]) DeleteKeyId(key string, id interface{}) error

func (*MySQLBaseDao[T]) FindTake added in v0.1.52

func (e *MySQLBaseDao[T]) FindTake(wheres map[string]map[string]interface{}) *T

FindTake 单个查询

func (*MySQLBaseDao[T]) GetByID added in v0.2.20

func (e *MySQLBaseDao[T]) GetByID(id interface{}) *T

GetById 根据ID查询

func (*MySQLBaseDao[T]) GetByKeyId added in v0.1.52

func (e *MySQLBaseDao[T]) GetByKeyId(key string, id interface{}) *T

GetById 根据ID查询

func (*MySQLBaseDao[T]) GetDB added in v0.1.52

func (e *MySQLBaseDao[T]) GetDB() *gorm.DB

GetDB 获取DB实例,用于自定义查询

func (*MySQLBaseDao[T]) List added in v0.1.52

func (e *MySQLBaseDao[T]) List(wheres map[string]map[string]interface{}) *[]T

List 列表查询

func (*MySQLBaseDao[T]) ListOrder added in v0.1.54

func (e *MySQLBaseDao[T]) ListOrder(wheres map[string]map[string]interface{}, orderBy []string) *[]T

List 列表查询 orderBy: 排序字段数组,如 []string{"id desc", "created_at asc"},支持多字段排序

func (*MySQLBaseDao[T]) ListPage added in v0.1.52

func (e *MySQLBaseDao[T]) ListPage(wheres map[string]map[string]interface{}, pages *Pages) (*[]T, int64)

ListPage 分页查询

func (*MySQLBaseDao[T]) ListPageOrder added in v0.1.54

func (e *MySQLBaseDao[T]) ListPageOrder(wheres map[string]map[string]interface{}, pages *Pages, orderBy []string) (*[]T, int64)

ListPageOrder 分页查询带排序 orderBy: 排序字段数组,如 []string{"id desc", "created_at asc"},支持多字段排序

func (*MySQLBaseDao[T]) SaveOrUpdate added in v0.1.52

func (e *MySQLBaseDao[T]) SaveOrUpdate(obj interface{}, id int64) error

SaveOrUpdate 保存或更新

func (*MySQLBaseDao[T]) SaveOrUpdateIdStr added in v0.2.23

func (e *MySQLBaseDao[T]) SaveOrUpdateIdStr(obj interface{}, id string) error

SaveOrUpdate 保存或更新

func (*MySQLBaseDao[T]) Update added in v0.2.23

func (d *MySQLBaseDao[T]) Update(obj *T) error

Update 更新记录

func (*MySQLBaseDao[T]) UpdateByID added in v0.2.23

func (d *MySQLBaseDao[T]) UpdateByID(id any, updates map[string]any) error

UpdateByID 根据ID更新指定字段

type MySQLOptions

type MySQLOptions struct {
	DSN                   string
	MaxIdleConnections    int
	MaxOpenConnections    int
	MaxConnectionLifeTime time.Duration
	// +optional
	LogLevel logger.LogLevel
}

MySQLOptions defines options for mysql database.

type Pages added in v0.1.5

type Pages struct {
	Page     int `json:"page"`
	PageSize int `json:"pageSize"`
}

type RedisDao added in v0.1.5

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

func NewRedisDao added in v0.1.5

func NewRedisDao(redis *redis.Client) *RedisDao

func (*RedisDao) Del added in v0.1.30

func (e *RedisDao) Del(key string) error

func (*RedisDao) Exists added in v0.1.30

func (e *RedisDao) Exists(keys ...string) bool

func (*RedisDao) Get added in v0.1.43

func (e *RedisDao) Get(key string) (string, error)

func (*RedisDao) GetJson added in v0.1.30

func (e *RedisDao) GetJson(key string, val interface{}) error

func (*RedisDao) GetScan added in v0.1.30

func (e *RedisDao) GetScan(key string, val interface{}) error

func (*RedisDao) ScanKeys added in v0.1.30

func (e *RedisDao) ScanKeys(prefix string, limit int64) ([]string, error)

func (*RedisDao) Set added in v0.1.30

func (e *RedisDao) Set(key string, val interface{}, expiration time.Duration) error

func (*RedisDao) SetJson added in v0.1.30

func (e *RedisDao) SetJson(key string, val interface{}, expiration time.Duration) error

type RedisOptions added in v0.1.5

type RedisOptions struct {
	DSN          string
	Password     string
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	PoolSize     int
	MinIdleConns int
	MaxRetries   int
}

Directories

Path Synopsis
@author fenglei
@author fenglei
@author fenglei
@author fenglei

Jump to

Keyboard shortcuts

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