同步自动配置,减少无用的配置提示.

This commit is contained in:
nieqiurong 2023-09-23 17:19:02 +08:00 committed by hubin
parent 48a61582a4
commit 5fc31de908
4 changed files with 262 additions and 18 deletions

View File

@ -267,10 +267,14 @@ public class MybatisPlusAutoConfiguration implements InitializingBean {
// TODO 入参使用 MybatisSqlSessionFactoryBean // TODO 入参使用 MybatisSqlSessionFactoryBean
private void applyConfiguration(MybatisSqlSessionFactoryBean factory) { private void applyConfiguration(MybatisSqlSessionFactoryBean factory) {
// TODO 使用 MybatisConfiguration // TODO 使用 MybatisConfiguration
MybatisConfiguration configuration = this.properties.getConfiguration(); MybatisPlusProperties.CoreConfiguration coreConfiguration = this.properties.getConfiguration();
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) { MybatisConfiguration configuration = null;
if (coreConfiguration != null || !StringUtils.hasText(this.properties.getConfigLocation())) {
configuration = new MybatisConfiguration(); configuration = new MybatisConfiguration();
} }
if (configuration != null && coreConfiguration != null) {
coreConfiguration.applyTo(configuration);
}
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) { if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
for (ConfigurationCustomizer customizer : this.configurationCustomizers) { for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
customizer.customize(configuration); customizer.customize(configuration);

View File

@ -15,16 +15,27 @@
*/ */
package com.baomidou.mybatisplus.autoconfigure; package com.baomidou.mybatisplus.autoconfigure;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig; import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils; import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.scripting.LanguageDriver; import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.AutoMappingBehavior;
import org.apache.ibatis.session.AutoMappingUnknownColumnBehavior;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.LocalCacheScope;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver;
@ -32,6 +43,7 @@ import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.IOException; import java.io.IOException;
import java.util.Optional; import java.util.Optional;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.stream.Stream; import java.util.stream.Stream;
/** /**
@ -100,10 +112,8 @@ public class MybatisPlusProperties {
/** /**
* A Configuration object for customize default settings. If {@link #configLocation} * A Configuration object for customize default settings. If {@link #configLocation}
* is specified, this property is not used. * is specified, this property is not used.
* TODO 使用 MybatisConfiguration
*/ */
@NestedConfigurationProperty private CoreConfiguration configuration;
private MybatisConfiguration configuration;
/** /**
* 不再需要这个配置,放心删除 * 不再需要这个配置,放心删除
@ -132,4 +142,237 @@ public class MybatisPlusProperties {
return new Resource[0]; return new Resource[0];
} }
} }
/**
* The configuration properties for mybatis core module.
* 虽然为高版本新增开始,但为了美化配置提示,这里也在SpringBoot2上使用.
*
* @since 3.0.0
*/
@Getter
@Setter
public static class CoreConfiguration {
/**
* Allows using RowBounds on nested statements. If allowed, set the false. Default is false.
*/
private Boolean safeRowBoundsEnabled;
/**
* Allows using ResultHandler on nested statements. If allowed, set the false. Default is true.
*/
private Boolean safeResultHandlerEnabled;
/**
* Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names
* aColumn. Default is false.
*/
private Boolean mapUnderscoreToCamelCase;
/**
* When enabled, any method call will load all the lazy properties of the object. Otherwise, each property is loaded
* on demand (see also lazyLoadTriggerMethods). Default is false.
*/
private Boolean aggressiveLazyLoading;
/**
* Allows or disallows multiple ResultSets to be returned from a single statement (compatible driver required).
* Default is true.
*/
private Boolean multipleResultSetsEnabled;
/**
* Allows JDBC support for generated keys. A compatible driver is required. This setting forces generated keys to be
* used if set to true, as some drivers deny compatibility but still work (e.g. Derby). Default is false.
*/
private Boolean useGeneratedKeys;
/**
* Uses the column label instead of the column name. Different drivers behave differently in this respect. Refer to
* the driver documentation, or test out both modes to determine how your driver behaves. Default is true.
*/
private Boolean useColumnLabel;
/**
* Globally enables or disables any caches configured in any mapper under this configuration. Default is true.
*/
private Boolean cacheEnabled;
/**
* Specifies if setters or map's put method will be called when a retrieved value is null. It is useful when you
* rely on Map.keySet() or null value initialization. Note primitives such as (int,boolean,etc.) will not be set to
* null. Default is false.
*/
private Boolean callSettersOnNulls;
/**
* Allow referencing statement parameters by their actual names declared in the method signature. To use this
* feature, your project must be compiled in Java 8 with -parameters option. Default is true.
*/
private Boolean useActualParamName;
/**
* MyBatis, by default, returns null when all the columns of a returned row are NULL. When this setting is enabled,
* MyBatis returns an empty instance instead. Note that it is also applied to nested results (i.e. collection and
* association). Default is false.
*/
private Boolean returnInstanceForEmptyRow;
/**
* Removes extra whitespace characters from the SQL. Note that this also affects literal strings in SQL. Default is
* false.
*/
private Boolean shrinkWhitespacesInSql;
/**
* Specifies the default value of 'nullable' attribute on 'foreach' tag. Default is false.
*/
private Boolean nullableOnForEach;
/**
* When applying constructor auto-mapping, argument name is used to search the column to map instead of relying on
* the column order. Default is false.
*/
private Boolean argNameBasedConstructorAutoMapping;
/**
* Globally enables or disables lazy loading. When enabled, all relations will be lazily loaded. This value can be
* superseded for a specific relation by using the fetchType attribute on it. Default is False.
*/
private Boolean lazyLoadingEnabled;
/**
* Sets the number of seconds the driver will wait for a response from the database.
*/
private Integer defaultStatementTimeout;
/**
* Sets the driver a hint as to control fetching size for return results. This parameter value can be overridden by a
* query setting.
*/
private Integer defaultFetchSize;
/**
* MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default,
* (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT local session will be
* used just for statement execution, no data will be shared between two different calls to the same SqlSession.
* Default is SESSION.
*/
private LocalCacheScope localCacheScope;
/**
* Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers
* require specifying the column JDBC type but others work with generic values like NULL, VARCHAR or OTHER. Default
* is OTHER.
*/
private JdbcType jdbcTypeForNull;
/**
* Specifies a scroll strategy when omit it per statement settings.
*/
private ResultSetType defaultResultSetType;
/**
* Configures the default executor. SIMPLE executor does nothing special. REUSE executor reuses prepared statements.
* BATCH executor reuses statements and batches updates. Default is SIMPLE.
*/
private ExecutorType defaultExecutorType;
/**
* Specifies if and how MyBatis should automatically map columns to fields/properties. NONE disables auto-mapping.
* PARTIAL will only auto-map results with no nested result mappings defined inside. FULL will auto-map result
* mappings of any complexity (containing nested or otherwise). Default is PARTIAL.
*/
private AutoMappingBehavior autoMappingBehavior;
/**
* Specify the behavior when detects an unknown column (or unknown property type) of automatic mapping target.
* Default is NONE.
*/
private AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior;
/**
* Specifies the prefix string that MyBatis will add to the logger names.
*/
private String logPrefix;
/**
* Specifies which Object's methods trigger a lazy load. Default is [equals,clone,hashCode,toString].
*/
private Set<String> lazyLoadTriggerMethods;
/**
* Specifies which logging implementation MyBatis should use. If this setting is not present logging implementation
* will be discovered.
*/
private Class<? extends Log> logImpl;
/**
* Specifies VFS implementations.
*/
private Class<? extends VFS> vfsImpl;
/**
* Specifies a sql provider class that holds provider method. This class apply to the type(or value) attribute on
* sql provider annotation(e.g. @SelectProvider), when these attribute was omitted.
*/
private Class<?> defaultSqlProviderType;
/**
* Specifies the TypeHandler used by default for Enum.
*/
Class<? extends TypeHandler> defaultEnumTypeHandler;
/**
* Specifies the class that provides an instance of Configuration. The returned Configuration instance is used to
* load lazy properties of deserialized objects. This class must have a method with a signature static Configuration
* getConfiguration().
*/
private Class<?> configurationFactory;
/**
* Specify any configuration variables.
*/
private Properties variables;
// 新增兼容开始...
private Class<? extends LanguageDriver> defaultScriptingLanguageDriver;
public void applyTo(Configuration target) {
PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
mapper.from(getSafeRowBoundsEnabled()).to(target::setSafeRowBoundsEnabled);
mapper.from(getSafeResultHandlerEnabled()).to(target::setSafeResultHandlerEnabled);
mapper.from(getMapUnderscoreToCamelCase()).to(target::setMapUnderscoreToCamelCase);
mapper.from(getAggressiveLazyLoading()).to(target::setAggressiveLazyLoading);
mapper.from(getMultipleResultSetsEnabled()).to(target::setMultipleResultSetsEnabled);
mapper.from(getUseGeneratedKeys()).to(target::setUseGeneratedKeys);
mapper.from(getUseColumnLabel()).to(target::setUseColumnLabel);
mapper.from(getCacheEnabled()).to(target::setCacheEnabled);
mapper.from(getCallSettersOnNulls()).to(target::setCallSettersOnNulls);
mapper.from(getUseActualParamName()).to(target::setUseActualParamName);
mapper.from(getReturnInstanceForEmptyRow()).to(target::setReturnInstanceForEmptyRow);
mapper.from(getShrinkWhitespacesInSql()).to(target::setShrinkWhitespacesInSql);
mapper.from(getNullableOnForEach()).to(target::setNullableOnForEach);
mapper.from(getArgNameBasedConstructorAutoMapping()).to(target::setArgNameBasedConstructorAutoMapping);
mapper.from(getLazyLoadingEnabled()).to(target::setLazyLoadingEnabled);
mapper.from(getLogPrefix()).to(target::setLogPrefix);
mapper.from(getLazyLoadTriggerMethods()).to(target::setLazyLoadTriggerMethods);
mapper.from(getDefaultStatementTimeout()).to(target::setDefaultStatementTimeout);
mapper.from(getDefaultFetchSize()).to(target::setDefaultFetchSize);
mapper.from(getLocalCacheScope()).to(target::setLocalCacheScope);
mapper.from(getJdbcTypeForNull()).to(target::setJdbcTypeForNull);
mapper.from(getDefaultResultSetType()).to(target::setDefaultResultSetType);
mapper.from(getDefaultExecutorType()).to(target::setDefaultExecutorType);
mapper.from(getAutoMappingBehavior()).to(target::setAutoMappingBehavior);
mapper.from(getAutoMappingUnknownColumnBehavior()).to(target::setAutoMappingUnknownColumnBehavior);
mapper.from(getVariables()).to(target::setVariables);
mapper.from(getLogImpl()).to(target::setLogImpl);
mapper.from(getVfsImpl()).to(target::setVfsImpl);
mapper.from(getDefaultSqlProviderType()).to(target::setDefaultSqlProviderType);
mapper.from(getConfigurationFactory()).to(target::setConfigurationFactory);
mapper.from(getDefaultEnumTypeHandler()).to(target::setDefaultEnumTypeHandler);
mapper.from(getDefaultScriptingLanguageDriver()).to(target::setDefaultScriptingLanguage);
}
}
} }

View File

@ -9,7 +9,7 @@
"sourceType": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties", "sourceType": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties",
"name": "mybatis-plus.configuration", "name": "mybatis-plus.configuration",
"sourceMethod": "getConfiguration()", "sourceMethod": "getConfiguration()",
"type": "com.baomidou.mybatisplus.core.MybatisConfiguration" "type": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties$CoreConfiguration"
}, },
{ {
"sourceType": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties", "sourceType": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties",
@ -79,24 +79,21 @@
"sourceType": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties", "sourceType": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties",
"name": "mybatis-plus.default-scripting-language-driver", "name": "mybatis-plus.default-scripting-language-driver",
"defaultValue": "com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver", "defaultValue": "com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver",
"type": "java.lang.Class<? extends org.apache.ibatis.scripting.LanguageDriver>", "type": "java.lang.Class<? extends org.apache.ibatis.scripting.LanguageDriver>"
"deprecation": {
"level": "error",
"reason": "如果修改了该值,你会至少失去几乎所有 mp 提供的功能."
}
}, },
{ {
"sourceType": "com.baomidou.mybatisplus.core.MybatisConfiguration", "sourceType": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties$CoreConfiguration",
"name": "mybatis-plus.configuration.default-scripting-language", "name": "mybatis-plus.configuration.default-scripting-language",
"defaultValue": "com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver", "defaultValue": "com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver",
"type": "java.lang.Class<? extends org.apache.ibatis.scripting.LanguageDriver>", "type": "java.lang.Class<? extends org.apache.ibatis.scripting.LanguageDriver>",
"deprecation": { "deprecation": {
"level": "error", "level": "error",
"reason": "设置无效." "reason": "设置无效.",
"replacement": "mybatis-plus.configuration.default-scripting-language-driver"
} }
}, },
{ {
"sourceType": "com.baomidou.mybatisplus.core.MybatisConfiguration", "sourceType": "com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties$CoreConfiguration",
"name": "mybatis-plus.configuration.default-enum-type-handler", "name": "mybatis-plus.configuration.default-enum-type-handler",
"defaultValue": "org.apache.ibatis.type.EnumTypeHandler", "defaultValue": "org.apache.ibatis.type.EnumTypeHandler",
"description": "A default TypeHandler class for Enum.", "description": "A default TypeHandler class for Enum.",

View File

@ -72,8 +72,8 @@ class MetadataTest {
Assertions.assertEquals(metadataMap.get("mybatis-plus.type-aliases-package"), new Metadata("mybatis-plus.type-aliases-package", String.class.getName(), MybatisPlusProperties.class.getName())); Assertions.assertEquals(metadataMap.get("mybatis-plus.type-aliases-package"), new Metadata("mybatis-plus.type-aliases-package", String.class.getName(), MybatisPlusProperties.class.getName()));
Assertions.assertEquals(metadataMap.get("mybatis-plus.global-config.db-config.table-underline"), new Metadata("mybatis-plus.global-config.db-config.table-underline", Boolean.class.getName(), GlobalConfig.DbConfig.class.getName())); Assertions.assertEquals(metadataMap.get("mybatis-plus.global-config.db-config.table-underline"), new Metadata("mybatis-plus.global-config.db-config.table-underline", Boolean.class.getName(), GlobalConfig.DbConfig.class.getName()));
Assertions.assertEquals(metadataMap.get("mybatis-plus.config-location"), new Metadata("mybatis-plus.config-location", String.class.getName(), MybatisPlusProperties.class.getName())); Assertions.assertEquals(metadataMap.get("mybatis-plus.config-location"), new Metadata("mybatis-plus.config-location", String.class.getName(), MybatisPlusProperties.class.getName()));
Assertions.assertEquals(metadataMap.get("mybatis-plus.configuration.call-setters-on-nulls"), new Metadata("mybatis-plus.configuration.call-setters-on-nulls", Boolean.class.getName(), MybatisConfiguration.class.getName())); Assertions.assertEquals(metadataMap.get("mybatis-plus.configuration.call-setters-on-nulls"), new Metadata("mybatis-plus.configuration.call-setters-on-nulls", Boolean.class.getName(), MybatisPlusProperties.CoreConfiguration.class.getName()));
Assertions.assertEquals(metadataMap.get("mybatis-plus.configuration.jdbc-type-for-null"), new Metadata("mybatis-plus.configuration.jdbc-type-for-null", JdbcType.class.getName(), MybatisConfiguration.class.getName())); Assertions.assertEquals(metadataMap.get("mybatis-plus.configuration.jdbc-type-for-null"), new Metadata("mybatis-plus.configuration.jdbc-type-for-null", JdbcType.class.getName(), MybatisPlusProperties.CoreConfiguration.class.getName()));
Assertions.assertEquals(metadataMap.get("mybatis-plus.configuration.map-underscore-to-camel-case"), new Metadata("mybatis-plus.configuration.map-underscore-to-camel-case", Boolean.class.getName(), MybatisConfiguration.class.getName())); Assertions.assertEquals(metadataMap.get("mybatis-plus.configuration.map-underscore-to-camel-case"), new Metadata("mybatis-plus.configuration.map-underscore-to-camel-case", Boolean.class.getName(), MybatisPlusProperties.CoreConfiguration.class.getName()));
} }
} }