- MyBatis 框架配置文件有兩種:
- 1是主配置文件 mybatis-config.xml
- 2是配置執(zhí)行 sql 的 MApper文件(接口或者 xml)
- mybatis 定義了一系列屬性來控制 mybatis 運(yùn)行時(shí)的行為,這些屬性可以在主配置通過 <setting>標(biāo)簽指定
- mybatis 所有屬性的含義可以在官方文檔查看 https://mybatis.org/mybatis-3/zh/configuration.html#settings
Configuaration 類字段注釋
public class Configration {
protected Environment environment;
// 允許 JDBC 支持自動(dòng)生成主鍵,需要驅(qū)動(dòng)兼容。
// 這就是insert時(shí)獲取MySQL自增主鍵/oracle sequence的開關(guān)。
// 注:一般來說,這是希望的結(jié)果,應(yīng)該默認(rèn)值為true比較合適。
protected boolean useGeneratedKeys;
// 是否啟用緩存
protected boolean cacheEnabled = true;
// 指定 MyBatis 增加到日志名稱的前綴。
protected String logPrefix;
// 指定 MyBatis 所用日志的具體實(shí)現(xiàn),未指定時(shí)將自動(dòng)查找。一般建議指定為slf4j或log4j
protected Class<? extends Log> logImpl;
// 當(dāng)沒有為參數(shù)提供特定的 JDBC 類型時(shí),為空值指定 JDBC 類型。 某些驅(qū)動(dòng)需要指定列的 JDBC 類型,多數(shù)情況直接用一般類型即可,比如 NULL、VARCHAR 或 OTHER。
protected JdbcType jdbcTypeForNull = JdbcType.OTHER;
// settings下的properties屬性
protected Properties variables = new Properties();
// 默認(rèn)的反射器工廠,用于操作屬性、構(gòu)造器方便
protected ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
// 對(duì)象工廠, 所有的類resultMap類都需要依賴于對(duì)象工廠來實(shí)例化
protected ObjectFactory objectFactory = new DefaultObjectFactory();
// 對(duì)象包裝器工廠,主要用來在創(chuàng)建非原生對(duì)象,比如增加了某些監(jiān)控或者特殊屬性的代理類
protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
// 延遲加載的全局開關(guān)。當(dāng)開啟時(shí),所有關(guān)聯(lián)對(duì)象都會(huì)延遲加載。特定關(guān)聯(lián)關(guān)系中可通過設(shè)置fetchType屬性來覆蓋該項(xiàng)的開關(guān)狀態(tài)。
protected boolean lazyLoadingEnabled = false;
// 指定 Mybatis 創(chuàng)建具有延遲加載能力的對(duì)象所用到的代理工具。MyBatis 3.3+使用JAVASSIST
protected ProxyFactory proxyFactory = new JavassistProxyFactory(); // #224 Using internal Javassist instead of OGNL
// MyBatis 可以根據(jù)不同的數(shù)據(jù)庫(kù)廠商執(zhí)行不同的語句,這種多廠商的支持是基于映射語句中的 databaseId 屬性。
protected String databaseId;
/**
* configuration 存儲(chǔ)屬性之外的數(shù)據(jù): 類型處理 TypeHandler,類型別名typeAlias,Mapper接口
* 以下字段信息會(huì)在啟動(dòng)的時(shí)候注冊(cè)到 configuration 對(duì)象
*/
// 存儲(chǔ)namespace與MapperProxyFactory<mapper接口> 對(duì)應(yīng)關(guān)系 Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();
protected final MIniMapperRegistry mapperRegistry = new MIniMapperRegistry(this);
// mybatis插件列表 List<Interceptor> interceptors = new ArrayList<>();
protected final InterceptorChain interceptorChain = new InterceptorChain();
// 類型注冊(cè)器,TypeAliasRegistry 構(gòu)造器會(huì)添加默認(rèn)值
// 用于在執(zhí)行sql語句的出入?yún)⒂成湟约癿ybatis-config文件里的各種配置
// 比如<transactionManager type="JDBC"/><dataSource type="POOLED">時(shí)使用簡(jiǎn)寫
// Map<String, Class<?>> typeAliases 存儲(chǔ)key別名,value 類型
protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
//Java和jdbc類型互相轉(zhuǎn)換
protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry();
protected ResultSetType defaultResultSetType;
//mapper.xml 中每條select|insert|update|delete 語句的映射
protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection")
.conflictMessageProducer((savedValue, targetValue) ->
". please check " + savedValue.getResource() + " and " + targetValue.getResource());
protected final Map<String, Cache> caches = new StrictMap<>("Caches collection");
//Mapper.xml 通過 <ResultMap> 配置的結(jié)果集映射關(guān)系信息
protected final Map<String, ResultMap> resultMaps = new StrictMap<>("Result Maps collection");
//Mapper.xml 通過 <ParameterMap> 配置的入?yún)⒂成潢P(guān)系信息
protected final Map<String, ParameterMap> parameterMaps = new StrictMap<>("Parameter Maps collection");
//KeyGenerator 是mybatis主鍵生成器 : mybatis提供了 3 中主鍵生成器 1:jdbc3KeyGenerator(數(shù)據(jù)庫(kù)自增主鍵) 2:SelectKeyGenerator (通過 select 語句查詢,如 oracle sequence) 3:noKeyGenerator(無自增主鍵)
protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<>("Key Generators collection");
// 注冊(cè)所有 Mapper.xml 的存放路徑
protected final Set<String> loadedResources = new HashSet<>();
protected final Map<String, XNode> sqlFragments = new StrictMap<>("XML fragments parsed from previous mappers");
/**
* 以下用于注冊(cè)出現(xiàn)解析異常的相關(guān)對(duì)象
*/
protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<>();
protected final Collection<CacheRefResolver> incompleteCacheRefs = new LinkedList<>();
protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<>();
protected final Collection<MethodResolver> incompleteMethods = new LinkedList<>();
構(gòu)造器注冊(cè)別名預(yù)覽
public MiniConfigration() {
// 內(nèi)置別名注冊(cè)
typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class);
typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class);
typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class);
typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class);
typeAliasRegistry.registerAlias("FIFO", FifoCache.class);
typeAliasRegistry.registerAlias("LRU", LruCache.class);
typeAliasRegistry.registerAlias("SOFT", SoftCache.class);
typeAliasRegistry.registerAlias("WEAK", WeakCache.class);
typeAliasRegistry.registerAlias("DB_VENDOR", VendorDatabaseIdProvider.class);
typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class);
typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class);
typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class);
typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class);
typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class);
typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class);
typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class);
typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class);
typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class);
typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class);
typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class);
languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class);
languageRegistry.register(RawLanguageDriver.class);
}
Configuration 作為其他組件的工廠類型,提供相關(guān)工廠方法
- Configuration 作為 Executor,StatementHander,ResultSetHander,ParameterHander 的工廠類,提供了創(chuàng)建這些實(shí)例對(duì)象的newXxx()工廠方法.
- mybatis 采用工廠模式創(chuàng)建對(duì)象的目的是實(shí)現(xiàn)插件攔截邏輯
這些工廠的方法簽名如下:
public MetaObject newMetaObject(Object object) {
return MetaObject.forObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
}
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
ResultHandler resultHandler, BoundSql boundSql) {
ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
return resultSetHandler;
}
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}
public Executor newExecutor(Transaction transaction) {
//return newExecutor(transaction, defaultExecutorType);
}