123456789101112131415161718192021222324 |
- package helper
- import "gorm.io/gorm"
- // QueryByConditions 通用多条查询
- func QueryByCondition[T any](db *gorm.DB, conditions map[string]interface{}) ([]T, error) {
- var results []T
- tx := db.Where(conditions).Find(&results)
- return results, tx.Error
- }
- // QueryByConditions 分页查询
- func QueryByConditions[T any](db *gorm.DB, conditions map[string]interface{}, pageNum, pageSize int) ([]T, int64, error) {
- var results []T
- var total int64
- tx := db.Where(conditions).Model(&results).Count(&total)
- if tx.Error != nil {
- return nil, 0, tx.Error
- }
- tx = db.Where(conditions).Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&results)
- return results, total, tx.Error
- }
|