@@ -5,132 +5,201 @@ This is an plus version of gorm, which is similar to the mybatis-plus syntax.
5
5
6
6
下载使用:
7
7
8
- go get github.com/gorm-plus /gorm-plus
8
+ go get github.com/acmestack /gorm-plus
9
9
10
10
使用demo:
11
11
12
12
``` go
13
13
14
+ var GormDb *gorm.DB
14
15
15
16
func init () {
16
17
dsn := " root:root-abcd-1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
17
18
var err error
18
- gormDb , err := gorm.Open (mysql.Open (dsn), &gorm.Config {})
19
+ GormDb, err = gorm.Open (mysql.Open (dsn), &gorm.Config {
20
+ Logger: logger.Default .LogMode (logger.Info ),
21
+ })
19
22
if err != nil {
20
- fmt. Println (err)
23
+ log. Fatalln (err)
21
24
}
22
- gormplus .Init (gormDb )
25
+ gplus .Init (GormDb )
23
26
}
24
27
25
- type Test1 struct {
26
- gorm.Model
27
- Code string
28
- Price uint
28
+
29
+ // +gplus:column=true
30
+
31
+ type User struct {
32
+ ID int64
33
+ Username string ` gorm:"column:username"`
34
+ Password string
35
+ Address string
36
+ Age int
37
+ Phone string
38
+ Score int
39
+ Dept string
40
+ CreatedAt time.Time
41
+ UpdatedAt time.Time
29
42
}
30
43
31
- func TestSave (t *testing .T ) {
32
- test1 := Test1{Code: " D1" , Price: 100 }
33
- resultDb := gormplus.Insert (&test1)
34
- fmt.Println (resultDb)
35
- fmt.Println (test1)
44
+ func (User ) TableName () string {
45
+ return " Users"
36
46
}
37
47
38
- func TestSaveMigrate (t *testing .T ) {
39
- test1 := Test1{Code: " D2" , Price: 100 }
40
- resultDb , err := gormplus.InsertMigrate (&test1)
41
- if err != nil {
42
- fmt.Println (err)
48
+ ```
49
+
50
+ select
51
+
52
+ ``` go
53
+
54
+ func TestSelectById (t *testing .T ) {
55
+ user , resultDb := gplus.SelectById [User](1 )
56
+ if resultDb.Error != nil {
57
+ if errors.Is (resultDb.Error , gorm.ErrRecordNotFound ) {
58
+ log.Fatalln (" SelectById Data not found:" , resultDb.Error )
59
+ }
60
+ log.Fatalln (" SelectById error:" , resultDb.Error )
43
61
}
44
- fmt.Println (resultDb)
45
- fmt.Println (test1)
62
+ log.Println (" RowsAffected:" , resultDb.RowsAffected )
63
+ marshal , _ := json.Marshal (user)
64
+ log.Println (string (marshal))
46
65
}
47
66
48
- func TestBatchSave (t *testing .T ) {
49
- test1 := Test1{Code: " D3" , Price: 100 }
50
- test2 := Test1{Code: " D4" , Price: 100 }
51
- resultDb := gormplus.InsertBatch (&test1, &test2)
52
- fmt.Println (resultDb)
53
- fmt.Println (test1)
54
- fmt.Println (test2)
67
+ func TestSelectByIds (t *testing .T ) {
68
+ var ids []int
69
+ ids = append (ids, 1 )
70
+ ids = append (ids, 2 )
71
+ users , resultDb := gplus.SelectByIds [User](ids)
72
+ if resultDb.Error != nil {
73
+ log.Fatalln (resultDb.Error )
74
+ }
75
+ log.Println (" RowsAffected:" , resultDb.RowsAffected )
76
+ marshal , _ := json.Marshal (users)
77
+ log.Println (string (marshal))
55
78
}
56
79
57
- func TestSaveBatchMigrate (t *testing .T ) {
58
- test1 := Test1{Code: " D5" , Price: 100 }
59
- test2 := Test1{Code: " D6" , Price: 100 }
60
- resultDb , err := gormplus.InsertBatchMigrate (&test1, &test2)
61
- if err != nil {
62
- fmt.Println (err)
80
+ func TestSelectOne1 (t *testing .T ) {
81
+ q := gplus.NewQuery [User]()
82
+ q.Eq (UserColumn.Username , " zhangsan1" )
83
+ user , resultDb := gplus.SelectOne (q)
84
+
85
+ if resultDb.Error != nil {
86
+ if errors.Is (resultDb.Error , gorm.ErrRecordNotFound ) {
87
+ log.Fatalln (" SelectOne Data not found:" , resultDb.Error )
88
+ }
89
+ log.Fatalln (" SelectOne error:" , resultDb.Error )
63
90
}
64
- fmt.Println (resultDb)
65
- fmt.Println (test1)
66
- fmt.Println (test2)
67
- }
68
91
69
- func TestDeleteById ( t * testing . T ) {
70
- resultDb := gormplus. DeleteById [Test1]( 1 )
71
- fmt .Println (resultDb )
92
+ log. Println ( " RowsAffected: " , resultDb. RowsAffected )
93
+ marshal , _ := json. Marshal (user )
94
+ log .Println (string (marshal) )
72
95
}
73
96
74
- func TestDeleteByIds (t *testing .T ) {
75
- resultDb := gormplus.DeleteByIds [Test1](4 , 5 )
76
- fmt.Println (resultDb)
77
- }
97
+ func TestSelectOne2 (t *testing .T ) {
98
+ q := gplus.NewQuery [User]()
99
+ q.Eq (UserColumn.Username , " zhangsan" ).
100
+ Select (UserColumn.Username , UserColumn.Password )
101
+ user , resultDb := gplus.SelectOne (q)
78
102
79
- func TestDelete ( t * testing . T ) {
80
- q := gormplus. Query [Test1]{}
81
- q. Eq ( " code " , " D1 " ). Eq ( " price " , 100 )
82
- resultDb := gormplus. Delete (&q)
83
- fmt. Println ( resultDb)
84
- }
103
+ if resultDb. Error != nil {
104
+ if errors. Is (resultDb. Error , gorm. ErrRecordNotFound ) {
105
+ log. Fatalln ( " SelectOne Data not found: " , resultDb. Error )
106
+ }
107
+ log. Fatalln ( " SelectOne error: " , resultDb. Error )
108
+ }
85
109
86
- func TestUpdateById (t *testing .T ) {
87
- test1 := Test1{Code: " 777" }
88
- resultDb := gormplus.UpdateById (6 , &test1)
89
- fmt.Println (resultDb)
110
+ log.Println (" RowsAffected:" , resultDb.RowsAffected )
111
+ marshal , _ := json.Marshal (user)
112
+ log.Println (string (marshal))
90
113
}
91
114
92
- func TestUpdate (t *testing .T ) {
93
- q := gormplus.Query [Test1]{}
94
- q.Eq (" code" , " D42" ).Eq (" price" , 100 )
95
- test1 := Test1{Code: " 888" }
96
- resultDb := gormplus.Update (&q, &test1)
97
- fmt.Println (resultDb)
115
+ func TestSelectList (t *testing .T ) {
116
+ q := gplus.NewQuery [User]()
117
+ q.Eq (UserColumn.Username , " zhangsan" )
118
+ users , resultDb := gplus.SelectList (q)
119
+ if resultDb.Error != nil {
120
+ log.Fatalln (" error:" , resultDb.Error )
121
+ }
122
+ for _ , u := range users {
123
+ marshal , _ := json.Marshal (u)
124
+ log.Println (string (marshal))
125
+ }
98
126
}
99
127
100
- func TestSelectById (t *testing .T ) {
101
- db , result := gormplus.SelectById [Test1](1 )
102
- fmt.Println (db)
103
- fmt.Println (result)
128
+ func TestSelectBracketList (t *testing .T ) {
129
+ q := gplus.NewQuery [User]()
130
+ bracketQuery := gplus.NewQuery [User]()
131
+ bracketQuery.Eq (UserColumn.Address , " 上海" ).Or ().Eq (UserColumn.Address , " 北京" )
132
+
133
+ q.Eq (UserColumn.Username , " zhangsan" ).AndBracket (bracketQuery)
134
+ users , resultDb := gplus.SelectList (q)
135
+ if resultDb.Error != nil {
136
+ log.Fatalln (" error:" , resultDb.Error )
137
+ }
138
+ for _ , u := range users {
139
+ marshal , _ := json.Marshal (u)
140
+ log.Println (string (marshal))
141
+ }
104
142
}
105
143
106
- func TestSelectByIds (t *testing .T ) {
107
- db , result := gormplus.SelectByIds [Test1](1 , 2 )
108
- fmt.Println (db)
109
- fmt.Println (result)
144
+ func TestSelectTableList (t *testing .T ) {
145
+ type deptCount struct {
146
+ Dept string
147
+ Count string
148
+ }
149
+ q := gplus.NewQuery [User]()
150
+ q.Group (UserColumn.Dept ).Select (UserColumn.Dept , " count(*) as count" )
151
+ users , resultDb := gplus.SelectListModel [User, deptCount](q)
152
+ if resultDb.Error != nil {
153
+ log.Fatalln (" error:" , resultDb.Error )
154
+ }
155
+ for _ , u := range users {
156
+ marshal , _ := json.Marshal (u)
157
+ log.Println (string (marshal))
158
+ }
110
159
}
111
160
112
- func TestSelectOne (t *testing .T ) {
113
- q := gormplus.Query [Test1]{}
114
- q.Eq (" code" , " D42" ).Eq (" price" , 100 )
115
- db , result := gormplus.SelectOne (&q)
116
- fmt.Println (db)
117
- fmt.Println (result)
161
+ func TestSelectPage (t *testing .T ) {
162
+ q := gplus.NewQuery [User]()
163
+ q.Eq (UserColumn.Age , 18 )
164
+ page := gplus.NewPage [User](1 , 10 )
165
+ pageResult , resultDb := gplus.SelectPage (page, q)
166
+ if resultDb.Error != nil {
167
+ log.Fatalln (" error:" , resultDb.Error )
168
+ }
169
+ log.Println (" total" , pageResult.Total )
170
+ for _ , u := range pageResult.Records {
171
+ marshal , _ := json.Marshal (u)
172
+ log.Println (string (marshal))
173
+ }
118
174
}
119
175
120
- func TestSelectList (t *testing .T ) {
121
- q := gormplus.Query [Test1]{}
122
- q.Eq (" price" , 100 )
123
- db , result := gormplus.SelectList (&q)
124
- fmt.Println (db.RowsAffected )
125
- fmt.Println (result)
176
+ func TestSelectTablePage (t *testing .T ) {
177
+ type deptCount struct {
178
+ Dept string
179
+ Count string
180
+ }
181
+ q := gplus.NewQuery [User]()
182
+ q.Group (UserColumn.Dept ).Select (UserColumn.Dept , " count(*) as count" )
183
+ page := gplus.NewPage [deptCount](1 , 2 )
184
+ pageResult , resultDb := gplus.SelectPageModel [User, deptCount](page, q)
185
+ if resultDb.Error != nil {
186
+ log.Fatalln (" error:" , resultDb.Error )
187
+ }
188
+ log.Println (" total:" , pageResult.Total )
189
+ for _ , u := range pageResult.Records {
190
+ marshal , _ := json.Marshal (u)
191
+ log.Println (string (marshal))
192
+ }
126
193
}
127
194
128
195
func TestSelectCount (t *testing .T ) {
129
- q := gormplus.Query [Test1]{}
130
- q.Eq (" price" , 100 )
131
- db , count := gormplus.SelectCount (&q)
132
- fmt.Println (db)
133
- fmt.Println (count)
196
+ q := gplus.NewQuery [User]()
197
+ q.Eq (UserColumn.Age , 18 )
198
+ count , resultDb := gplus.SelectCount (q)
199
+ if resultDb.Error != nil {
200
+ log.Fatalln (" error:" , resultDb.Error )
201
+ }
202
+ log.Println (" count:" , count)
134
203
}
135
-
136
204
```
205
+
0 commit comments