mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 17:48:25 +08:00
feat: 动态表名添加对 Spring @Configuration 自动配置的支持
This commit is contained in:
parent
7aa4857ed3
commit
6b7bb7c6cb
@ -44,3 +44,26 @@ 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.DataSourceDecipher;
|
||||||
import com.mybatisflex.core.datasource.DataSourceManager;
|
import com.mybatisflex.core.datasource.DataSourceManager;
|
||||||
import com.mybatisflex.core.mybatis.FlexConfiguration;
|
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 com.mybatisflex.spring.FlexSqlSessionFactoryBean;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.mapping.DatabaseIdProvider;
|
import org.apache.ibatis.mapping.DatabaseIdProvider;
|
||||||
@ -106,15 +109,23 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
|||||||
|
|
||||||
protected final List<SqlSessionFactoryBeanCustomizer> sqlSessionFactoryBeanCustomizers;
|
protected final List<SqlSessionFactoryBeanCustomizer> sqlSessionFactoryBeanCustomizers;
|
||||||
|
|
||||||
|
//数据源解密器
|
||||||
protected final DataSourceDecipher dataSourceDecipher;
|
protected final DataSourceDecipher dataSourceDecipher;
|
||||||
|
|
||||||
|
//动态表名
|
||||||
|
protected final DynamicTableProcessor dynamicTableProcessor;
|
||||||
|
protected final DynamicSchemaProcessor dynamicSchemaProcessor;
|
||||||
|
|
||||||
|
|
||||||
public MybatisFlexAutoConfiguration(MybatisFlexProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
|
public MybatisFlexAutoConfiguration(MybatisFlexProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
|
||||||
ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider,
|
ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider,
|
||||||
ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider,
|
ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider,
|
||||||
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider,
|
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider,
|
||||||
ObjectProvider<List<SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers,
|
ObjectProvider<List<SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers,
|
||||||
ObjectProvider<DataSourceDecipher> dataSourceDecipherProvider) {
|
ObjectProvider<DataSourceDecipher> dataSourceDecipherProvider,
|
||||||
|
ObjectProvider<DynamicTableProcessor> dynamicTableProcessorProvider,
|
||||||
|
ObjectProvider<DynamicSchemaProcessor> dynamicSchemaProcessorProvider
|
||||||
|
) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
this.interceptors = interceptorsProvider.getIfAvailable();
|
this.interceptors = interceptorsProvider.getIfAvailable();
|
||||||
this.typeHandlers = typeHandlersProvider.getIfAvailable();
|
this.typeHandlers = typeHandlersProvider.getIfAvailable();
|
||||||
@ -126,16 +137,29 @@ public class MybatisFlexAutoConfiguration implements InitializingBean {
|
|||||||
|
|
||||||
//数据密码
|
//数据密码
|
||||||
this.dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable();
|
this.dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable();
|
||||||
|
|
||||||
|
//动态表名
|
||||||
|
this.dynamicTableProcessor = dynamicTableProcessorProvider.getIfAvailable();
|
||||||
|
this.dynamicSchemaProcessor = dynamicSchemaProcessorProvider.getIfAvailable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
// 检测 MyBatis 原生配置文件是否存在
|
// 检测 MyBatis 原生配置文件是否存在
|
||||||
checkConfigFileExists();
|
checkConfigFileExists();
|
||||||
|
|
||||||
// 添加 MyBatis-Flex 全局配置
|
// 添加 MyBatis-Flex 全局配置
|
||||||
if (properties.getGlobalConfig() != null) {
|
if (properties.getGlobalConfig() != null) {
|
||||||
properties.getGlobalConfig().applyTo(FlexGlobalConfig.getDefaultConfig());
|
properties.getGlobalConfig().applyTo(FlexGlobalConfig.getDefaultConfig());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 动态表名配置
|
||||||
|
if (dynamicTableProcessor != null) {
|
||||||
|
TableManager.setDynamicTableProcessor(dynamicTableProcessor);
|
||||||
|
}
|
||||||
|
if (dynamicSchemaProcessor != null) {
|
||||||
|
TableManager.setDynamicSchemaProcessor(dynamicSchemaProcessor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkConfigFileExists() {
|
private void checkConfigFileExists() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user