Skip to content

Commit 98a0833

Browse files
committed
feat: add example
1 parent de1e787 commit 98a0833

File tree

11 files changed

+239
-59
lines changed

11 files changed

+239
-59
lines changed

example/example_insert.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package example
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/gorm-plus/gorm-plus/gormplus"
7+
)
8+
9+
func Insert() {
10+
user := &User{Username: "zhangsan", Password: "123456", Age: 18, Score: 100, Dept: "A部门"}
11+
result := gormplus.Insert(user)
12+
if result.Error != nil {
13+
fmt.Println(result.Error)
14+
}
15+
fmt.Println(result.RowsAffected)
16+
}
17+
18+
func InsertBatch() {
19+
user1 := &User{Username: "zhangsan1", Password: "123456", Age: 18, Score: 12, Dept: "导弹部门"}
20+
user2 := &User{Username: "lisi", Password: "123456", Age: 16, Score: 34, Dept: "投诉部门"}
21+
user3 := &User{Username: "wangwu", Password: "123456", Age: 26, Score: 33, Dept: "研发部门"}
22+
user4 := &User{Username: "zhangsan4", Password: "123456", Age: 30, Score: 11, Dept: "产品部门"}
23+
user5 := &User{Username: "zhangsan5", Password: "123456", Age: 12, Score: 34, Dept: "产品部门1"}
24+
user6 := &User{Username: "zhangsan6", Password: "123456", Age: 45, Score: 123, Dept: "产品部门12"}
25+
26+
var users []*User
27+
users = append(users, user1)
28+
users = append(users, user2)
29+
users = append(users, user3)
30+
users = append(users, user4)
31+
users = append(users, user5)
32+
users = append(users, user6)
33+
34+
result := gormplus.InsertBatch[User](users)
35+
if result.Error != nil {
36+
fmt.Println(result.Error)
37+
}
38+
fmt.Println(result.RowsAffected)
39+
for _, u := range users {
40+
marshal, _ := json.Marshal(u)
41+
fmt.Println(string(marshal))
42+
}
43+
}
44+
45+
func InsertBatchSize() {
46+
user1 := &User{Username: "zhangsan1", Password: "123456", Age: 18, Score: 12, Dept: "导弹部门"}
47+
user2 := &User{Username: "lisi", Password: "123456", Age: 16, Score: 34, Dept: "投诉部门"}
48+
user3 := &User{Username: "wangwu", Password: "123456", Age: 26, Score: 33, Dept: "研发部门"}
49+
user4 := &User{Username: "zhangsan4", Password: "123456", Age: 30, Score: 11, Dept: "产品部门"}
50+
user5 := &User{Username: "zhangsan5", Password: "123456", Age: 12, Score: 34, Dept: "产品部门1"}
51+
user6 := &User{Username: "zhangsan6", Password: "123456", Age: 45, Score: 123, Dept: "产品部门12"}
52+
53+
var users []*User
54+
users = append(users, user1)
55+
users = append(users, user2)
56+
users = append(users, user3)
57+
users = append(users, user4)
58+
users = append(users, user5)
59+
users = append(users, user6)
60+
61+
result := gormplus.InsertBatchSize[User](users, 3)
62+
if result.Error != nil {
63+
fmt.Println(result.Error)
64+
}
65+
fmt.Println(result.RowsAffected)
66+
for _, u := range users {
67+
marshal, _ := json.Marshal(u)
68+
fmt.Println(string(marshal))
69+
}
70+
}

example/example_insert_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package example
2+
3+
import "testing"
4+
5+
func TestInsert(t *testing.T) {
6+
Insert()
7+
}
8+
9+
func TestInsertBatch(t *testing.T) {
10+
InsertBatch()
11+
}
12+
13+
func TestInsertBatchSize(t *testing.T) {
14+
InsertBatchSize()
15+
}

example/example_update.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package example
2+
3+
import (
4+
"fmt"
5+
"github.com/gorm-plus/gorm-plus/gormplus"
6+
)
7+
8+
func UpdateById() {
9+
user := &User{ID: 1, Username: "zhangsan", Password: "123456", Age: 18, Score: 100, Dept: "A部门asdfasdf"}
10+
result := gormplus.UpdateById(user)
11+
if result.Error != nil {
12+
fmt.Println(result.Error)
13+
}
14+
fmt.Println(result.RowsAffected)
15+
}
16+
17+
func Update() {
18+
q := gormplus.Query[User]{}
19+
q.Eq(UserColumn.Username, "zhangsan").Set(UserColumn.Dept, "相关部门123123").
20+
Set(UserColumn.Phone, 12312)
21+
result := gormplus.Update(&q)
22+
if result.Error != nil {
23+
fmt.Println(result.Error)
24+
}
25+
fmt.Println(result.RowsAffected)
26+
}

