更新mysql基础操作

This commit is contained in:
白茶清欢 2022-07-19 12:17:26 +08:00
parent 77df474a4f
commit 2fb098b469
1 changed files with 16 additions and 15 deletions

31
base.go
View File

@ -27,7 +27,7 @@ func (b *BaseDao) Create(dbInstance *gorm.DB, table string, data interface{}) er
// //
// Date : 8:12 下午 2021/8/8 // Date : 8:12 下午 2021/8/8
func (b *BaseDao) Update(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) (int64, error) { func (b *BaseDao) Update(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) (int64, error) {
b.setTxCondition(dbInstance, table, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
r := dbInstance.Updates(updateDate) r := dbInstance.Updates(updateDate)
return r.RowsAffected, r.Error return r.RowsAffected, r.Error
} }
@ -38,7 +38,7 @@ func (b *BaseDao) Update(dbInstance *gorm.DB, table string, updateDate interface
// //
// Date : 8:14 下午 2021/8/8 // Date : 8:14 下午 2021/8/8
func (b *BaseDao) List(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error { func (b *BaseDao) List(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error {
b.setTxCondition(dbInstance, table, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
return dbInstance.Find(result).Error return dbInstance.Find(result).Error
} }
@ -48,7 +48,7 @@ func (b *BaseDao) List(dbInstance *gorm.DB, table string, result interface{}, op
// //
// Date : 8:25 下午 2021/8/8 // Date : 8:25 下午 2021/8/8
func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error { func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error {
b.setTxCondition(dbInstance, table, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
return dbInstance.First(result).Error return dbInstance.First(result).Error
} }
@ -58,7 +58,7 @@ func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, result interface{},
// //
// Date : 8:25 下午 2021/8/8 // Date : 8:25 下午 2021/8/8
func (b *BaseDao) Count(dbInstance *gorm.DB, table string, optionFuncList ...SetOption) (int64, error) { func (b *BaseDao) Count(dbInstance *gorm.DB, table string, optionFuncList ...SetOption) (int64, error) {
b.setTxCondition(dbInstance, table, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, table, optionFuncList...)
var cnt int64 var cnt int64
return cnt, dbInstance.Table(table).Count(&cnt).Error return cnt, dbInstance.Table(table).Count(&cnt).Error
} }
@ -95,9 +95,9 @@ func (b *BaseDao) Rollback(db *gorm.DB) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:38 2022/5/15 // Date : 17:38 2022/5/15
func (b *BaseDao) setTxCondition(tx *gorm.DB, table string, optionFuncList ...SetOption) { func (b *BaseDao) setTxCondition(tx *gorm.DB, table string, optionFuncList ...SetOption) *gorm.DB {
// 指定查询的表 // 指定查询的表
tx.Table(table) tx = tx.Table(table)
// 构建查询条件 // 构建查询条件
o := &Option{} o := &Option{}
@ -107,54 +107,55 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, table string, optionFuncList ...Se
// 设置where条件 // 设置where条件
if nil != o.Where && len(o.Where) > 0 { if nil != o.Where && len(o.Where) > 0 {
tx.Where(o.Where) tx = tx.Where(o.Where)
} }
// 设置 limit offset // 设置 limit offset
if o.Limit > 0 { if o.Limit > 0 {
tx.Limit(o.Limit) tx = tx.Limit(o.Limit)
} }
if o.Offset >= 0 { if o.Offset >= 0 {
tx.Offset(o.Offset) tx = tx.Offset(o.Offset)
} }
// in 语句 // in 语句
if nil != o.In { if nil != o.In {
tx.Where(o.In) tx = tx.Where(o.In)
} }
// not in 语句 // not in 语句
if nil != o.NotIn { if nil != o.NotIn {
for field, value := range o.NotIn { for field, value := range o.NotIn {
tx.Where(field+" NOT IN ? ", value) tx = tx.Where(field+" NOT IN ? ", value)
} }
} }
// like 语句 // like 语句
if nil != o.Like { if nil != o.Like {
for field, value := range o.Like { for field, value := range o.Like {
tx.Where(field+" LIKE ? ", "%"+value+"%") tx = tx.Where(field+" LIKE ? ", "%"+value+"%")
} }
} }
// NOT LIKE 语句 // NOT LIKE 语句
if nil != o.NotLike { if nil != o.NotLike {
for field, value := range o.NotLike { for field, value := range o.NotLike {
tx.Where(field+" NOT LIKE ? ", "%"+value+"%") tx = tx.Where(field+" NOT LIKE ? ", "%"+value+"%")
} }
} }
// >= // >=
if nil != o.Start { if nil != o.Start {
for field, value := range o.Start { for field, value := range o.Start {
tx.Where(field+" >= ?", value) tx = tx.Where(field+" >= ?", value)
} }
} }
// < // <
if nil != o.End { if nil != o.End {
for field, value := range o.End { for field, value := range o.End {
tx.Where(field+" < ?", value) tx = tx.Where(field+" < ?", value)
} }
} }
return tx
} }