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)

相关推荐

  1. goframe ORM封装

    2024-07-15 19:56:03       23 阅读
  2. ormSQLAlchemy

    2024-07-15 19:56:03       37 阅读
  3. 深度学习矩阵形式的法则推导

    2024-07-15 19:56:03       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 19:56:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 19:56:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 19:56:03       58 阅读
  4. Python语言-面向对象

    2024-07-15 19:56:03       69 阅读

热门阅读

  1. 高通平台android的Framework开发遇到的一些问题总结

    2024-07-15 19:56:03       20 阅读
  2. 第六章 动画【Android基础学习】

    2024-07-15 19:56:03       18 阅读
  3. 【爬虫】爬虫基础

    2024-07-15 19:56:03       19 阅读
  4. CSS 技巧与案例详解:开篇介绍

    2024-07-15 19:56:03       21 阅读
  5. 力扣刷题之2732.找到矩阵中的好子集

    2024-07-15 19:56:03       21 阅读
  6. golang基础用法

    2024-07-15 19:56:03       18 阅读
  7. shell脚本传参调用http接口

    2024-07-15 19:56:03       17 阅读
  8. JsonCPP源码分析——分配器和配置器

    2024-07-15 19:56:03       15 阅读