example/example_update_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package example
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestUpdateById(t *testing.T) {
8+
UpdateById()
9+
}
10+
11+
func TestUpdate(t *testing.T) {
12+
Update()
13+
}

example/gormplus.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package example
2+
3+
import (
4+
"github.com/gorm-plus/gorm-plus/gormplus"
5+
"gorm.io/driver/mysql"
6+
"gorm.io/gorm"
7+
"gorm.io/gorm/logger"
8+
"log"
9+
)
10+
11+
var gormDb *gorm.DB
12+
13+
func init() {
14+
dsn := "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
15+
var err error
16+
gormDb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
17+
Logger: logger.Default.LogMode(logger.Info),
18+
})
19+
if err != nil {
20+
log.Fatalln(err)
21+
}
22+
gormplus.Init(gormDb)
23+
}

example/user.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package example
2+
3+
import "time"
4+
5+
type User struct {
6+
ID int64 `gorm:"primaryKey"`
7+
Username string `gorm:"column:username"`
8+
Password string
9+
Age int
10+
Phone string
11+
Score int
12+
Dept string
13+
CreatedAt time.Time
14+
UpdatedAt time.Time
15+
}
16+
17+
func (User) TableName() string {
18+
return "Users"
19+
}

example/zz_gen_column.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package example
2+
3+
var UserColumn = struct {
4+
ID string
5+
Username string
6+
Password string
7+
Age string
8+
Phone string
9+
Score string
10+
Dept string
11+
CreatedAt string
12+
UpdatedAt string
13+
}{
14+
ID: "id",
15+
Username: "username",
16+
Password: "password",
17+
Age: "age",
18+
Phone: "phone",
19+
Score: "score",
20+
Dept: "dept",
21+
CreatedAt: "created_at",
22+
UpdatedAt: "updated_at",
23+
}

