JeecgBoot开发多租户SAAS数据隔离,查询数据库,改造多租户后 Mybatis-plus 查询数据库的SQL语句tenant-id[租户Id] 一直为0

JeecgBoot开发多租户SAAS查询数据或者对外开发接口的时候会自动进行数据隔离

1、登录状态的情况下

从登录状态当中获取最新的tenantId

LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

String tenantId = loginUser.getTenantId();

从header当中获取

String tenantId = req.getHeader("tenant-id");

在查询数据库的Service和Mapper之前设置

TenantContext.setTenant(tenantId);
xxService.list();
TenantContext.setTenant(tenantId);
xxMapper.list();

2、直接可以从前端的传入的参数当中获取tenantId

TenantContext.setTenant(tenantId);
xxService.list();
TenantContext.setTenant(tenantId);
xxMapper.list();

猜你喜欢

转载自blog.csdn.net/qq_44757034/article/details/128157502