在go1.18以前我们实现不同表格的增删改查,需要新建很多struct 之后绑定指针类型方法,并且所有不同的表都得重写一遍例如这样;
type UserDao struct {
BaseDao
}
func (ud *UserDao) GetInfo(tx *gorm.DB, where UserCommonWhere) (info *CompanyWechatUser, err error) {
info = new(CompanyWechatUser)
tx = tx.Table(CompanyWechatUser{}.TableName())
err = ud.CommonWhere(tx, where).Find(&info).Error
return info, err
}
// GetList
func (ud *UserDao) GetList(tx *gorm.DB, where UserCommonWhere) (list []CompanyWechatUser, err error) {
tx = tx.Table(CompanyWechatUser{}.TableName())
err = ud.CommonWhere(tx, where).Find(&list).Error
return list, err
}
func (ud *UserDao) CommonWhere(tx *gorm.DB, where UserCommonWhere) *gorm.DB {
if where.ID > 0 {
tx = tx.Where("id = ?", where.ID)
}
if where.UserId != "" {
tx = tx.Where("user_id = ?", where.UserId)
}
if where.OrderBy != "" {
tx = tx.Order(where.OrderBy)
}
return tx
}
如果有100个表,没有泛型支持这里的代码需要写100遍,特别的麻烦

最低0.47元/天 解锁文章
764

被折叠的 条评论
为什么被折叠?



