创建支持指定表名

This commit is contained in:
白茶清欢 2023-04-06 22:12:35 +08:00
parent c2548cd930
commit abdfc23a0e
1 changed files with 11 additions and 3 deletions

14
base.go
View File

@ -17,7 +17,7 @@ import (
// //
// Date : 14:13 2023/2/9 // Date : 14:13 2023/2/9
type BaseDao struct { type BaseDao struct {
TableName string // 继承BaseDao需要指定表名后续调用不用传递表名进来 TableName string // 继承BaseDao需要指定表名后续调用不用传递表名进来如果分表了使用SetOption设置表名, 这里的优先级更高
} }
// Create 创建新的数据 // Create 创建新的数据
@ -25,8 +25,16 @@ type BaseDao struct {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 8:06 下午 2021/8/8 // Date : 8:06 下午 2021/8/8
func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}) error { func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}, optionList ...SetOption) error {
return dbInstance.Create(data).Error o := &Option{}
for _, itemFunc := range optionList {
itemFunc(o)
}
tableName := b.TableName
if len(o.Table) > 0 {
tableName = o.Table
}
return dbInstance.Table(tableName).Create(data).Error
} }
// Update 更新数据 // Update 更新数据