mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
feat: 动态表名添加对 Spring @Configuration 自动配置的支持
This commit is contained in:
parent
7aa4857ed3
commit
6b7bb7c6cb
@ -43,4 +43,27 @@ TableManager.setDynamicSchemaProcessor(new DynamicSchemaProcessor() {
|
||||
});
|
||||
```
|
||||
|
||||
动态 Schema 的配置,只对使用了注解 `@Table(schema="xxx")` 的 Entity 有效。
|
||||
动态 Schema 的配置,只对使用了注解 `@Table(schema="xxx")` 的 Entity 有效。
|
||||
|
||||
## SpringBoot 支持
|
||||
在 SpringBoot 项目下,直接通过 `@Configuration` 即可使用:
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
public class MyConfiguration {
|
||||
|
||||
@Bean
|
||||
public DynamicTableProcessor dynamicTableProcessor(){
|
||||
DynamicTableProcessor processor = new ....;
|
||||
return processor;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public DynamicSchemaProcessor dynamicSchemaProcessor(){
|
||||
DynamicSchemaProcessor processor = new ....;
|
||||
return processor;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@ -19,6 +19,9 @@ import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.datasource.DataSourceDecipher;
|
||||
import com.mybatisflex.core.datasource.DataSourceManager;
|
||||
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.spring.FlexSqlSessionFactoryBean;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.mapping.DatabaseIdProvider;
|
||||
@ -106,15 +109,23 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
||||
|
||||
protected final List<SqlSessionFactoryBeanCustomizer> sqlSessionFactoryBeanCustomizers;
|
||||
|
||||
//数据源解密器
|
||||
protected final DataSourceDecipher dataSourceDecipher;
|
||||
|
||||
//动态表名
|
||||
protected final DynamicTableProcessor dynamicTableProcessor;
|
||||
protected final DynamicSchemaProcessor dynamicSchemaProcessor;
|
||||
|
||||
|
||||
public MybatisFlexAutoConfiguration(MybatisFlexProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
|
||||
ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider,
|
||||
ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider,
|
||||
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider,
|
||||
ObjectProvider<List<SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers,
|
||||
ObjectProvider<DataSourceDecipher> dataSourceDecipherProvider) {
|
||||
ObjectProvider<DataSourceDecipher> dataSourceDecipherProvider,
|
||||
ObjectProvider<DynamicTableProcessor> dynamicTableProcessorProvider,
|
||||
ObjectProvider<DynamicSchemaProcessor> dynamicSchemaProcessorProvider
|
||||
) {
|
||||
this.properties = properties;
|
||||
this.interceptors = interceptorsProvider.getIfAvailable();
|
||||
this.typeHandlers = typeHandlersProvider.getIfAvailable();
|
||||
@ -126,16 +137,29 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
||||
|
||||
//数据密码
|
||||
this.dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable();
|
||||
|
||||
//动态表名
|
||||
this.dynamicTableProcessor = dynamicTableProcessorProvider.getIfAvailable();
|
||||
this.dynamicSchemaProcessor = dynamicSchemaProcessorProvider.getIfAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
// 检测 MyBatis 原生配置文件是否存在
|
||||
checkConfigFileExists();
|
||||
|
||||
// 添加 MyBatis-Flex 全局配置
|
||||
if (properties.getGlobalConfig() != null) {
|
||||
properties.getGlobalConfig().applyTo(FlexGlobalConfig.getDefaultConfig());
|
||||
}
|
||||
|
||||
// 动态表名配置
|
||||
if (dynamicTableProcessor != null) {
|
||||
TableManager.setDynamicTableProcessor(dynamicTableProcessor);
|
||||
}
|
||||
if (dynamicSchemaProcessor != null) {
|
||||
TableManager.setDynamicSchemaProcessor(dynamicSchemaProcessor);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkConfigFileExists() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user