出名的框架几乎都是从初始化配置文件开始的,mybatis也一样。
mybatis的配置文件是通过工具类XMLConfigBuilder,XMLMapperBuilder,XMLStatementBuilder解析后都存在Configuration中
private void parseConfiguration(XNode root) { try { propertiesElement(root.evalNode("properties")); //issue #117 read properties first typeAliasesElement(root.evalNode("typeAliases")); pluginElement(root.evalNode("plugins")); objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); settingsElement(root.evalNode("settings")); environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631 databaseIdProviderElement(root.evalNode("databaseIdProvider")); typeHandlerElement(root.evalNode("typeHandlers")); mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }
public class Configuration { protected Environment environment; protected boolean safeRowBoundsEnabled = false; protected boolean safeResultHandlerEnabled = true; protected boolean mapUnderscoreToCamelCase = false; protected boolean aggressiveLazyLoading = true; protected boolean multipleResultSetsEnabled = true; protected boolean useGeneratedKeys = false; protected boolean useColumnLabel = true; //二级缓存开关,直接决定sqlSession是否启用CachingExecutor protected boolean cacheEnabled = true; protected boolean callSettersOnNulls = false; protected String logPrefix; protected Class <? extends Log> logImpl; //一级缓存的默认作用域 protected LocalCacheScope localCacheScope = LocalCacheScope.SESSION; protected JdbcType jdbcTypeForNull = JdbcType.OTHER; protected Set<String> lazyLoadTriggerMethods = new HashSet<String>(Arrays.asList(new String[] { "equals", "clone", "hashCode", "toString" })); protected Integer defaultStatementTimeout; protected Integer defaultFetchSize; protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE; protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL; //config配置文件Properties标签的数据存储 -----全局属性配置对象 protected Properties variables = new Properties(); protected ReflectorFactory reflectorFactory = new DefaultReflectorFactory(); //config配置文件objectFactory标签的数据存储 protected ObjectFactory objectFactory = new DefaultObjectFactory(); protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory(); //maper接口的存储容器, mybatis访问数据库的动态代理接口,MapperProxyFactory 是经典的C/S架构客户端同步访问实现 protected MapperRegistry mapperRegistry = new MapperRegistry(this); protected boolean lazyLoadingEnabled = false; protected ProxyFactory proxyFactory = new JavassistProxyFactory(); // #224 Using internal Javassist instead of OGNL //数据源 id protected String databaseId; /** * Configuration factory class. * Used to create Configuration for loading deserialized unread properties. * * @see <a href='https://code.google.com/p/mybatis/issues/detail?id=300'>Issue 300</a> (google code) */ protected Class<?> configurationFactory; //插件,可以在核心业务逻辑处理类“statementHandler”中增加sql解析,sql路由等等功能 protected final InterceptorChain interceptorChain = new InterceptorChain(); //config配置文件typeHandler标签的数据容器 protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry(); //config配置文件typeAlias标签的数据容器 ---别名对应容器 protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry(); //language容器 protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry(); //mapper配置文件的数据容器 protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection"); //缓存容器 protected final Map<String, Cache> caches = new StrictMap<Cache>("Caches collection"); //resultMap容器,思考:抛弃ORM设计思想, 还可以通过JDBC的ResultSet相关方法组装成map对象返回,不需要resultSetHandler.<E>handleResultSets(statement)。 protected final Map<String, ResultMap> resultMaps = new StrictMap<ResultMap>("Result Maps collection"); //输入参数类型容器 protected final Map<String, ParameterMap> parameterMaps = new StrictMap<ParameterMap>("Parameter Maps collection"); //主键生成器容器 protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<KeyGenerator>("Key Generators collection"); protected final Set<String> loadedResources = new HashSet<String>(); protected final Map<String, XNode> sqlFragments = new StrictMap<XNode>("XML fragments parsed from previous mappers"); //异常数据容器 protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<XMLStatementBuilder>(); //异常数据容器 protected final Collection<CacheRefResolver> incompleteCacheRefs = new LinkedList<CacheRefResolver>(); //异常数据容器 protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<ResultMapResolver>(); //异常数据容器 protected final Collection<MethodResolver> incompleteMethods = new LinkedList<MethodResolver>(); /* * A map holds cache-ref relationship. The key is the namespace that * references a cache bound to another namespace and the value is the * namespace which the actual cache is bound to. */ protected final Map<String, String> cacheRefMap = new HashMap<String, String>();