当前位置: 首页 > news >正文

golang gorm通过泛型实现通用单表增删改

golang gorm通过泛型实现通用单表增删改

无废话,直接上代码

想实现通用,首先得实现查询的通用,可以用传递map实现

func Where(where map[string]interface{}) func(db *gorm.DB) *gorm.DB {return func(db *gorm.DB) *gorm.DB {dbTmp := dbfor ko, value := range where {koSlice := strings.Split(ko, "#")var key, op stringif len(koSlice) == 1 {key = koif php2go.InArray(key, []string{"select", "order", "offset", "limit"}) {op = ko} else {op = "eq"}} else {key = koSlice[0]op = koSlice[1]}switch op {case "eq":dbTmp.Where(key+" = ?", value)case "neq":dbTmp.Where(key+" != ?", value)case "gt":dbTmp.Where(key+" > ?", value)case "gte":dbTmp.Where(key+" >= ?", value)case "lt":dbTmp.Where(key+" < ?", value)case "lte":dbTmp.Where(key+" <= ?", value)case "like":dbTmp.Where(key+" like ?", "%"+value.(string)+"%")case "in":dbTmp.Where(key+" in ?", value)case "notin":dbTmp.Where(key+" not in ?", value)case "between":dbTmp.Where(key+" between ? and ?", value.([]int)[0], value.([]int)[1])case "betweens":dbTmp.Where(key+" between ? and ?", value.([]string)[0], value.([]string)[1])case "select":dbTmp.Select(value.(string))case "order":dbTmp.Order(value.(string))case "offset":dbTmp.Offset(value.(int))case "limit":dbTmp.Limit(value.(int))}}return dbTmp}
}

调用方式

examleWhere := map[string]interface{}{"name#like":           data.ToUserName,"id":        		   data.UserID,"create_time#between": []int{17000000, 18000000},"grade#in":            []int{3,4},"order":               "id desc",}
Gorm().Table("xxx").Scopes(Where(examleWhere)).xxx打印 sql:
select * from xxx where name like "%xx%" and id = xxx and create_time between 17xxx and 18xxx and grade in (3,4) order by id desc

通用查询

type FieldType interface {uint | uint8 | uint16 | uint32 | uint64 | int | int8 | int16 | int32 | int64 | float32 | float64 | string
}
func Get[T any](where map[string]interface{}, field ...string) (ret T, err error) {db := sys.Gorm().Scopes(Where(where)).Where("delete_time = 0")if len(field) > 0 {db.Select(field)}err = db.Find(&ret).Errorreturn
}func GetVal[T FieldType](tableName string, where map[string]interface{}, field string, includeDel ...bool) (ret T, err error) {db := sys.Gorm().Table(tableName).Scopes(Where(where)).Select(field)if len(includeDel) > 0 && includeDel[0] == true {db.Unscoped()} else {db.Where("delete_time = 0")}err = db.Find(&ret).Errorreturn
}func Gets[T any](where map[string]interface{}, field ...string) (ret []T, err error) {db := sys.Gorm().Scopes(Where(where)).Where("delete_time = 0")if len(field) > 0 {db.Select(field)}err = db.Find(&ret).Errorreturn
}func GetSlice[T FieldType](tableName string, where map[string]interface{}, field string, includeDel ...bool) (ret []T, err error) {ret = make([]T, 0)db := sys.Gorm().Table(tableName).Scopes(Where(where)).Select(field)if len(includeDel) > 0 && includeDel[0] == true {db.Unscoped()} else {db.Where("delete_time = 0")}err = db.Find(&ret).Errorreturn
}func Update[T any](where map[string]interface{}, update T) (err error) {db := sys.Gorm()err = db.Scopes(Where(where)).Updates(update).Errorreturn
}func UpdateByMap(table string, where, update map[string]interface{}) (err error) {db := sys.Gorm().Table(table)err = db.Scopes(Where(where)).Updates(update).Errorreturn
}func UpdateByMapTx(tx *gorm.DB, table string, where, update map[string]interface{}) (err error) {db := tx.Table(table)err = db.Scopes(Where(where)).Updates(update).Errorreturn
}// Create
// doUpdate: on duplicate key update : all, []string{}...
func Create[T any](data *[]T, doUpdate ...string) (err error) {var db *gorm.DBif len(doUpdate) > 0 {if doUpdate[0] == "all" {db = sys.Gorm().Clauses(clause.OnConflict{UpdateAll: true,}).Scopes()} else {db = sys.Gorm().Clauses(clause.OnConflict{DoUpdates: clause.AssignmentColumns(doUpdate),}).Scopes()}} else {db = sys.Gorm().Scopes()}err = db.Create(&data).Errorreturn
}func CreateTx[T any](tx *gorm.DB, data *T, doUpdate ...string) (err error) {if len(doUpdate) > 0 {if doUpdate[0] == "all" {tx.Clauses(clause.OnConflict{UpdateAll: true,}).Scopes()} else {tx.Clauses(clause.OnConflict{DoUpdates: clause.AssignmentColumns(doUpdate),}).Scopes()}}err = tx.Create(&data).Errorreturn
}func CreatesTx[T any](tx *gorm.DB, data *[]T, doUpdate ...string) (err error) {if len(doUpdate) > 0 {if doUpdate[0] == "all" {tx.Clauses(clause.OnConflict{UpdateAll: true,}).Scopes()} else {tx.Clauses(clause.OnConflict{DoUpdates: clause.AssignmentColumns(doUpdate),}).Scopes()}}err = tx.Create(&data).Errorreturn
}

