repository

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseRepo

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

BaseRepo 基础仓库实现

func NewBaseRepo

func NewBaseRepo[T any](db *gorm.DB) *BaseRepo[T]

NewBaseRepo 创建基础仓库

func (*BaseRepo[T]) Count

func (r *BaseRepo[T]) Count(ctx context.Context) (int64, error)

Count 统计数量

func (*BaseRepo[T]) CountWhere

func (r *BaseRepo[T]) CountWhere(ctx context.Context, query string, args ...any) (int64, error)

CountWhere 条件统计

func (*BaseRepo[T]) Create

func (r *BaseRepo[T]) Create(ctx context.Context, model *T) error

Create 创建记录

func (*BaseRepo[T]) CreateBatch

func (r *BaseRepo[T]) CreateBatch(ctx context.Context, models []T) error

CreateBatch 批量创建

func (*BaseRepo[T]) Delete

func (r *BaseRepo[T]) Delete(ctx context.Context, id uint) error

Delete 删除记录(软删除)

func (*BaseRepo[T]) DeleteBatch

func (r *BaseRepo[T]) DeleteBatch(ctx context.Context, ids []uint) error

DeleteBatch 批量删除

func (*BaseRepo[T]) Exists

func (r *BaseRepo[T]) Exists(ctx context.Context, id uint) (bool, error)

Exists 检查是否存在

func (*BaseRepo[T]) ExistsWhere

func (r *BaseRepo[T]) ExistsWhere(ctx context.Context, query string, args ...any) (bool, error)

ExistsWhere 条件检查是否存在

func (*BaseRepo[T]) FindAll

func (r *BaseRepo[T]) FindAll(ctx context.Context) ([]T, error)

FindAll 查询所有记录

func (*BaseRepo[T]) FindAllWithDeleted

func (r *BaseRepo[T]) FindAllWithDeleted(ctx context.Context) ([]T, error)

FindAllWithDeleted 查询所有记录(包括软删除)

func (*BaseRepo[T]) FindByID

func (r *BaseRepo[T]) FindByID(ctx context.Context, id uint) (*T, error)

FindByID 根据 ID 查询

func (*BaseRepo[T]) FindByIDs

func (r *BaseRepo[T]) FindByIDs(ctx context.Context, ids []uint) ([]T, error)

FindByIDs 批量查询

func (*BaseRepo[T]) FindDeleted

func (r *BaseRepo[T]) FindDeleted(ctx context.Context) ([]T, error)

FindDeleted 查询已软删除的记录

func (*BaseRepo[T]) FindLimited

func (r *BaseRepo[T]) FindLimited(ctx context.Context, limit int) ([]T, error)

FindLimited 查询指定数量记录

func (*BaseRepo[T]) FindOne

func (r *BaseRepo[T]) FindOne(ctx context.Context, query string, args ...any) (*T, error)

FindOne 条件查询单条记录

func (*BaseRepo[T]) FindOrdered

func (r *BaseRepo[T]) FindOrdered(ctx context.Context, order string, limit int) ([]T, error)

FindOrdered 查询并排序

func (*BaseRepo[T]) FindPage

func (r *BaseRepo[T]) FindPage(ctx context.Context, page, pageSize int) (*PageResult[T], error)

FindPage 分页查询

func (*BaseRepo[T]) FindPageOrdered

func (r *BaseRepo[T]) FindPageOrdered(ctx context.Context, page, pageSize int, order string) (*PageResult[T], error)

FindPageOrdered 分页查询并排序

func (*BaseRepo[T]) FindPageWhere

func (r *BaseRepo[T]) FindPageWhere(ctx context.Context, page, pageSize int, query string, args ...any) (*PageResult[T], error)

FindPageWhere 条件分页查询

func (*BaseRepo[T]) FindPageWhereOrdered

func (r *BaseRepo[T]) FindPageWhereOrdered(ctx context.Context, page, pageSize int, query string, args []any, order string) (*PageResult[T], error)

FindPageWhereOrdered 条件分页查询并排序

func (*BaseRepo[T]) FindWhere

func (r *BaseRepo[T]) FindWhere(ctx context.Context, query string, args ...any) ([]T, error)

FindWhere 条件查询多条记录

