赞
踩
EnableTransactionManagement(proxyTargetClass = true) @Configuration @MapperScan("${mybatis-plus.mapperPackage}") public class MybatisPlusConfig { /** * 默认租户字段 */ private static final String TENANT_FIELD_NAME = "t_id"; /** * 租户1 */ private static final String ENTERPRISE_FIELD_NAME = "e_id"; /** * 租户2 */ private static final String PROVIDER_FIELD_NAME = "p_id"; /** * 哪些表需要做多租户 */ private static final List<String> TENANT_TABLES = new ArrayList<>(); static { // 那些表有多租户 //用户、角色、部门 TENANT_TABLES.add("sys_user"); TENANT_TABLES.add("sys_dept"); } @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { @Override public Expression getTenantId() { Long tId = TenantContext.getTenantId(); if (tId == null) { tId = -1L; } return new LongValue(tId); } @Override public String getTenantIdColumn() { Integer tenantType = TenantContext.getTenantType(); if (tenantType != null) { if (PlatformEnum.PROVIDER.getCode() == tenantType) { return PROVIDER_FIELD_NAME; } else if (PlatformEnum.ENTERPRISE.getCode() == tenantType) { return ENTERPRISE_FIELD_NAME; } else { return TENANT_FIELD_NAME; } } else { return TENANT_FIELD_NAME; } } // 返回 true 表示不走租户逻辑 需要在SQL上加 @InterceptorIgnore(tenantLine = "true") @Override public boolean ignoreTable(String tableName) { if (TenantContext.getTenantType() == null){ return true; } //服务商和企业公共的 if (TenantContext.getTenantType() == PlatformEnum.PROVIDER.getCode() || TenantContext.getTenantType() == PlatformEnum.ENTERPRISE.getCode()) { return !TENANT_TABLES.stream().anyMatch(i -> i.equalsIgnoreCase(tableName)); } //服务商需要额外的 if (TenantContext.getTenantType() == PlatformEnum.PROVIDER.getCode()) { return !"sys_enterprise".equalsIgnoreCase(tableName); } return true; } })); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。