Skip to content

2.快速上手

afumu edited this page Jun 6, 2023 · 1 revision

现有一张 Users 表,其表结构如下:

CREATE TABLE `users` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `address` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `age` bigint DEFAULT NULL,
  `phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `score` bigint DEFAULT NULL,
  `dept` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=407 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

对应的数据如下:

INSERT INTO `users` (`username`, `password`, `address`, `age`, `phone`, `score`, `dept`, `created_at`, `updated_at`)
VALUES
  ('张三', 'password1', '地址1', 25, '12345678901', 80, '部门1', NOW(), NOW()),
  ('李四', 'password2', '地址2', 30, '12345678902', 90, '部门2', NOW(), NOW()),
  ('王五', 'password3', '地址3', 35, '12345678903', 70, '部门1', NOW(), NOW()),
  ('赵六', 'password4', '地址4', 28, '12345678904', 85, '部门2', NOW(), NOW()),
  ('钱七', 'password5', '地址5', 32, '12345678905', 75, '部门1', NOW(), NOW());

开始使用

下载Gorm-Plus

 go get github.com/acmestack/gorm-plus
package main

import (
  "github.com/acmestack/gorm-plus/gplus"
  "gorm.io/driver/mysql"
  "gorm.io/gorm"
  "gorm.io/gorm/logger"
  "log"
  "time"
)

type User struct {
  ID        int64
  Username  string
  Password  string
  Address   string
  Age       int
  Phone     string
  Score     int
  Dept      string
  CreatedAt time.Time
  UpdatedAt time.Time
}

var gormDb *gorm.DB

func init() {
  dsn := "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
  var err error
  gormDb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
    Logger: logger.Default.LogMode(logger.Info),
  })
  if err != nil {
    log.Println(err)
  }

  // 初始化gplus
  gplus.Init(gormDb)
}

func main() {
  users, resultDb := gplus.SelectList[User](nil)
  log.Println("error:", resultDb.Error)
  log.Println("RowsAffected:", resultDb.RowsAffected)
  for _, user := range users {
    log.Println("user:", user)
  }
}

控制台输出:

2023/06/01 17:48:19 error: <nil>
2023/06/01 17:48:19 RowsAffected: 5
2023/06/01 17:48:19 user: &{1 张三 password1 地址1 25 12345678901 80 部门1 2023-06-01 17:48:11 +0800 CST 2023-06-01 17:48:11 +0800 CST}
2023/06/01 17:48:19 user: &{2 李四 password2 地址2 30 12345678902 90 部门2 2023-06-01 17:48:11 +0800 CST 2023-06-01 17:48:11 +0800 CST}
2023/06/01 17:48:19 user: &{3 王五 password3 地址3 35 12345678903 70 部门1 2023-06-01 17:48:11 +0800 CST 2023-06-01 17:48:11 +0800 CST}
2023/06/01 17:48:19 user: &{4 赵六 password4 地址4 28 12345678904 85 部门2 2023-06-01 17:48:11 +0800 CST 2023-06-01 17:48:11 +0800 CST}
2023/06/01 17:48:19 user: &{5 钱七 password5 地址5 32 12345678905 75 部门1 2023-06-01 17:48:11 +0800 CST 2023-06-01 17:48:11 +0800 CST}

总结

从上述步骤中,我们可以看到集成Gorm-Plus非常简单。只需在初始化Gorm之后添加一行代码gplus.Init(gormDb)

即可使用。不仅如此,使用Gorm-Plus也同样轻松,只需一行代码即可完成列表查询。

然而,Gorm-Plus的强大功能远不止于此。如果你想详细了解它的威力,继续往下看吧!

Clone this wiki locally