func (*BaseRepo[T]) FindWhereOrdered

func (r *BaseRepo[T]) FindWhereOrdered(ctx context.Context, query string, args []any, order string) ([]T, error)

FindWhereOrdered 条件查询并排序

func (*BaseRepo[T]) GetDB

func (r *BaseRepo[T]) GetDB() *gorm.DB

GetDB 获取数据库实例

func (*BaseRepo[T]) HardDelete

func (r *BaseRepo[T]) HardDelete(ctx context.Context, id uint) error

HardDelete 硬删除记录(物理删除)

func (*BaseRepo[T]) HardDeleteBatch

func (r *BaseRepo[T]) HardDeleteBatch(ctx context.Context, ids []uint) error

HardDeleteBatch 批量硬删除

func (*BaseRepo[T]) NewQueryBuilder

func (r *BaseRepo[T]) NewQueryBuilder() *QueryBuilder[T]

NewQueryBuilder 创建查询构建器

func (*BaseRepo[T]) Restore

func (r *BaseRepo[T]) Restore(ctx context.Context, id uint) error

Restore 恢复软删除记录

func (*BaseRepo[T]) RestoreBatch

func (r *BaseRepo[T]) RestoreBatch(ctx context.Context, ids []uint) error

RestoreBatch 批量恢复软删除记录

func (*BaseRepo[T]) Update

func (r *BaseRepo[T]) Update(ctx context.Context, model *T) error

Update 更新记录

func (*BaseRepo[T]) UpdateBatch

func (r *BaseRepo[T]) UpdateBatch(ctx context.Context, ids []uint, field string, value any) error

UpdateBatch 批量更新(指定字段)

func (*BaseRepo[T]) WithTransaction

func (r *BaseRepo[T]) WithTransaction(ctx context.Context, fn func(txRepo *BaseRepo[T]) error) error

WithTransaction 在事务中执行操作

type BaseRepository

type BaseRepository[T any] interface {
	// FindByID 根据 ID 查询
	FindByID(ctx context.Context, id uint) (*T, error)
	// Create 创建记录
	Create(ctx context.Context, model *T) error
	// Update 更新记录
	Update(ctx context.Context, model *T) error
	// Delete 删除记录(软删除)
	Delete(ctx context.Context, id uint) error
	// FindByIDs 批量查询
	FindByIDs(ctx context.Context, ids []uint) ([]T, error)
}

BaseRepository 基础仓库接口

type PageResult

type PageResult[T any] struct {
	Items    []T   `json:"items"`
	Total    int64 `json:"total"`
	Page     int   `json:"page"`
	PageSize int   `json:"page_size"`
}

PageResult 分页结果

type QueryBuilder

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

QueryBuilder 链式查询构建器

func (*QueryBuilder[T]) Count

func (qb *QueryBuilder[T]) Count(ctx context.Context) (int64, error)

Count 执行统计

func (*QueryBuilder[T]) Find

func (qb *QueryBuilder[T]) Find(ctx context.Context) ([]T, error)

Find 执行查询

func (*QueryBuilder[T]) First

func (qb *QueryBuilder[T]) First(ctx context.Context) (*T, error)

First 执行查询并返回第一条

func (*QueryBuilder[T]) Limit

func (qb *QueryBuilder[T]) Limit(limit int) *QueryBuilder[T]

Limit 设置数量限制

func (*QueryBuilder[T]) Offset

func (qb *QueryBuilder[T]) Offset(offset int) *QueryBuilder[T]

Offset 设置偏移量

func (*QueryBuilder[T]) Or

func (qb *QueryBuilder[T]) Or(query string, args ...any) *QueryBuilder[T]

Or 添加 OR 条件

func (*QueryBuilder[T]) Order

func (qb *QueryBuilder[T]) Order(order string) *QueryBuilder[T]

Order 设置排序

func (*QueryBuilder[T]) Page

func (qb *QueryBuilder[T]) Page(ctx context.Context, page, pageSize int) (*PageResult[T], error)

Page 执行分页查询

func (*QueryBuilder[T]) Where

func (qb *QueryBuilder[T]) Where(query string, args ...any) *QueryBuilder[T]

Where 添加条件

Jump to

Keyboard shortcuts

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