Skip to content

Commit 0d84687

Browse files
authored
doc: update readme (#15)
* doc: update README.md * doc: update README.md * fix: fix updateById bug * doc: update README.md
1 parent 61ed40d commit 0d84687

File tree

2 files changed

+186
-21
lines changed

2 files changed

+186
-21
lines changed

README.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,188 @@ func main() {
8686
}
8787
```
8888

89+
## 使用
90+
91+
### 下载
92+
93+
通过以下命令安装使用:
94+
95+
~~~go
96+
go get github.com/acmestack/gorm-plus
97+
~~~
98+
99+
100+
101+
### 定义表结构
102+
103+
~~~go
104+
type Student struct {
105+
ID int
106+
Name string
107+
Age uint8
108+
Email string
109+
Birthday time.Time
110+
CreatedAt time.Time
111+
UpdatedAt time.Time
112+
}
113+
~~~
114+
115+
116+
117+
### 连接数据库
118+
119+
~~~go
120+
var gormDb *gorm.DB
121+
122+
func init() {
123+
dsn := "root:root-abcd-1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
124+
var err error
125+
gormDb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
126+
Logger: logger.Default.LogMode(logger.Info),
127+
})
128+
if err != nil {
129+
log.Println(err)
130+
}
131+
gplus.Init(gormDb)
132+
}
133+
~~~
134+
135+
136+
137+
### 自动迁移
138+
139+
~~~go
140+
var student Student
141+
// 自动迁移
142+
gormDb.AutoMigrate(student)
143+
~~~
144+
145+
146+
147+
### 基础增删改查
148+
149+
##### 插入一条数据
150+
151+
~~~go
152+
studentItem := Student{Name: "zhangsan", Age: 18, Email: "123@11.com", Birthday: time.Now()}
153+
gplus.Insert(&studentItem)
154+
~~~
155+
156+
##### 插入多条数据
157+
158+
~~~go
159+
student1 := Student{Name: "zhangsan1", Age: 18, Email: "123@11.com", Birthday: time.Now()}
160+
student2 := Student{Name: "zhangsan2", Age: 18, Email: "123@11.com", Birthday: time.Now()}
161+
var students = []*Student{&student1, &student2}
162+
gplus.InsertBatch[Student](students)
163+
~~~
164+
165+
##### 分批插入数据
166+
167+
~~~go
168+
student1 := Student{Name: "zhangsan1", Age: 18, Email: "123@11.com", Birthday: time.Now()}
169+
student2 := Student{Name: "zhangsan2", Age: 18, Email: "123@11.com", Birthday: time.Now()}
170+
student3 := Student{Name: "zhangsan3", Age: 18, Email: "123@11.com", Birthday: time.Now()}
171+
student4 := Student{Name: "zhangsan4", Age: 18, Email: "123@11.com", Birthday: time.Now()}
172+
var students = []*Student{&student1, &student2, &student3, &student4}
173+
// 每次插入2条数据
174+
gplus.InsertBatchSize[Student](students, 2)
175+
~~~
176+
177+
##### 根据一个ID查询
178+
179+
~~~go
180+
student, resultDb := gplus.SelectById[Student](2)
181+
log.Printf("error:%v\n", resultDb.Error)
182+
log.Printf("RowsAffected:%v\n", resultDb.RowsAffected)
183+
log.Printf("student:%+v\n", student)
184+
~~~
185+
186+
187+
188+
##### 根据多个ID查询
189+
190+
~~~go
191+
var ids = []int{2, 3}
192+
students, resultDb := gplus.SelectByIds[Student](ids)
193+
log.Printf("error:%v\n", resultDb.Error)
194+
log.Printf("RowsAffected:%v\n", resultDb.RowsAffected)
195+
for _, student := range students {
196+
log.Printf("student:%+v\n", student)
197+
}
198+
~~~
199+
200+
201+
202+
##### 根据条件查询一条数据
203+
~~~go
204+
query, model := gplus.NewQuery[Student]()
205+
query.Eq(&model.Name, "zhangsan")
206+
student, resultDb := gplus.SelectOne(query)
207+
log.Printf("error:%v\n", resultDb.Error)
208+
log.Printf("RowsAffected:%v\n", resultDb.RowsAffected)
209+
log.Printf("student:%v\n", student)
210+
~~~
211+
212+
##### 根据条件查询多条数据
213+
~~~go
214+
query, model := gplus.NewQuery[Student]()
215+
query.Eq(&model.Name, "zhangsan")
216+
students, resultDb := gplus.SelectList(query)
217+
log.Printf("error:%v\n", resultDb.Error)
218+
log.Printf("RowsAffected:%v\n", resultDb.RowsAffected)
219+
for _, student := range students {
220+
log.Printf("student:%v\n", student)
221+
}
222+
~~~
223+
224+
##### 根据条件查询多条数据(泛型封装)
225+
~~~go
226+
type StudentVo struct {
227+
Name string
228+
Age int
229+
}
230+
query, model := gplus.NewQuery[Student]()
231+
query.Eq(&model.Name, "zhangsan")
232+
students, resultDb := gplus.SelectListModel[Student, StudentVo](query)
233+
log.Printf("error:%v\n", resultDb.Error)
234+
log.Printf("RowsAffected:%v\n", resultDb.RowsAffected)
235+
for _, student := range students {
236+
log.Printf("student:%v\n", student)
237+
}
238+
~~~
239+
##### 根据条件分页查询
240+
~~~go
241+
query, model := gplus.NewQuery[Student]()
242+
page := gplus.NewPage[Student](1, 5)
243+
query.Eq(&model.Name, "zhangsan")
244+
page, resultDb := gplus.SelectPage(page, query)
245+
log.Printf("error:%v\n", resultDb.Error)
246+
log.Printf("RowsAffected:%v\n", resultDb.RowsAffected)
247+
log.Printf("total:%v\n", page.Total)
248+
log.Printf("current:%v\n", page.Current)
249+
log.Printf("size:%v\n", page.Size)
250+
for _, student := range page.Records {
251+
log.Printf("student:%v\n", student)
252+
}
253+
~~~
254+
255+
##### 根据条件分页查询(泛型封装)
256+
~~~go
257+
type StudentVo struct {
258+
Name string
259+
Age int
260+
}
261+
query, model := gplus.NewQuery[Student]()
262+
page := gplus.NewPage[StudentVo](1, 5)
263+
query.Eq(&model.Name, "zhangsan")
264+
page, resultDb := gplus.SelectPageModel[Student, StudentVo](page, query)
265+
log.Printf("error:%v\n", resultDb.Error)
266+
log.Printf("RowsAffected:%v\n", resultDb.RowsAffected)
267+
log.Printf("total:%v\n", page.Total)
268+
log.Printf("current:%v\n", page.Current)
269+
log.Printf("size:%v\n", page.Size)
270+
for _, student := range page.Records {
271+
log.Printf("student:%v\n", student)
272+
}
273+
~~~

gplus/base_dao.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package gplus
1919

2020
import (
21-
"fmt"
2221
"github.com/acmestack/gorm-plus/constants"
2322
"gorm.io/gorm"
2423
"gorm.io/gorm/schema"
@@ -88,7 +87,7 @@ func Delete[T any](q *Query[T]) *gorm.DB {
8887
}
8988

9089
func UpdateById[T any](entity *T) *gorm.DB {
91-
resultDb := gormDb.Model(&entity).Where(getPkColumnName[T](), getPkColumnValue(entity)).Updates(entity)
90+
resultDb := gormDb.Model(entity).Updates(entity)
9291
return resultDb
9392
}
9493

@@ -244,22 +243,3 @@ func getPkColumnName[T any]() string {
244243
}
245244
return columnName
246245
}
247-
248-
func getPkColumnValue(entity any) string {
249-
entityValue := reflect.ValueOf(entity)
250-
entityValue = reflect.Indirect(entityValue)
251-
entityType := reflect.Indirect(entityValue).Type()
252-
numField := entityType.NumField()
253-
var primaryKeyValue reflect.Value
254-
for i := 0; i < numField; i++ {
255-
field := entityType.Field(i)
256-
tagSetting := schema.ParseTagSetting(field.Tag.Get("gorm"), ";")
257-
isPrimaryKey := utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"])
258-
if isPrimaryKey {
259-
primaryKeyValue = entityValue.Field(i)
260-
break
261-
}
262-
}
263-
id := fmt.Sprintf("%v", primaryKeyValue)
264-
return id
265-
}

0 commit comments

Comments
 (0)