mirror of https://gitee.com/stylefeng/roses
【8.3.4】【mp】调整租户的相关接口
parent
3956efe691
commit
979cc77555
|
@ -107,4 +107,9 @@ public interface RuleConstants {
|
|||
*/
|
||||
String TENANT_DB_PREFIX = "sys_tenant_db_";
|
||||
|
||||
/**
|
||||
* 默认租户ID
|
||||
*/
|
||||
Long DEFAULT_ROOT_TENANT_ID = 1L;
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package cn.stylefeng.roses.kernel.db.mp.tenant;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.pojo.TenantSwitchInfo;
|
||||
|
||||
/**
|
||||
* 租户业务的相关API
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/4 18:35
|
||||
*/
|
||||
public interface SaasServiceApi {
|
||||
|
||||
/**
|
||||
* 通过租户id获取租户的切换信息
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/4 18:35
|
||||
*/
|
||||
TenantSwitchInfo getTenantSwitchInfo(Long tenantId);
|
||||
|
||||
/**
|
||||
* 通过租户编码获取租户的去切换信息
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/5 11:31
|
||||
*/
|
||||
TenantSwitchInfo getTenantSwitchInfo(String tenantCode);
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package cn.stylefeng.roses.kernel.db.mp.tenant;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.pojo.TenantSwitchInfo;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 统一切换当前租户的API
|
||||
* <p>
|
||||
* 根据租户的数据源配置,判断是否切分租户id或者租户的独立数据源
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/5 10:59
|
||||
*/
|
||||
public interface TenantSwitchApi {
|
||||
|
||||
/**
|
||||
* 根据租户编码切换租户,并执行相关业务
|
||||
*
|
||||
* @param tenantCode 租户的编码
|
||||
* @param action 需要切换租户的代码
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/4 17:23
|
||||
*/
|
||||
<T> T changeTenant(String tenantCode, Supplier<T> action);
|
||||
|
||||
/**
|
||||
* 根据租户id切换租户,并执行相关业务
|
||||
*
|
||||
* @param tenantId 租户的id
|
||||
* @param action 需要切换租户的代码
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/4 17:23
|
||||
*/
|
||||
<T> T changeTenant(Long tenantId, Supplier<T> action);
|
||||
|
||||
/**
|
||||
* 切换租户,并执行相关业务
|
||||
*
|
||||
* @param tenantSwitchInfo 租户的详细信息
|
||||
* @param action 需要切换租户的代码
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/4 17:23
|
||||
*/
|
||||
<T> T changeTenant(TenantSwitchInfo tenantSwitchInfo, Supplier<T> action);
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package cn.stylefeng.roses.kernel.db.mp.tenant.context;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.TenantSwitchApi;
|
||||
|
||||
/**
|
||||
* 租户切换的上下文,快捷工具
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/5 11:21
|
||||
*/
|
||||
public class TenantSwitchContext {
|
||||
|
||||
public TenantSwitchContext() {
|
||||
}
|
||||
|
||||
public static TenantSwitchApi me() {
|
||||
return SpringUtil.getBean(TenantSwitchApi.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package cn.stylefeng.roses.kernel.db.mp.tenant.defaultimpl;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.SaasServiceApi;
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.pojo.TenantSwitchInfo;
|
||||
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
|
||||
|
||||
/**
|
||||
* 默认的租户业务的实现
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/5 21:06
|
||||
*/
|
||||
public class DefaultSaasServiceImpl implements SaasServiceApi {
|
||||
|
||||
@Override
|
||||
public TenantSwitchInfo getTenantSwitchInfo(Long tenantId) {
|
||||
return new TenantSwitchInfo(RuleConstants.DEFAULT_ROOT_TENANT_ID, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TenantSwitchInfo getTenantSwitchInfo(String tenantCode) {
|
||||
return new TenantSwitchInfo(RuleConstants.DEFAULT_ROOT_TENANT_ID, tenantCode, null);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package cn.stylefeng.roses.kernel.db.mp.tenant.defaultimpl;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.TenantSwitchApi;
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.pojo.TenantSwitchInfo;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 某人的租户切换的实现
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/5 21:06
|
||||
*/
|
||||
public class DefaultTenantSwitchImpl implements TenantSwitchApi {
|
||||
|
||||
@Override
|
||||
public <T> T changeTenant(String tenantCode, Supplier<T> action) {
|
||||
return action.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T changeTenant(Long tenantId, Supplier<T> action) {
|
||||
return action.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T changeTenant(TenantSwitchInfo tenantSwitchInfo, Supplier<T> action) {
|
||||
return action.get();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package cn.stylefeng.roses.kernel.db.mp.tenant.pojo;
|
||||
|
||||
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 租户切换信息
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/4 20:38
|
||||
*/
|
||||
@Data
|
||||
public class TenantSwitchInfo {
|
||||
|
||||
/**
|
||||
* 租户id
|
||||
*/
|
||||
@ChineseDescription("租户id")
|
||||
private Long tenantId;
|
||||
|
||||
/**
|
||||
* 租户唯一标识
|
||||
*/
|
||||
@ChineseDescription("租户唯一标识")
|
||||
private String tenantCode;
|
||||
|
||||
/**
|
||||
* 数据隔离方式:1-租户id隔离,2-数据库分离
|
||||
*/
|
||||
@ChineseDescription("数据隔离方式:1-租户id隔离,2-数据库分离")
|
||||
private Integer dataMode;
|
||||
|
||||
public TenantSwitchInfo() {
|
||||
}
|
||||
|
||||
public TenantSwitchInfo(Long tenantId, String tenantCode, Integer dataMode) {
|
||||
this.tenantId = tenantId;
|
||||
this.tenantCode = tenantCode;
|
||||
this.dataMode = dataMode;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.db.starter;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.SaasServiceApi;
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.TenantSwitchApi;
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.defaultimpl.DefaultSaasServiceImpl;
|
||||
import cn.stylefeng.roses.kernel.db.mp.tenant.defaultimpl.DefaultTenantSwitchImpl;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 租户相关的自动配置
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/5 21:03
|
||||
*/
|
||||
@Configuration
|
||||
public class ProjectTenantAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 租户数据获取的实现
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/5 21:07
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SaasServiceApi saasServiceApi() {
|
||||
return new DefaultSaasServiceImpl();
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认租户切换
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2025/5/5 21:08
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public TenantSwitchApi tenantSwitchApi() {
|
||||
return new DefaultTenantSwitchImpl();
|
||||
}
|
||||
|
||||
}
|
|
@ -24,6 +24,8 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.sys.api.constants;
|
||||
|
||||
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
|
||||
|
||||
/**
|
||||
* 基础核心业务业务
|
||||
*
|
||||
|
@ -75,6 +77,6 @@ public interface SysConstants {
|
|||
/**
|
||||
* 默认租户ID
|
||||
*/
|
||||
Long DEFAULT_ROOT_TENANT_ID = 1L;
|
||||
Long DEFAULT_ROOT_TENANT_ID = RuleConstants.DEFAULT_ROOT_TENANT_ID;
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue