当前位置: 首页 > news >正文

gorm多租户插件的使用

一、关于gorm多租户插件的使用

  • 1、安装依赖

    go get -u github.com/kuangshp/gorm-tenant
    
  • 2、创建一个mysql数据表

    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT primary key COMMENT '主键id',`name` varchar(50) not null  comment '名称',`tenant_id` int(11) default 0 comment '租户id',`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',`deleted_at` datetime NULL DEFAULT NULL COMMENT '软删除时间'
    )   ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
    
  • 3、将数据库文件同步生成go的数据模型

    type UserEntity struct {ID        int64          `gorm:"column:id;type:int;primaryKey;autoIncrement:true;comment:主键id" json:"id"` // 主键idName      string         `gorm:"column:name;type:varchar(50);not null;comment:名称" json:"name"`            // 名称TenantID  int64          `gorm:"column:tenant_id;type:int;comment:租户id" json:"tenantId"`                  // 租户idCreatedAt time.Time      `gorm:"column:created_at;comment:创建时间" json:"createdAt"`                         // 创建时间UpdatedAt time.Time      `gorm:"column:updated_at;comment:更新时间" json:"updatedAt"`                         // 更新时间DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime;comment:软删除时间" json:"-"`                  // 软删除时间
    }
    
  • 4、在创建数据库连接的时候使用插件

    func NewMysqlDB(username, password, host, port, database, charset, loc string) *gorm.DB {// 字符串拼接dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true&loc=%s",username,password,host,port,database,charset,url.QueryEscape(loc),)fmt.Println(dataSource, "数据库连接")db, err := gorm.Open(mysql.Open(dataSource), &gorm.Config{Logger:                                   logger.Default.LogMode(logger.Info),DisableForeignKeyConstraintWhenMigrating: true, // 自动创建表的时候不创建外键SkipDefaultTransaction:                   false,NamingStrategy: schema.NamingStrategy{ // 自动创建表时候表名的配置SingularTable: true,},})if err != nil {panic("连接数据库失败")}dao.SetDefault(db)// 多租户插件db.Use(&gormTenant.TenantPlugin{)fmt.Println("mysql启动成功...")return db
    }
    
  • 5、在操作的时候使用自己的上下文就可以,本案例使用的是go-zero来实现

    func NewGetUserListApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetUserListApiLogic {hostname, _ := os.Hostname()return &GetUserListApiLogic{Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(logx.Field("Log_UUID", uuid.New().String()),logx.Field("hostname", hostname)),ctx:     gormTenant.NewContext(ctx), // 这个替换官方生成的svcCtx:  svcCtx,Request: request,}
    }
    
  • 6、查询和不使用租户的查询方式(关于租户传值的后面会介绍)

    // 使用了租户字段
    dao.UserEntity.WithContext(l.ctx).Where(dao.UserEntity.Name.Eq("你好")).Find()
    // SELECT * FROM `user` WHERE `user`.`name` = '你好' AND `user`.`tenant_id` = 455 AND `user`.`deleted_at` IS NULL
    // 不使用租户
    dao.UserEntity.WithContext(tenant.SkipTenantContext(l.ctx)).Find()
    // SELECT * FROM `user` WHERE `user`.`deleted_at` IS NULL
    

二、关于tenant_id传递值

  • 1、在go-zero中将租户信息在中间件中写入到上下文中

    
    func (m *AuthMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {return func(w http.ResponseWriter, r *http.Request) {...ctx = context.WithValue(ctx, "tenant_id", "455")next(w, r.WithContext(ctx))}
    }
    

三、关于数据库字段不是叫tenant_id

  • 1、比如现在数据库模型租户字段叫TenantNo,直接在使用的时候传递数据库字段

    db.Use(&tenant.TenantPlugin{TenantField: "TenantNo"})
    

四、如果上下文件中定义租户字段不是叫tenant_id

  • 1、在重写ctx的时候可以传递你上下文中定义的字段

    gormTenant.NewContext(ctx,"test1") // 注意test1要保持和上下文一致就可以
    

相关文章:

  • 第十三节:第五部分:集合框架:集合嵌套
  • 攻防世界RE-happyctf
  • GO协程(Goroutine)问题总结
  • zynq远程更新程序
  • C++类二
  • 电子电路基础1(杂乱)
  • 使用 Preetham 天空模型与硬边太阳圆盘实现真实感天空渲染
  • Day 40训练
  • Unknown key: ‘auto_activate_base‘解决
  • AI变革思考2:当小众需求遇上人工智能,催生长尾应用的春天
  • 【AAOS】【源码分析】用户管理(三)-- 用户启动
  • 045-代码味道-数据泥团
  • ObservableRecipient与ObservableObject
  • 深度学习习题2
  • Python爬虫与Java爬虫深度对比:从原理到实战案例解析
  • Java 中比较两个 long 类型变量大小的方法
  • Linux网桥实战手册:从基础配置到虚拟化网络深度优化
  • N2语法 強調、限定
  • RK3588 InsightFace人脸识别移植及精度测试全解析
  • 合并表格(按行合并)
  • 人大网站建设成就/小程序开发软件
  • 河南手机网站建设公司排名/搜索引擎广告案例
  • 做网站的结论/百度指数怎么查
  • 如何用dedecms做网站/广州seo营销培训
  • 学校设计网站方案/怎么免费创建网站
  • 网站建设公司做销售好不好?/安阳企业网站优化外包