ToB企服应用市场:ToB评测及商务社交产业平台

标题: MyBatis 核心组件 —— Configuration [打印本页]

作者: 海哥    时间: 2023-8-1 16:19
标题: MyBatis 核心组件 —— Configuration
概述

Mybatis 的核心组件如下所示:
在使用 MyBatis 时,我们使用到 SqlSession 组件,它是用户层面的 API。实际上 SqlSession 是Executor 组件的外观,目的是为用户提供更友好的数据库操作接口,这是设计模式中外观模式的典型应用。真正执行 SQL 操作的是 Executor 组件,Executor 可以理解为 SQL 执行器,它会使用 StatementHandler 组件对 JDBC 的 Statement 对象进行操作。当 Statement 类型为 CallableStatement 和 PreparedStatement 时,会通过 ParameterHandler 组件为参数占位符赋值。ParameterHandler 组件中会根据 Java 类型找到对应的 TypeHandler 对象,TypeHandler 会通过 Statement 对象提供的set 方法为 Statement 对象中的参数占位符设置值。StatementHandler 组件使用 JDBC 中的 Statement 对象与数据库完成交互后,当 SQL 语句类型为 SELECT 时,MyBatis 通过 ResultSetHandler 组件从 Statement 对象中获取 ResultSet 对象,然后将 ResultSet 对象转换为 Java 对象

Configuration

MyBatis 框架的配置信息有两种,一种是配置 MyBatis 框架属性的主配置文件;另一种是配置执行 SQL 语句的 Mapper 配置文件。Configuration 的作用是描述 MyBatis 主配置文件的信息。Configuration 类中定义了一系列的属性用来控制MyBatis 运行时的行为,这些属性代码如下:
  1. public class Configuration {
  2.         protected boolean safeRowBoundsEnabled;
  3.         protected boolean safeResultHandlerEnabled = true;
  4.         protected boolean mapUnderscoreToCamelCase;
  5.         protected boolean aggressivelazyLoading;
  6.         protected boolean multipleResultSetsEnabled = true;
  7.         protected boolean useGeneratedKeys;
  8.         protected boolean useColumnLabel = true;
  9.         .......
  10. }
复制代码
这些属性的值可以在MyBatis主配置文件中通过  标签指定,例如:
  1. <settings>
  2.         <setting name="cacheEnabled" value="true"/>
  3.         <setting name="lazyLoadingEnabled" value="true"/>
  4. </settings>
复制代码
所有属性的作用及配置说明参考如下:
Configuration 除了提供上面的属性控制 MyBatis 的行为外,还作为容器存放 TypeHandler(类型处理器)、TypeAlias(类型别名)、Mapper 接口及 MapperSQL 配置信息。这些信息在 MyBatis 框架启动时注册到 Configuration 组件中。Configuration 类通过下面的属性保存 TypeHandler、TypeAlias 等信息:
  1. protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
  2. protected final InterceptorChain interceptorChain = new InterceptorChain();
  3. protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
  4. protected final TypeAliasRegistry typeAliasRegistry= new TypeAliasRegistry();
  5. protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry();
  6. protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");
  7. protected final Map<String, Cache> caches = new StrictMap<Cache>("Caches collection");
  8. protected final Map<String, ResultMap> resultMaps = new StrictMap<ResultMap>("Result Maps collection");
  9. protected final Map<String, ParameterMap> parameterMaps = new StrictMap<ParameterMap>("Parameter Maps collection");
  10. protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<KeyGenerator>("Key Generators collection");
  11. protected final Set<String> loadedResources = new HashSet<String>();
  12. protected final Map<String, XNode> sqlFragments = new StrictMap<XNode>("XML fragments parsed from previous mappers");
  13. protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<XMLStatementBuilder>();
  14. protected final Collection<CacheRefResolver> incompletecacheRefs = new LinkedList<CacheRefResolver>()
  15. protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<ResultMapResolver>();
  16. protected final Collection<MethodResolver> incompleteMethods = new LinkedList<MethodResolver>();
  17. protected final Map<String, String> cacheRefMap = new HashMap<String, String>();
复制代码
这些属性的含义如下:
MyBatis 框架启动时,会对所有的配置信息进行解析,然后将解析后的内容注册到 Configuration 对象的这些属性中。除此之外,Configuration 组件还作为 Executor、StatementHandler、ResultSetHandler、ParameterHandler 组件的工厂类,用于创建这些组件的实例。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4