Skip to content

Commit 1f82446

Browse files
authored
refactor: Refactor code (#22)
* feat: Add db parameters to each function * feat: Add db parameters to each function * feat: Add db parameters to each function * refactor: Remove redundant code * refactor: Remove common_dao.go * doc: update README.md
1 parent 9bd3ffa commit 1f82446

File tree

4 files changed

+107
-254
lines changed

4 files changed

+107
-254
lines changed

README.md

Lines changed: 35 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -317,108 +317,6 @@ log.Printf("error:%v\n", resultDb.Error)
317317
log.Printf("RowsAffected:%v\n", resultDb.RowsAffected)
318318
```
319319

320-
321-
322-
### 通用查询
323-
324-
gplus提供通用的CRUD操作,只需要在struct中嵌入` gplus.CommonDao`即可使用gplus提供的所有CRUD操作。
325-
326-
```Go
327-
type StudentDao struct {
328-
gplus.CommonDao[Student]
329-
}
330-
331-
var studentDao = &StudentDao{}
332-
```
333-
334-
335-
336-
#### 根据ID查询
337-
338-
```Go
339-
student, resultDb := studentDao.GetById(2)
340-
log.Printf("error:%+v", resultDb.Error)
341-
log.Printf("RowsAffected:%+v", resultDb.RowsAffected)
342-
log.Printf("student:%+v", student)
343-
```
344-
345-
346-
347-
#### 根据条件查询一条数据
348-
349-
```Go
350-
query, model := gplus.NewQuery[Student]()
351-
query.Eq(&model.Name, "zhangsan1")
352-
student, resultDb := studentDao.GetOne(query)
353-
log.Printf("error:%+v", resultDb.Error)
354-
log.Printf("RowsAffected:%+v", resultDb.RowsAffected)
355-
log.Printf("student:%+v", student)
356-
```
357-
358-
359-
360-
#### 查询列表所有数据
361-
362-
```Go
363-
students, resultDb := studentDao.ListAll()
364-
log.Printf("error:%+v", resultDb.Error)
365-
fmt.Println("RowsAffected:", resultDb.RowsAffected)
366-
for _, student := range students {
367-
log.Printf("student:%+v", student)
368-
}
369-
```
370-
371-
372-
373-
#### 根据条件查询数据
374-
375-
```Go
376-
query, model := gplus.NewQuery[Student]()
377-
query.Eq(&model.Name, "zhangsan1")
378-
students, resultDb := studentDao.List(query)
379-
log.Printf("error:%+v", resultDb.Error)
380-
fmt.Println("RowsAffected:", resultDb.RowsAffected)
381-
for _, student := range students {
382-
log.Printf("student:%+v", student)
383-
}
384-
```
385-
386-
387-
388-
#### 分页查询所有数据
389-
390-
```Go
391-
page := gplus.NewPage[Student](1, 2)
392-
page, resultDb := studentDao.PageAll(page)
393-
log.Printf("error:%+v", resultDb.Error)
394-
fmt.Println("RowsAffected:", resultDb.RowsAffected)
395-
for _, student := range page.Records {
396-
log.Printf("student:%+v", student)
397-
}
398-
```
399-
400-
401-
402-
#### 分页条件查询数据
403-
404-
```Go
405-
page := gplus.NewPage[Student](1, 2)
406-
query, model := gplus.NewQuery[Student]()
407-
query.Eq(&model.Name, "zhangsan1")
408-
page, resultDb := studentDao.Page(page, query)
409-
log.Printf("error:%+v", resultDb.Error)
410-
fmt.Println("RowsAffected:", resultDb.RowsAffected)
411-
for _, student := range page.Records {
412-
log.Printf("student:%+v", student)
413-
}
414-
```
415-
416-
417-
418-
419-
420-
421-
422320
### 高级查询
423321

424322
#### 条件构造器
@@ -458,7 +356,42 @@ gorm-plus 提供了强大的条件构造器,通过构造器能够组合不同的
458356
log.Printf("studentResult:%+v\n", studentResult)
459357
```
460358

359+
#### Query泛型简化
360+
361+
如果不希望每次创建Query对象的时候携带上泛型,我们可以提供一个全局的泛型Dao。
461362

363+
```Go
364+
var dao gplus.Dao[Student]
365+
func main() {
366+
query, model := dao.NewQuery()
367+
query.Eq(&model.Name, "zhangsan")
368+
list, resultDb := gplus.SelectList(query)
369+
fmt.Println(resultDb.RowsAffected)
370+
for _, v := range list {
371+
marshal, _ := json.Marshal(v)
372+
fmt.Println(string(marshal))
373+
}
374+
}
375+
```
376+
377+
我们也可以把`gplus.Dao`组合到我们自己定义的Dao对象中
378+
379+
```Go
380+
type StudentDao struct {
381+
gplus.Dao[Student]
382+
}
383+
var studentDao StudentDao
384+
func main() {
385+
query, model := studentDao.NewQuery()
386+
query.Eq(&model.Name, "zhangsan")
387+
list, resultDb := gplus.SelectList(query)
388+
fmt.Println(resultDb.RowsAffected)
389+
for _, v := range list {
390+
marshal, _ := json.Marshal(v)
391+
fmt.Println(string(marshal))
392+
}
393+
}
394+
```
462395

463396
#### 查询指定字段
464397

gplus/common_dao.go

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)