升级BaseDao

This commit is contained in:
白茶清欢 2023-02-09 14:21:26 +08:00
parent 5e21219974
commit 6ac24e2236

44
base.go
View File

@ -11,7 +11,13 @@ import (
"gorm.io/gorm"
)
// BaseDao 基础dao层
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:13 2023/2/9
type BaseDao struct {
TableName string // 继承BaseDao需要指定表名后续调用不用传递表名进来
}
// Create 创建新的数据
@ -19,8 +25,8 @@ type BaseDao struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:06 下午 2021/8/8
func (b *BaseDao) Create(dbInstance *gorm.DB, table string, data interface{}) error {
return dbInstance.Table(table).Create(data).Error
func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}) error {
return dbInstance.Table(b.TableName).Create(data).Error
}
// Update 更新数据
@ -28,9 +34,9 @@ func (b *BaseDao) Create(dbInstance *gorm.DB, table string, data interface{}) er
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:12 下午 2021/8/8
func (b *BaseDao) Update(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
r := dbInstance.Updates(updateDate)
func (b *BaseDao) Update(dbInstance *gorm.DB, updateDate interface{}, optionFuncList ...SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
r := dbInstance.Table(b.TableName).Updates(updateDate)
return r.RowsAffected, r.Error
}
@ -39,9 +45,9 @@ func (b *BaseDao) Update(dbInstance *gorm.DB, table string, updateDate interface
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:14 下午 2021/8/8
func (b *BaseDao) List(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error {
dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
return dbInstance.Find(result).Error
func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.Table(b.TableName).Find(result).Error
}
// Delete 删除数据, 硬删除, 对应 delete语句
@ -49,9 +55,9 @@ func (b *BaseDao) List(dbInstance *gorm.DB, table string, result interface{}, op
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:46 2023/2/9
func (b *BaseDao) Delete(dbInstance *gorm.DB, table string, dataModel interface{}, optionFuncList ...SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
return dbInstance.Delete(dataModel).RowsAffected, dbInstance.Delete(dataModel).Error
func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel interface{}, optionFuncList ...SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.Table(b.TableName).Delete(dataModel).RowsAffected, dbInstance.Delete(dataModel).Error
}
// Detail 查询详情
@ -59,9 +65,9 @@ func (b *BaseDao) Delete(dbInstance *gorm.DB, table string, dataModel interface{
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error {
dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
return dbInstance.First(result).Error
func (b *BaseDao) Detail(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.Table(b.TableName).First(result).Error
}
// Count 查询数量
@ -69,10 +75,10 @@ func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, result interface{},
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Count(dbInstance *gorm.DB, table string, optionFuncList ...SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
func (b *BaseDao) Count(dbInstance *gorm.DB, optionFuncList ...SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
var cnt int64
return cnt, dbInstance.Table(table).Count(&cnt).Error
return cnt, dbInstance.Table(b.TableName).Count(&cnt).Error
}
// Tx 执行事务
@ -124,9 +130,9 @@ func (b *BaseDao) Rollback(db *gorm.DB) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:38 2022/5/15
func (b *BaseDao) setTxCondition(tx *gorm.DB, table string, optionFuncList ...SetOption) *gorm.DB {
func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm.DB {
// 指定查询的表
tx = tx.Table(table)
tx = tx.Table(b.TableName)
// 构建查询条件
o := &Option{}