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
- Variables
- func CloseMongo(mongo *mongo.Client) error
- func CloseMySQL(db *gorm.DB) error
- func CloseRedis(redis *redis.Client) error
- func CloseRedisCluster(cluster *redis.ClusterClient) error
- func GenMySQL(dns string, tables ...string) error
- func GormError(err error) bool
- func GormToMap(v any) map[string]interface{}
- func GormWhere(wheres map[string]map[string]interface{}) func(db *gorm.DB) *gorm.DB
- func MongoPage(p *Pages) (findOptions options.FindOptions)
- func NewMongo(opts *MongoOptions) (*mongo.Client, error)
- func NewMySQL(opts *MySQLOptions) (*gorm.DB, error)
- func NewRedis(opts *RedisOptions) (*redis.Client, error)
- func NewRedisCluster(opts *RedisOptions) (*redis.ClusterClient, error)
- func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB
- func PaginatePage(p *Pages) func(db *gorm.DB) *gorm.DB
- type MongoOptions
- type MySQLBaseDao
- func (e *MySQLBaseDao[T]) Count(wheres map[string]map[string]interface{}) int64
- func (d *MySQLBaseDao[T]) Create(obj *T) error
- func (e *MySQLBaseDao[T]) DeleteId(id interface{}) error
- func (e *MySQLBaseDao[T]) DeleteKeyId(key string, id interface{}) error
- func (e *MySQLBaseDao[T]) FindTake(wheres map[string]map[string]interface{}) *T
- func (e *MySQLBaseDao[T]) GetByID(id interface{}) *T
- func (e *MySQLBaseDao[T]) GetByKeyId(key string, id interface{}) *T
- func (e *MySQLBaseDao[T]) GetDB() *gorm.DB
- func (e *MySQLBaseDao[T]) List(wheres map[string]map[string]interface{}) *[]T
- func (e *MySQLBaseDao[T]) ListOrder(wheres map[string]map[string]interface{}, orderBy []string) *[]T
- func (e *MySQLBaseDao[T]) ListPage(wheres map[string]map[string]interface{}, pages *Pages) (*[]T, int64)
- func (e *MySQLBaseDao[T]) ListPageOrder(wheres map[string]map[string]interface{}, pages *Pages, orderBy []string) (*[]T, int64)
- func (e *MySQLBaseDao[T]) SaveOrUpdate(obj interface{}, id int64) error
- func (e *MySQLBaseDao[T]) SaveOrUpdateIdStr(obj interface{}, id string) error
- func (d *MySQLBaseDao[T]) Update(obj *T) error
- func (d *MySQLBaseDao[T]) UpdateByID(id any, updates map[string]any) error
- type MySQLOptions
- type Pages
- type RedisDao
- func (e *RedisDao) Del(key string) error
- func (e *RedisDao) Exists(keys ...string) bool
- func (e *RedisDao) Get(key string) (string, error)
- func (e *RedisDao) GetJson(key string, val interface{}) error
- func (e *RedisDao) GetScan(key string, val interface{}) error
- func (e *RedisDao) ScanKeys(prefix string, limit int64) ([]string, error)
- func (e *RedisDao) Set(key string, val interface{}, expiration time.Duration) error
- func (e *RedisDao) SetJson(key string, val interface{}, expiration time.Duration) error
- type RedisOptions
Constants ¶
const ( DefaultDialTimeout = 10 * time.Second //连接超时 DefaultReadTimeout = 5 * time.Second //读超时 DefaultWriteTimeout = 5 * time.Second //写超时 DefaultPoolSize = 10 //连接池大小 DefaultMinIdleConns = 5 //最小空闲连接 DefaultMaxRetries = 3 //重试次数 )
默认配置常量
Variables ¶
var DBDefaultPageSize = 10
设置缺省分页
Functions ¶
func CloseMongo ¶ added in v0.1.5
func CloseMySQL ¶
func CloseRedis ¶ added in v0.1.6
func CloseRedisCluster ¶ added in v0.2.11
func CloseRedisCluster(cluster *redis.ClusterClient) error
func GormWhere ¶
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 PaginatePage ¶ added in v0.1.5
Types ¶
type MongoOptions ¶ added in v0.1.5
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 RedisDao ¶ added in v0.1.5
type RedisDao struct {
// contains filtered or unexported fields
}