goframe 之ORM链式封装

其实goframe本身的ORM框架已经比较强大了,只是在此基础上能否进行简写就好了,例如不要手写表字段,因为容易写错,第二个,要自行判断值是否为空,是否需要进行拼接,比较麻烦,要写多行,有没有一种方式直接写一行的?答案是有的,还是希望框架本身后期能够支持这种方式。

直接上代码:

import (
	"github.com/gogf/gf/v2/database/gdb"
	"skynet/utility/lang"
)

type orm struct {
    //这块可以使用匿名即可,这样就可以支持原生的方法了,我不建议这样做,因为以后如果要换框架比较麻烦
	model *gdb.Model
}

// NewOrm
//
//	@Author  zhaosy
//	@Description: 新建ORM工具
//	@date  2024-07-15 16:32:18
func NewOrm(m *gdb.Model) *orm {
	//设置安全默认为false,因为这个是流程链,所以进行关闭,如果不关闭,每次调用链式时框架都会返回一个克隆版本出来
	m.Safe(false)
	o := &orm{model: m}
	return o
}

// PageScanData
//
//	@Author  zhaosy
//	@Description: 分页,同时扫描数据和count
//	@date  2024-07-15 16:32:02
func (o *orm) PageScanData(data interface{}, count *int, where ...interface{}) error {
	//必须在第一行
	c, err := o.model.Count(where)
	//扫描数据
	o.model.Scan(data, where)

	if err != nil {
		return err
	}
	*count = c
	return nil
}

// Eq
//
//	@Author  zhaosy
//	@Description: Eq 相等
//	@date  2024-07-15 16:31:57
func (o *orm) Eq(columnName string, value interface{}) *orm {
	return o.EqCond(lang.IsNotEmpty(value), columnName, value)
}

// EqCond 相等,需要传入生效条件
func (o *orm) EqCond(condition bool, cloumName string, value interface{}) *orm {
	if condition {
		o.model.Where(cloumName, value)
	}
	return o
}

// Ne
//
//	@Author  zhaosy
//	@Description: Ne 不相等  ,需要传入生效条件
//	@date  2024-07-15 16:31:47
func (o *orm) Ne(columnName string, value interface{}) *orm {
	return o.NeCond(lang.IsNotEmpty(value), columnName, value)
}

// NeCond
//
//	@Author  zhaosy
//	@Description: NeCond 不相等  ,需要传入生效条件
//	@date  2024-07-15 16:31:41
func (o *orm) NeCond(condition bool, columnName string, value interface{}) *orm {
	if condition {
		o.model.WhereNot(columnName, value)
	}
	return o
}

// LikeRight
//
//	@Author  zhaosy
//	@Description: LikeRight 右模糊
//	@date  2024-07-15 16:31:34
func (o *orm) LikeRight(columnName string, value interface{}) *orm {
	if lang.IsNotEmpty(value) {
		o.model.WhereLike(columnName, value.(string)+"%")
	}
	return o
}

// LikeLeft
//
//	@Author  zhaosy
//	@Description: LikeLeft 左模糊
//	@date  2024-07-15 16:31:29
func (o *orm) LikeLeft(cloumName string, value interface{}) *orm {
	if lang.IsNotEmpty(value) {
		o.model.WhereLike(cloumName, "%"+value.(string))
	}
	return o
}

// Like
//
//	@Author  zhaosy
//	@Description: Like,全模糊
//	@date  2024-07-15 16:26:47
func (o *orm) Like(cloumName string, value interface{}) *orm {
	if lang.IsNotEmpty(value) {
		o.model.WhereLike(cloumName, "%"+value.(string)+"%")
	}
	return o
}


// Line 换行符, 采用("",
// "")方式换行,防止一行过长问题
func (o *orm) Line(string, string) *orm {
	return o
}

//其他的根据需要自行创建新方法即可
......

测试:

func (c *ControllerV1) DeviceList(ctx context.Context, req *v1.DeviceListReq) (res *result.ResultRes, err error) {
	//r := ghttp.RequestFromCtx(ctx) //获取request对象
	//r := g.RequestFromCtx(ctx)
	list := []do.DeviceInfo{}
	columns := dao.DeviceInfo.Columns()
	count := 0
	//进行分页查询
	ormutil.NewOrm(dao.DeviceInfo.Ctx(ctx)).Eq(columns.TableName, req.TableName).Like(columns.BusinessId, req.BusinessId).LikeRight(columns.DeviceName, req.DeviceName).PageScanData(&list, &count)

	return result.Success(req.PageRecords(list, count)), nil

}

类似这样,一行代码直接搞定,简单明了,也比较清爽

//进行分页查询
	ormutil.NewOrm(dao.DeviceInfo.Ctx(ctx)).Eq(columns.TableName, req.TableName).Like(columns.BusinessId, req.BusinessId).LikeRight(columns.DeviceName, req.DeviceName).PageScanData(&list, &count)

换行:

	//进行分页查询
	ormutil.NewOrm(dao.DeviceInfo.Ctx(ctx)).Eq(columns.TableName, req.TableName).Like(columns.BusinessId, "6").LikeRight(columns.DeviceName, req.DeviceName).Line("",
		"").LikeRight(columns.DeviceName, req.DeviceName).LikeRight(columns.DeviceName, req.DeviceName).LikeRight(columns.DeviceName, req.DeviceName).Line("",
		"").LikeRight(columns.DeviceName, req.DeviceName).LikeRight(columns.DeviceName, req.DeviceName).LikeRight(columns.DeviceName, req.DeviceName).PageScanData(&list, &count)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值