给出调用方式案例

model.Get[model.WwStaff](map[string]interface{}{"corp_id": req.CorpId, "userid": req.StaffUserid})model.GetVal[int]("table_name", map[string]interface{}{"corp_id":     corpId,"dept_id":     deptId,"delete_time": 0,}, "parentid")model.GetSlice[int64]("table_name",map[string]interface{}{"staff_id": req.Id}, "id")// insert ...
model.Create(&tableModel)
// insert ... ON DUPLICATE KEY UPDATE id = VALUES(id) ......
model.Create(&tableModel, "id", "update_time")model.UpdateByMap("table_name",map[string]interface{}{"id#in": roomUnionIds},map[string]interface{}{"upload": 2})model.UpdateByMapTx(tx, "table_name",map[string]interface{}{"id#in": roomUnionIds},map[string]interface{}{"upload": 2})

可以依照代码,写出其它灵活的使用方式
结束

相关文章:

golang gorm通过泛型实现通用单表增删改

golang gorm通过泛型实现通用单表增删改 无废话&#xff0c;直接上代码 想实现通用&#xff0c;首先得实现查询的通用&#xff0c;可以用传递map实现 func Where(where map[string]interface{}) func(db *gorm.DB) *gorm.DB {return func(db *gorm.DB) *gorm.DB {dbTmp : db…...

十、K8S之ConfigMap

ConfigMap 一、概念 在K8S中&#xff0c;ConfigMap是一种用于存储配置数据的API对象&#xff0c;一般用于存储Pod中应用所需的一些配置信息&#xff0c;或者环境变量。将配置于 Pod 分开&#xff0c;避免应为修改配置导致还需要重新构建 镜像与容器。 二、创建 可以使用 ku…...

python飞书群机器人通过webhook发送消息