example/zz_script.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
DROP TABLE IF EXISTS `users`;
2+
CREATE TABLE `users` (
3+
`id` int(0) NOT NULL AUTO_INCREMENT,
4+
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
5+
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
6+
`age` int(0) NULL DEFAULT NULL,
7+
`phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
8+
`score` int(0) NULL DEFAULT NULL,
9+
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
10+
`dept` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
11+
`created_at` datetime(0) NULL DEFAULT NULL,
12+
`updated_at` datetime(0) NULL DEFAULT NULL,
13+
PRIMARY KEY (`id`) USING BTREE
14+
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
15+
16+
SET FOREIGN_KEY_CHECKS = 1;

gormplus/mapper.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,19 @@ func Insert[T any](entity *T) *gorm.DB {
2121
return resultDb
2222
}
2323

24-
func InsertMigrate[T any](entity *T) (*gorm.DB, error) {
25-
if err := gormDb.AutoMigrate(new(T)); err != nil {
26-
return nil, err
27-
}
28-
resultDb := gormDb.Create(&entity)
29-
return resultDb, nil
30-
}
31-
32-
func InsertBatch[T any](entities ...*T) *gorm.DB {
33-
resultDb := gormDb.CreateInBatches(&entities, defaultBatchSize)
24+
func InsertBatch[T any](entities any) *gorm.DB {
25+
resultDb := gormDb.CreateInBatches(entities, defaultBatchSize)
3426
return resultDb
3527
}
3628

37-
func InsertBatchSize[T any](batchSize int, entities ...*T) *gorm.DB {
29+
func InsertBatchSize[T any](entities any, batchSize int) *gorm.DB {
3830
if batchSize <= 0 {
3931
batchSize = defaultBatchSize
4032
}
41-
resultDb := gormDb.CreateInBatches(&entities, batchSize)
33+
resultDb := gormDb.CreateInBatches(entities, batchSize)
4234
return resultDb
4335
}
4436

45-
func InsertBatchMigrate[T any](entities ...*T) (*gorm.DB, error) {
46-
if err := gormDb.AutoMigrate(new(T)); err != nil {
47-
return nil, err
48-
}
49-
resultDb := gormDb.Create(&entities)
50-
return resultDb, nil
51-
}
52-
5337
func DeleteById[T any](id any) *gorm.DB {
5438
resultDb := gormDb.Delete(new(T), id)
5539
return resultDb
@@ -67,15 +51,13 @@ func Delete[T any](q *Query[T]) *gorm.DB {
6751
return resultDb
6852
}
6953

70-
func UpdateById[T any](id any, entity *T) *gorm.DB {
71-
var e T
72-
gormDb.First(&e, id)
73-
resultDb := gormDb.Model(&e).Updates(entity)
54+
func UpdateById[T any](entity *T) *gorm.DB {
55+
resultDb := gormDb.Model(&entity).Updates(&entity)
7456
return resultDb
7557
}
7658

77-
func Update[T any](q *Query[T], entity *T) *gorm.DB {
78-
resultDb := gormDb.Where(q.QueryBuilder.String(), q.QueryArgs...).Updates(entity)
59+
func Update[T any](q *Query[T]) *gorm.DB {
60+
resultDb := gormDb.Model(new(T)).Where(q.QueryBuilder.String(), q.QueryArgs...).Updates(&q.UpdateMap)
7961
return resultDb
8062
}
8163

gormplus/mapper_test.go

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ func TestInsert(t *testing.T) {
3333
fmt.Println(test1)
3434
}
3535

36-
func TestInsertMigrate(t *testing.T) {
37-
test1 := Test1{Code: "D455", Price: 100}
38-
resultDb, err := InsertMigrate(&test1)
39-
if err != nil {
40-
fmt.Println(err)
41-
}
42-
fmt.Println(resultDb)
43-
fmt.Println(test1)
44-
}
45-
4636
func TestInsertBatch(t *testing.T) {
47-
test1 := Test1{Code: "D466", Price: 100}
48-
test2 := Test1{Code: "D466", Price: 100}
49-
50-
resultDb := InsertBatch(&test1, &test2)
51-
fmt.Println(resultDb)
37+
test1 := Test1{Code: "D477", Price: 100}
38+
test2 := Test1{Code: "D477", Price: 100}
39+
test3 := Test1{Code: "D477", Price: 100}
40+
var ts []*Test1
41+
ts = append(ts, &test1)
42+
ts = append(ts, &test2)
43+
ts = append(ts, &test3)
44+
resultDb := InsertBatch[Test1](&ts)
45+
fmt.Println(resultDb.RowsAffected)
5246
fmt.Println(test1)
5347
fmt.Println(test2)
5448
}
@@ -57,20 +51,12 @@ func TestInsertBatchSize(t *testing.T) {
5751
test1 := Test1{Code: "D466", Price: 100}
5852
test2 := Test1{Code: "D466", Price: 100}
5953
test3 := Test1{Code: "D466", Price: 100}
54+
var ts []Test1
55+
ts = append(ts, test1)
56+
ts = append(ts, test2)
57+
ts = append(ts, test3)
6058

61-
resultDb := InsertBatchSize(2, &test1, &test2, &test3)
62-
fmt.Println(resultDb)
63-
fmt.Println(test1)
64-
fmt.Println(test2)
65-
}
66-
67-
func TestInsertBatchMigrate(t *testing.T) {
68-
test1 := Test1{Code: "D477", Price: 100}
69-
test2 := Test1{Code: "D477", Price: 100}
70-
resultDb, err := InsertBatchMigrate(&test1, &test2)
71-
if err != nil {
72-
fmt.Println(err)
73-
}
59+
resultDb := InsertBatchSize[Test1](ts, 2)
7460
fmt.Println(resultDb)
7561
fmt.Println(test1)
7662
fmt.Println(test2)
@@ -95,16 +81,14 @@ func TestDelete(t *testing.T) {
9581

9682
func TestUpdateById(t *testing.T) {
9783
test1 := Test1{Code: "777"}
98-
resultDb := UpdateById(6, &test1)
84+
resultDb := UpdateById(&test1)
9985
fmt.Println(resultDb)
10086
}
10187

10288
func TestUpdate(t *testing.T) {
10389
q := Query[Test1]{}
104-
q.Eq("code", "D42").Eq("price", 100)
105-
test1 := Test1{Code: "888"}
106-
// todo 需要给用户选择字段更新
107-
resultDb := Update(&q, &test1)
90+
q.Eq("code", "D42").Set("price", 100)
91+
resultDb := Update(&q)
10892
fmt.Println(resultDb)
10993
}
11094

0 commit comments

Comments
 (0)