Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
cc4191eb
提交
cc4191eb
authored
10月 15, 2025
作者:
mooncake
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
d49535f2
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
33 行增加
和
5 行删除
+33
-5
odao.go
xcommon/odao/odao.go
+33
-5
没有找到文件。
xcommon/odao/odao.go
浏览文件 @
cc4191eb
...
...
@@ -172,7 +172,7 @@ func (x *ODao[T]) DeleteByIDsTx(ctx context.Context, tx *gorm.DB, ids []xsf.ID)
return
nil
}
func
(
x
*
ODao
[
T
])
UpdateByID
(
ctx
context
.
Context
,
id
xsf
.
ID
,
tb
*
T
)
error
{
func
(
x
*
ODao
[
T
])
UpdateByID
(
ctx
context
.
Context
,
id
xsf
.
ID
,
tb
any
)
error
{
if
id
<=
0
{
return
nil
}
...
...
@@ -182,7 +182,7 @@ func (x *ODao[T]) UpdateByID(ctx context.Context, id xsf.ID, tb *T) error {
return
nil
}
func
(
x
*
ODao
[
T
])
UpdateByIDSilent
(
ctx
context
.
Context
,
id
xsf
.
ID
,
tb
*
T
)
error
{
func
(
x
*
ODao
[
T
])
UpdateByIDSilent
(
ctx
context
.
Context
,
id
xsf
.
ID
,
tb
any
)
error
{
if
id
<=
0
{
return
nil
}
...
...
@@ -192,7 +192,7 @@ func (x *ODao[T]) UpdateByIDSilent(ctx context.Context, id xsf.ID, tb *T) error
return
nil
}
func
(
x
*
ODao
[
T
])
UpdateByIDs
(
ctx
context
.
Context
,
ids
[]
xsf
.
ID
,
tb
*
T
)
error
{
func
(
x
*
ODao
[
T
])
UpdateByIDs
(
ctx
context
.
Context
,
ids
[]
xsf
.
ID
,
tb
any
)
error
{
if
len
(
ids
)
==
0
{
return
nil
}
...
...
@@ -202,7 +202,7 @@ func (x *ODao[T]) UpdateByIDs(ctx context.Context, ids []xsf.ID, tb *T) error {
return
nil
}
func
(
x
*
ODao
[
T
])
UpdateByIDTx
(
ctx
context
.
Context
,
tx
*
gorm
.
DB
,
id
xsf
.
ID
,
tb
*
T
)
error
{
func
(
x
*
ODao
[
T
])
UpdateByIDTx
(
ctx
context
.
Context
,
tx
*
gorm
.
DB
,
id
xsf
.
ID
,
tb
any
)
error
{
return
x
.
updByIDTx
(
ctx
,
tx
,
id
,
tb
)
}
...
...
@@ -224,7 +224,7 @@ func (x *ODao[T]) updByIDTx(ctx context.Context, tx *gorm.DB, id xsf.ID, upd any
return
nil
}
func
(
x
*
ODao
[
T
])
UpdateByIDsTx
(
ctx
context
.
Context
,
tx
*
gorm
.
DB
,
ids
[]
xsf
.
ID
,
tb
*
T
)
error
{
func
(
x
*
ODao
[
T
])
UpdateByIDsTx
(
ctx
context
.
Context
,
tx
*
gorm
.
DB
,
ids
[]
xsf
.
ID
,
tb
any
)
error
{
return
x
.
updByIDsTx
(
ctx
,
tx
,
ids
,
tb
)
}
...
...
@@ -232,6 +232,34 @@ func (x *ODao[T]) UpdateByIDsTx(ctx context.Context, tx *gorm.DB, ids []xsf.ID,
// ids 必传
// upd 可以是struct、map
// Updates 方法支持 struct 和 map[string]interface{} 参数。当使用 struct 更新时,默认情况下GORM 只会更新非零值的字段
// 1. 零值问题的解决方案
// 如果需要用 Updates更新零值字段,可以使用:
// // 方法1:使用Select指定字段
//
// db.Model(&user).Select("Name", "Email").Updates(User{
// Name: "张三",
// Email: "", // 这次会被更新
// })
//
// // 方法2:使用map和指针
//
// db.Model(&user).Updates(map[string]interface{}{
// "name": "张三",
// "email": "", // 使用map可以更新零值
// })
//
// 2. 与Save的区别
// // Updates - 只生成更新修改字段的SQL
// // SQL: UPDATE users SET name = '张三', age = 30 WHERE id = 1;
//
// db.Model(&user).Where("id = ?", 1).Updates(User{
// Name: "张三",
// Age: 30,
// })
//
// Save - 生成更新所有字段的SQL
// SQL: UPDATE users SET name = '张三', age = 30, email = ”, address = ”, ...所有字段 WHERE id = 1;
// db.Save(&user)
func
(
x
*
ODao
[
T
])
updByIDsTx
(
ctx
context
.
Context
,
tx
*
gorm
.
DB
,
ids
[]
xsf
.
ID
,
upd
any
)
error
{
if
len
(
ids
)
==
0
{
return
nil
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论