python飞书群机器人通过webhook发送消息 import json import loggingimport requestslogger logging.getLogger(__name__) logging.basicConfig(levellogging.DEBUG)class FeishuTalk:"""飞书群机器人通过webhook发送消息"""def __init__(self…...

埃隆·马斯克的 AI 聊天机器人 Grok 已经上线

昨天&#xff0c;埃隆马斯克 (Elon Musk) 通过他的公司 xAI 推出了一款名为 Grok 的新型人工智能聊天机器人。这款新的聊天机器人将通过 Twitter 更新实时获取世界知识&#xff0c;使其成为最新的对话 AI 系统。 Grok 的独特和基本优势在于它可以通过 &#x1d54f; 平台实时了…...

【代码随想录】算法训练营 第十五天 第六章 二叉树 Part 2

102. 二叉树的层序遍历 层序遍历&#xff0c;就是一层一层地遍历二叉树&#xff0c;最常见的就是从上到下&#xff0c;从左到右来遍历&#xff0c;遍历的方法依然有两种&#xff0c;第一种是借助队列&#xff0c;第二种则是递归&#xff0c;都算是很简单、很容易理解的方法&am…...

使用ssl_certificate_by_lua指令动态加载证书

1、下载 OpenResty - 下载 根据自己系统选择下载&#xff0c;我的是64位 2、解压到目录 3、启动openresty 进入解压后的目录&#xff0c;执行nginx.exe 浏览器输入 http://localhost 查看是否正常。显示以下画面就表示没有问题。 接下来可以开始准备动态安装证书 4、使用o…...

Qt中Opencv转Qimage出现重影或者颜色不对

废话不多说 在qt中opencv获取的图像转qimage时出现重影原因&#xff1a; 图像数据的内存对齐可能会导致画面重影&#xff0c;如果出现误差转换出来的图就会出现重影 解决办法&#xff1a; cv::Mat image_bgr cv::imread(“example.jpg”); cv::Mat image_aligned; cv::copyMak…...

upload-labs-1

文章目录 Pass-01 Pass-01 先上传一个正常的图片&#xff0c;查看返回结果&#xff0c;结果中带有文件上传路径&#xff0c;可以进行利用&#xff1a; 上传一个恶意的webshell&#xff0c;里面写入一句话木马&#xff1a; <?php eval($_POST[cmd]); echo "hello&quo…...

【vite配置路径别名@】/启动配置

npm install types/node --save-dev npm install path --save import { defineConfig } from vite import vue from vitejs/plugin-vue // 配置别名 import { resolve } from "path";// https://vitejs.dev/config/ export default defineConfig({plugins: [vue()]…...

3. List

数据结构在Java集合中的对应关系 线性表【数组】 -> ArrayList 线性表【链表】-> LinkedList 队列 -> Queue -> LinkedList&#xff0c;PriorityQueue, ArrayBlockingQueue … etc. 双端队列 -> Deque -> ArrayDeque 栈 -> LinkedList 哈希表 -> Hash…...

Django初窥门径-oauth登录认证

引言 在现代Web应用程序中&#xff0c;用户身份验证和授权是至关重要的组成部分。Django&#xff0c;一个流行的Python Web框架&#xff0c;为用户身份验证提供了内置支持。本文将探讨如何创建和注册Django应用&#xff0c;自定义身份验证服务&#xff0c;配置AUTHENTICATION_…...

数学到底在哪里支撑着编程?

数学到底在哪里支撑着编程&#xff1f; 除了少数算法等明显相关情况外&#xff0c;说点日常的。 编程是个极度依赖逻辑的领域&#xff0c;逻辑严谨性好&#xff0c;你的编程工作会顺畅很多一-绝大多 数的bug都是最近很多小伙伴找我&#xff0c;说想要一些嵌入式的资料&#x…...

Python模块ADB的, 已经 pyadb

Python模块ADB的使用指南_笔记大全_设计学院 (python100.com) pip install adb Python 调用ADB_python 调用adb命令_实相实相的博客-CSDN博客 Python ADB.shell_command Examples, pyadb.ADB.shell_command Python Examples - HotExamples Gitee 极速下载/PyADB - 码云 - 开…...

猫头虎分享从Python到JavaScript传参数:多面手的数据传递术

&#x1f337;&#x1f341; 博主猫头虎 带您 Go to New World.✨&#x1f341; &#x1f984; 博客首页——猫头虎的博客&#x1f390; &#x1f433;《面试题大全专栏》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33a; &a…...

注解汇总:Spring 常用的注解

前言 本栏目的内容已经讲完了&#xff0c;本案例将把案例中所有讲到的注解都汇总起来&#xff0c;方便日后的学习需要用到的时候能够快速的找到相应的注解。本案例将结合小案例一起做汇总&#xff0c;也想丹玉是再复习一遍讲过用过的注解。 一、注解汇总 1、Component Reposi…...

合肥工业大学操作系统实验5

✅作者简介:CSDN内容合伙人、信息安全专业在校大学生🏆 🔥系列专栏 :hfut实验课设 📃新人博主 :欢迎点赞收藏关注,会回访! 💬舞台再大,你不上台,永远是个观众。平台再好,你不参与,永远是局外人。能力再大,你不行动,只能看别人成功!没有人会关心你付出过多少…...

基于SpringBoot+Vue的点餐管理系统

基于springbootvue的点餐平台网站系统的设计与实现~ 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringBootMyBatisVue工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 菜品详情 个人中心 订单 管理员界面 菜品管理 摘要 点餐管理系统是一种用…...

C# 继承,抽象,接口,泛型约束,扩展方法

文章目录 前言模拟需求场景模拟重复性高的需求初始类结构继承优化抽象类 需求1&#xff1a;打印CreateTime方法1&#xff1a;使用重载方法2&#xff1a;基类函数方法3&#xff1a;泛型约束方法3.1&#xff1a;普通泛型方法方法3.2&#xff1a;高级泛型约束&#xff0c;扩展方法…...

mysql的备份和恢复

备份&#xff1a;完全备份 增量备份 完全备份&#xff1a;将整个数据库完整的进行备份 增量备份&#xff1a;在完全备份的基础之上&#xff0c;对后续新增的内容进行备份 备份的需求 1、在生产环境中&#xff0c;数据的安全至关重要&#xff0c;任何数据的都可能产生非常严重…...

【机器学习3】有监督学习经典分类算法

1 支持向量机 在现实世界的机器学习领域&#xff0c; SVM涵盖了各个方面的知识&#xff0c; 也是面试题目中常见的基础模型。 SVM的分类结果仅依赖于支持向量&#xff0c;对于任意线性可分的两组点&#xff0c;它 们在SVM分类的超平面上的投影都是线性不可分的。 2逻辑回归 …...

达摩院CAM++说话人识别模型实战:5分钟搞定Modelscope部署与中文测试

达摩院CAM说话人识别模型实战&#xff1a;5分钟搞定Modelscope部署与中文测试 语音技术正在重塑人机交互的边界&#xff0c;而说话人识别作为其中的核心能力之一&#xff0c;已经从实验室走向工业级应用。达摩院开源的CAM模型以其卓越的准确率和计算效率&#xff0c;正在成为开…...

Mybatis @MapKey注解:高效实现List到Map的转换技巧

1. 为什么需要List转Map&#xff1f; 在实际开发中&#xff0c;我们经常会遇到这样的场景&#xff1a;从数据库查询出一批数据后&#xff0c;需要根据某个字段快速查找对应的记录。比如查询用户列表后&#xff0c;需要根据用户ID快速获取用户信息。这时候&#xff0c;把List转换…...

终极开源数据标注工具:Label Studio完整使用指南

终极开源数据标注工具&#xff1a;Label Studio完整使用指南 【免费下载链接】label-studio Label Studio is a multi-type data labeling and annotation tool with standardized output format 项目地址: https://gitcode.com/GitHub_Trending/la/label-studio 在当今…...

实战应用:基于快马平台构建项目级UI颜色规范管理工具

今天想和大家分享一个最近在项目中用到的实用工具——基于InsCode(快马)平台搭建的UI颜色规范管理系统。作为一个经常要和设计系统打交道的前端开发者&#xff0c;我发现在团队协作中&#xff0c;颜色代码的管理常常是个痛点&#xff0c;这次尝试用快马平台快速实现了一个解决方…...

解锁浏览器超能力:Greasy Fork用户脚本平台完全指南

解锁浏览器超能力&#xff1a;Greasy Fork用户脚本平台完全指南 【免费下载链接】greasyfork An online repository of user scripts. 项目地址: https://gitcode.com/gh_mirrors/gr/greasyfork 认知启蒙&#xff1a;重新认识浏览器脚本的价值 还在为浏览器功能不足烦恼…...

基于 N-gram 全新模型:嵌入扩展新范式,实现轻量化 MoE 高效进化

在技术领域&#xff0c;我们常常被那些闪耀的、可见的成果所吸引。今天&#xff0c;这个焦点无疑是大语言模型技术。它们的流畅对话、惊人的创造力&#xff0c;让我们得以一窥未来的轮廓。然而&#xff0c;作为在企业一线构建、部署和维护复杂系统的实践者&#xff0c;我们深知…...

实用指南:使用applera1n安全绕过iOS 15-16激活锁的完整教程

实用指南&#xff1a;使用applera1n安全绕过iOS 15-16激活锁的完整教程 【免费下载链接】applera1n icloud bypass for ios 15-16 项目地址: https://gitcode.com/gh_mirrors/ap/applera1n iOS设备的激活锁是Apple保护用户隐私的重要安全功能&#xff0c;但当您忘记Appl…...

OpenClaw(龙虾)WSL Ubuntu 环境安装配置完整指南

从零开始在 WSL Ubuntu 中安装 OpenClaw&#xff0c;配置公司中转站&#xff0c;解决各种问题&#xff0c;最终成功运行。目录环境准备OpenClaw 安装配置文件详解常见问题与解决方案日常使用指南完整配置模板一、环境准备1.1 确认 WSL Ubuntu 环境# 查看 Ubuntu 版本 lsb_relea…...

Leantime容器化部署实战指南:从环境搭建到生产运维

Leantime容器化部署实战指南&#xff1a;从环境搭建到生产运维 【免费下载链接】docker-leantime Official Docker Image for Leantime https://leantime.io 项目地址: https://gitcode.com/gh_mirrors/do/docker-leantime 环境准备&#xff1a;部署前的必要检查 系统兼…...

Automerge与区块链技术结合:构建去中心化数据协作的终极指南

Automerge与区块链技术结合&#xff1a;构建去中心化数据协作的终极指南 【免费下载链接】automerge A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically. 项目地址: https://gitcode.com/gh_mirr…...