其实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)