mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
feat: 多租户 TenantFactory 添加对 Spring @Configuration 自动配置的支持
This commit is contained in:
parent
6b7bb7c6cb
commit
039ef607b3
@ -60,6 +60,26 @@ public interface TenantFactory {
|
||||
除了显示租户自己的数据以外,还包含下级租户的数据,这种场景则要求 `getTenantIds` 返回多个值。
|
||||
- **场景3**:忽略租户条件,由代码自定义条件查询,此项要求 `getTenantIds` 返回 null 或者 空数组。
|
||||
|
||||
|
||||
|
||||
|
||||
## SpringBoot 支持
|
||||
在 SpringBoot 项目下,直接通过 `@Configuration` 即可使用:
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
public class MyConfiguration {
|
||||
|
||||
@Bean
|
||||
public TenantFactory tenantFactory(){
|
||||
TenantFactory tenantFactory = new ....;
|
||||
return tenantFactory;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## 注意事项
|
||||
|
||||
### 新增数据时
|
||||
@ -150,4 +170,4 @@ try {
|
||||
}
|
||||
```
|
||||
|
||||
当然,除此之外,`TenantFactory` 返回空数据,也会忽略 tenant 条件。
|
||||
当然,除此之外,`TenantFactory` 返回空数据,也会忽略 tenant 条件。
|
||||
|
||||
@ -22,6 +22,8 @@ import com.mybatisflex.core.mybatis.FlexConfiguration;
|
||||
import com.mybatisflex.core.table.DynamicSchemaProcessor;
|
||||
import com.mybatisflex.core.table.DynamicTableProcessor;
|
||||
import com.mybatisflex.core.table.TableManager;
|
||||
import com.mybatisflex.core.tenant.TenantFactory;
|
||||
import com.mybatisflex.core.tenant.TenantManager;
|
||||
import com.mybatisflex.spring.FlexSqlSessionFactoryBean;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.mapping.DatabaseIdProvider;
|
||||
@ -116,6 +118,9 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
||||
protected final DynamicTableProcessor dynamicTableProcessor;
|
||||
protected final DynamicSchemaProcessor dynamicSchemaProcessor;
|
||||
|
||||
//多租户
|
||||
protected final TenantFactory tenantFactory;
|
||||
|
||||
|
||||
public MybatisFlexAutoConfiguration(MybatisFlexProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
|
||||
ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider,
|
||||
@ -124,7 +129,8 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
||||
ObjectProvider<List<SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers,
|
||||
ObjectProvider<DataSourceDecipher> dataSourceDecipherProvider,
|
||||
ObjectProvider<DynamicTableProcessor> dynamicTableProcessorProvider,
|
||||
ObjectProvider<DynamicSchemaProcessor> dynamicSchemaProcessorProvider
|
||||
ObjectProvider<DynamicSchemaProcessor> dynamicSchemaProcessorProvider,
|
||||
ObjectProvider<TenantFactory> tenantFactoryProvider
|
||||
) {
|
||||
this.properties = properties;
|
||||
this.interceptors = interceptorsProvider.getIfAvailable();
|
||||
@ -141,6 +147,9 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
||||
//动态表名
|
||||
this.dynamicTableProcessor = dynamicTableProcessorProvider.getIfAvailable();
|
||||
this.dynamicSchemaProcessor = dynamicSchemaProcessorProvider.getIfAvailable();
|
||||
|
||||
//多租户
|
||||
this.tenantFactory = tenantFactoryProvider.getIfAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -160,6 +169,11 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
||||
if (dynamicSchemaProcessor != null) {
|
||||
TableManager.setDynamicSchemaProcessor(dynamicSchemaProcessor);
|
||||
}
|
||||
|
||||
//多租户
|
||||
if (tenantFactory != null) {
|
||||
TenantManager.setTenantFactory(tenantFactory);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkConfigFileExists() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user