@@ -109,10 +109,26 @@ func Delete[T any](q *QueryCond[T], opts ...OptionFunc) *gorm.DB {
109
109
// UpdateById 根据 ID 更新
110
110
func UpdateById [T any ](entity * T , opts ... OptionFunc ) * gorm.DB {
111
111
db := getDb (opts ... )
112
+
113
+ // 如果用户没有设置选择更新的字段,默认更新所有的字段,包括零值更新
114
+ updateAllIfNeed (entity , opts , db )
115
+
112
116
resultDb := db .Model (entity ).Updates (entity )
113
117
return resultDb
114
118
}
115
119
120
+ func updateAllIfNeed (entity any , opts []OptionFunc , db * gorm.DB ) {
121
+ option := getOption (opts )
122
+ if len (option .Selects ) == 0 {
123
+ columnNameMap := getColumnNameMap (entity )
124
+ var columnNames []string
125
+ for _ , columnName := range columnNameMap {
126
+ columnNames = append (columnNames , columnName )
127
+ }
128
+ db .Select (columnNames )
129
+ }
130
+ }
131
+
116
132
// Update 根据 Map 更新
117
133
func Update [T any ](q * QueryCond [T ], opts ... OptionFunc ) * gorm.DB {
118
134
db := getDb (opts ... )
@@ -143,12 +159,6 @@ func SelectOne[T any](q *QueryCond[T], opts ...OptionFunc) (*T, *gorm.DB) {
143
159
return & entity , resultDb .First (& entity )
144
160
}
145
161
146
- // Exists 根据条件判断记录是否存在
147
- func Exists [T any ](q * QueryCond [T ], opts ... OptionFunc ) (bool , error ) {
148
- _ , dbRes := SelectOne [T ](q , opts ... )
149
- return dbRes .RowsAffected > 0 , dbRes .Error
150
- }
151
-
152
162
// SelectList 根据条件查询多条记录
153
163
func SelectList [T any ](q * QueryCond [T ], opts ... OptionFunc ) ([]* T , * gorm.DB ) {
154
164
resultDb := buildCondition (q , opts ... )
@@ -157,32 +167,6 @@ func SelectList[T any](q *QueryCond[T], opts ...OptionFunc) ([]*T, *gorm.DB) {
157
167
return results , resultDb
158
168
}
159
169
160
- // SelectListModel 根据条件查询多条记录
161
- // 第一个泛型代表数据库表实体
162
- // 第二个泛型代表返回记录实体
163
- func SelectListModel [T any , R any ](q * QueryCond [T ], opts ... OptionFunc ) ([]* R , * gorm.DB ) {
164
- resultDb := buildCondition (q , opts ... )
165
- var results []* R
166
- resultDb .Scan (& results )
167
- return results , resultDb
168
- }
169
-
170
- // SelectListByMap 根据 Map 查询多条记录
171
- func SelectListByMap [T any ](q * QueryCond [T ], opts ... OptionFunc ) ([]* T , * gorm.DB ) {
172
- resultDb := buildCondition (q , opts ... )
173
- var results []* T
174
- resultDb .Find (& results )
175
- return results , resultDb
176
- }
177
-
178
- // SelectListMaps 根据条件查询,返回Map记录
179
- func SelectListMaps [T any ](q * QueryCond [T ], opts ... OptionFunc ) ([]map [string ]any , * gorm.DB ) {
180
- resultDb := buildCondition (q , opts ... )
181
- var results []map [string ]any
182
- resultDb .Find (& results )
183
- return results , resultDb
184
- }
185
-
186
170
// SelectPage 根据条件分页查询记录
187
171
func SelectPage [T any ](page * Page [T ], q * QueryCond [T ], opts ... OptionFunc ) (* Page [T ], * gorm.DB ) {
188
172
option := getOption (opts )
@@ -203,28 +187,24 @@ func SelectPage[T any](page *Page[T], q *QueryCond[T], opts ...OptionFunc) (*Pag
203
187
return page , resultDb
204
188
}
205
189
206
- // SelectPageModel 根据条件分页查询记录
207
- // 第一个泛型代表数据库表实体
208
- // 第二个泛型代表返回记录实体
209
- func SelectPageModel [T any , R any ](page * Page [R ], q * QueryCond [T ], opts ... OptionFunc ) (* Page [R ], * gorm.DB ) {
210
- option := getOption (opts )
211
- // 如果需要分页忽略总数,不查询总数
212
- if ! option .IgnoreTotal {
213
- total , countDb := SelectCount [T ](q , opts ... )
214
- if countDb .Error != nil {
215
- return page , countDb
216
- }
217
- page .Total = total
218
- }
190
+ // SelectCount 根据条件查询记录数量
191
+ func SelectCount [T any ](q * QueryCond [T ], opts ... OptionFunc ) (int64 , * gorm.DB ) {
192
+ var count int64
219
193
resultDb := buildCondition (q , opts ... )
220
- var results []* R
221
- resultDb .Scopes (paginate (page )).Scan (& results )
222
- page .Records = results
223
- return page , resultDb
194
+ resultDb .Count (& count )
195
+ return count , resultDb
224
196
}
225
197
226
- // SelectPageMaps 根据条件分页查询,返回分页Map记录
227
- func SelectPageMaps [T any ](page * Page [map [string ]any ], q * QueryCond [T ], opts ... OptionFunc ) (* Page [map [string ]any ], * gorm.DB ) {
198
+ // Exists 根据条件判断记录是否存在
199
+ func Exists [T any ](q * QueryCond [T ], opts ... OptionFunc ) (bool , error ) {
200
+ _ , dbRes := SelectOne [T ](q , opts ... )
201
+ return dbRes .RowsAffected > 0 , dbRes .Error
202
+ }
203
+
204
+ // SelectPageGeneric 根据传入的泛型封装分页记录
205
+ // 第一个泛型代表数据库表实体
206
+ // 第二个泛型代表返回记录实体
207
+ func SelectPageGeneric [T any , R any ](page * Page [R ], q * QueryCond [T ], opts ... OptionFunc ) (* Page [R ], * gorm.DB ) {
228
208
option := getOption (opts )
229
209
// 如果需要分页忽略总数,不查询总数
230
210
if ! option .IgnoreTotal {
@@ -235,20 +215,21 @@ func SelectPageMaps[T any](page *Page[map[string]any], q *QueryCond[T], opts ...
235
215
page .Total = total
236
216
}
237
217
resultDb := buildCondition (q , opts ... )
238
- var results []map [ string ] any
239
- resultDb .Scopes (paginate (page )).Find (& results )
218
+ var results []R
219
+ resultDb .Scopes (paginate (page )).Scan (& results )
240
220
for _ , m := range results {
241
221
page .Records = append (page .Records , & m )
242
222
}
243
223
return page , resultDb
244
224
}
245
225
246
- // SelectCount 根据条件查询记录数量
247
- func SelectCount [T any ](q * QueryCond [T ], opts ... OptionFunc ) (int64 , * gorm.DB ) {
248
- var count int64
226
+ // SelectGeneric 根据传入的泛型封装记录
227
+ // 第一个泛型代表数据库表实体
228
+ // 第二个泛型代表返回记录实体
229
+ func SelectGeneric [T any , R any ](q * QueryCond [T ], opts ... OptionFunc ) (R , * gorm.DB ) {
230
+ var entity R
249
231
resultDb := buildCondition (q , opts ... )
250
- resultDb .Count (& count )
251
- return count , resultDb
232
+ return entity , resultDb .Scan (& entity )
252
233
}
253
234
254
235
func Begin (opts ... * sql.TxOptions ) * gorm.DB {
0 commit comments