This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package jnpf.util;
|
||||
|
||||
import jnpf.base.UserInfo;
|
||||
import jnpf.config.ConfigValueUtil;
|
||||
import jnpf.database.util.TenantDataSourceUtil;
|
||||
import jnpf.exception.LoginException;
|
||||
import jnpf.util.data.DataSourceContextHolder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Component
|
||||
public class TenantUtil {
|
||||
@Autowired
|
||||
private ConfigValueUtil configValueUtil;
|
||||
|
||||
public void switchTenant(){
|
||||
switchTenant("yawen");
|
||||
}
|
||||
public void switchTenant(String tenantId){
|
||||
// 判断是否为多租户
|
||||
if (configValueUtil.isMultiTenancy()) {
|
||||
// 判断是不是从外面直接请求
|
||||
if (StringUtil.isNotEmpty(tenantId)) {
|
||||
//切换成租户库
|
||||
try {
|
||||
TenantDataSourceUtil.switchTenant(tenantId);
|
||||
} catch (LoginException e) {
|
||||
throw new RuntimeException("切换租户失败");
|
||||
}
|
||||
} else {
|
||||
UserInfo userInfo = UserProvider.getUser();
|
||||
Assert.notNull(userInfo.getUserId(), "缺少租户信息");
|
||||
DataSourceContextHolder.setDatasource(userInfo.getTenantId(), userInfo.getTenantDbConnectionString(), userInfo.isAssignDataSource());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user