Skip to content

Latest commit

 

History

History
55 lines (39 loc) · 1.45 KB

qk-money-tenant.md

File metadata and controls

55 lines (39 loc) · 1.45 KB

租户模块

​ 该模块使用Mybatis-Plus多租户插件提供基于表字段的多租户功能。需要为进行隔离的数据表添加 tenant_id字段,开启多租户后,所有执行的Mybatis-Plus方法sql都会自动进行过滤处理,无需显式添加条件。对于自写的sql按照规范书写依旧能自动处理(sql涉及到多个表的每个表都要给别名,特别是 inner join 的要写标准的 inner join)。

依赖

<!-- 多租户模块 -->
<dependency>
    <groupId>com.money</groupId>
    <artifactId>qk-money-tenant</artifactId>
</dependency>

使用

  1. 为需要租户的表添加tenant_id字段

    image-20221030102155484

  2. 配置不需要租户的表

    money:
      # 多租户
      tenant:
        # 开关
        enabled: true
        # 请求头
        header: Y-tenant
        # 默认租户id
        default-tenant-id: "0"
        # 忽略的表
        ignore-table:
          - sys_tenant
          - sys_dict
          - sys_dict_detail
  3. 前端请求时请求头携带租户id

    image-20221030102356904

获取当前租户id

​ 使用租户上下文TenantContextHolder

Long tenantId = TenantContextHolder.getTenant();

延伸阅读

Mybatis-Plus多租户插件