|
1 | 1 | # gorm-plus
|
2 | 2 | 这是一个gorm的增强版,类似mybatis-plus语法
|
| 3 | + |
| 4 | +下载使用: |
| 5 | + |
| 6 | +go get github.com/zouchangfu/gorm-plus |
| 7 | + |
| 8 | +使用demo: |
| 9 | + |
| 10 | +``` |
| 11 | +
|
| 12 | +func init() { |
| 13 | + dsn := "root:root-abcd-1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local" |
| 14 | + var err error |
| 15 | + gormDb, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) |
| 16 | + if err != nil { |
| 17 | + fmt.Println(err) |
| 18 | + } |
| 19 | + gormplus.Init(gormDb) |
| 20 | +} |
| 21 | +
|
| 22 | +type Test1 struct { |
| 23 | + gorm.Model |
| 24 | + Code string |
| 25 | + Price uint |
| 26 | +} |
| 27 | +
|
| 28 | +func TestSave(t *testing.T) { |
| 29 | + test1 := Test1{Code: "D1", Price: 100} |
| 30 | + resultDb := mapper.Insert(&test1) |
| 31 | + fmt.Println(resultDb) |
| 32 | + fmt.Println(test1) |
| 33 | +} |
| 34 | +
|
| 35 | +func TestSaveMigrate(t *testing.T) { |
| 36 | + test1 := Test1{Code: "D2", Price: 100} |
| 37 | + resultDb, err := mapper.InsertMigrate(&test1) |
| 38 | + if err != nil { |
| 39 | + fmt.Println(err) |
| 40 | + } |
| 41 | + fmt.Println(resultDb) |
| 42 | + fmt.Println(test1) |
| 43 | +} |
| 44 | +
|
| 45 | +func TestBatchSave(t *testing.T) { |
| 46 | + test1 := Test1{Code: "D3", Price: 100} |
| 47 | + test2 := Test1{Code: "D4", Price: 100} |
| 48 | + resultDb := mapper.InsertBatch(&test1, &test2) |
| 49 | + fmt.Println(resultDb) |
| 50 | + fmt.Println(test1) |
| 51 | + fmt.Println(test2) |
| 52 | +} |
| 53 | +
|
| 54 | +func TestSaveBatchMigrate(t *testing.T) { |
| 55 | + test1 := Test1{Code: "D5", Price: 100} |
| 56 | + test2 := Test1{Code: "D6", Price: 100} |
| 57 | + resultDb, err := mapper.InsertBatchMigrate(&test1, &test2) |
| 58 | + if err != nil { |
| 59 | + fmt.Println(err) |
| 60 | + } |
| 61 | + fmt.Println(resultDb) |
| 62 | + fmt.Println(test1) |
| 63 | + fmt.Println(test2) |
| 64 | +} |
| 65 | +
|
| 66 | +func TestDeleteById(t *testing.T) { |
| 67 | + resultDb := mapper.DeleteById[Test1](1) |
| 68 | + fmt.Println(resultDb) |
| 69 | +} |
| 70 | +
|
| 71 | +func TestDeleteByIds(t *testing.T) { |
| 72 | + resultDb := mapper.DeleteByIds[Test1](4, 5) |
| 73 | + fmt.Println(resultDb) |
| 74 | +} |
| 75 | +
|
| 76 | +func TestDelete(t *testing.T) { |
| 77 | + q := mapper.Query[Test1]{} |
| 78 | + q.Eq("code", "D1").Eq("price", 100) |
| 79 | + resultDb := mapper.Delete(&q) |
| 80 | + fmt.Println(resultDb) |
| 81 | +} |
| 82 | +
|
| 83 | +func TestUpdateById(t *testing.T) { |
| 84 | + test1 := Test1{Code: "777"} |
| 85 | + resultDb := mapper.UpdateById(6, &test1) |
| 86 | + fmt.Println(resultDb) |
| 87 | +} |
| 88 | +
|
| 89 | +func TestUpdate(t *testing.T) { |
| 90 | + q := mapper.Query[Test1]{} |
| 91 | + q.Eq("code", "D42").Eq("price", 100) |
| 92 | + test1 := Test1{Code: "888"} |
| 93 | + resultDb := mapper.Update(&q, &test1) |
| 94 | + fmt.Println(resultDb) |
| 95 | +} |
| 96 | +
|
| 97 | +func TestSelectById(t *testing.T) { |
| 98 | + db, result := mapper.SelectById[Test1](1) |
| 99 | + fmt.Println(db) |
| 100 | + fmt.Println(result) |
| 101 | +} |
| 102 | +
|
| 103 | +func TestSelectByIds(t *testing.T) { |
| 104 | + db, result := mapper.SelectByIds[Test1](1, 2) |
| 105 | + fmt.Println(db) |
| 106 | + fmt.Println(result) |
| 107 | +} |
| 108 | +
|
| 109 | +func TestSelectOne(t *testing.T) { |
| 110 | + q := mapper.Query[Test1]{} |
| 111 | + q.Eq("code", "D42").Eq("price", 100) |
| 112 | + db, result := mapper.SelectOne(&q) |
| 113 | + fmt.Println(db) |
| 114 | + fmt.Println(result) |
| 115 | +} |
| 116 | +
|
| 117 | +func TestSelectList(t *testing.T) { |
| 118 | + q := mapper.Query[Test1]{} |
| 119 | + q.Eq("price", 100) |
| 120 | + db, result := mapper.SelectList(&q) |
| 121 | + fmt.Println(db.RowsAffected) |
| 122 | + fmt.Println(result) |
| 123 | +} |
| 124 | +
|
| 125 | +func TestSelectCount(t *testing.T) { |
| 126 | + q := mapper.Query[Test1]{} |
| 127 | + q.Eq("price", 100) |
| 128 | + db, count := mapper.SelectCount(&q) |
| 129 | + fmt.Println(db) |
| 130 | + fmt.Println(count) |
| 131 | +} |
| 132 | +
|
| 133 | +``` |
0 commit comments