mybatis源码解析4---Configuration解析

Configuration类解析

Configuration类位于mybatis包的org.apache.ibatis.session目录下,是mybatis的全局变量,属性就是对应于mybatis的全局配置文件mybatis-config.xml的配置,将XML配置中的内容解析赋值到Configuration对象中。

由于XML配置项有很多,所以Configuration类的属性也很多。先来看下Configuration对于的XML配置文件示例:

 1 <?xml version="1.0" encoding="UTF-8"?>    
 2 
 3 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">    
 4 <!-- 全局配置顶级节点 -->
 5 <configuration>    
 6      
 7      <!-- 属性配置,读取properties中的配置文件 -->
 8     <properties resource="db.propertis">
 9        <property name="XXX" value="XXX"/>
10     </properties>
11     
12     <!-- 类型别名 -->
13     <typeAliases>
14        <!-- 在用到User类型的时候,可以直接使用别名,不需要输入User类的全部路径 -->
15        <typeAlias type="com.luck.codehelp.entity.User" alias="user"/>
16     </typeAliases>
17 
18     <!-- 类型处理器 -->
19     <typeHandlers>
20         <!-- 类型处理器的作用是完成JDBC类型和java类型的转换,mybatis默认已经由了很多类型处理器,正常无需自定义-->
21     </typeHandlers>
22     
23     <!-- 对象工厂 -->
24     <!-- mybatis创建结果对象的新实例时,会通过对象工厂来完成,mybatis有默认的对象工厂,正常无需配置 -->
25     <objectFactory type=""></objectFactory>
26     
27     <!-- 插件 -->
28     <plugins>
29         <!-- 可以自定义拦截器通过plugin标签加入 -->
30        <plugin interceptor="com.lucky.interceptor.MyPlugin"></plugin>
31     </plugins>
32     
33     <!-- 全局配置参数 -->
34     <settings>   
35         <setting name="cacheEnabled" value="false" />   
36         <setting name="useGeneratedKeys" value="true" /><!-- 是否自动生成主键 -->
37         <setting name="defaultExecutorType" value="REUSE" />   
38         <setting name="lazyLoadingEnabled" value="true"/><!-- 延迟加载标识 -->
39         <setting name="aggressiveLazyLoading" value="true"/><!--有延迟加载属性的对象是否延迟加载 -->
40         <setting name="multipleResultSetsEnabled" value="true"/><!-- 是否允许单个语句返回多个结果集 -->
41         <setting name="useColumnLabel" value="true"/><!-- 使用列标签而不是列名 -->
42         <setting name="autoMappingBehavior" value="PARTIAL"/><!-- 指定mybatis如何自动映射列到字段属性;NONE:自动映射;PARTIAL:只会映射结果没有嵌套的结果;FULL:可以映射任何复杂的结果 -->
43         <setting name="defaultExecutorType" value="SIMPLE"/><!-- 默认执行器类型 -->
44         <setting name="defaultFetchSize" value=""/>
45         <setting name="defaultStatementTimeout" value="5"/><!-- 驱动等待数据库相应的超时时间 ,单位是秒-->
46         <setting name="safeRowBoundsEnabled" value="false"/><!-- 是否允许使用嵌套语句RowBounds -->
47         <setting name="safeResultHandlerEnabled" value="true"/>
48         <setting name="mapUnderscoreToCamelCase" value="false"/><!-- 下划线列名是否自动映射到驼峰属性:如user_id映射到userId -->
49         <setting name="localCacheScope" value="SESSION"/><!-- 本地缓存(session是会话级别) -->
50         <setting name="jdbcTypeForNull" value="OTHER"/><!-- 数据为空值时,没有特定的JDBC类型的参数的JDBC类型 -->
51         <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/><!-- 指定触发延迟加载的对象的方法 -->
52         <setting name="callSettersOnNulls" value="false"/><!--如果setter方法或map的put方法,如果检索到的值为null时,数据是否有用  -->
53         <setting name="logPrefix" value="XXXX"/><!-- mybatis日志文件前缀字符串 -->
54         <setting name="logImpl" value="SLF4J"/><!-- mybatis日志的实现类 -->
55         <setting name="proxyFactory" value="CGLIB"/><!-- mybatis代理工具 -->
56     </settings>  
57 
58     <!-- 环境配置集合 -->
59     <environments default="development">    
60         <environment id="development">    
61             <transactionManager type="JDBC"/><!-- 事务管理器 -->
62             <dataSource type="POOLED"><!-- 数据库连接池 -->    
63                 <property name="driver" value="com.mysql.jdbc.Driver" />    
64                 <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />    
65                 <property name="username" value="root" />    
66                 <property name="password" value="*****" />    
67             </dataSource>    
68         </environment>    
69     </environments>    
70     
71     <!-- mapper文件映射配置 -->
72     <mappers>    
73         <mapper resource="com/luck/codehelp/mapper/UserMapper.xml"/>    
74     </mappers>    
75 </configuration>   

而对于XML的配置,Configuration类的属性是和XML配置对应的。Configuration类属性如下:

 1   protected Environment environment;//运行环境
 2 
 3   protected boolean safeRowBoundsEnabled = false;
 4   protected boolean safeResultHandlerEnabled = true;
 5   protected boolean mapUnderscoreToCamelCase = false;
 6   protected boolean aggressiveLazyLoading = true; //true:有延迟加载属性的对象被调用时完全加载任意属性;false:每个属性按需要加载
 7   protected boolean multipleResultSetsEnabled = true;//是否允许多种结果集从一个单独的语句中返回
 8   protected boolean useGeneratedKeys = false;//是否支持自动生成主键
 9   protected boolean useColumnLabel = true;//是否使用列标签
10   protected boolean cacheEnabled = true;//是否使用缓存标识
11   protected boolean callSettersOnNulls = false;//
12   protected boolean useActualParamName = true;
13 
14   protected String logPrefix;
15   protected Class <? extends Log> logImpl;
16   protected Class <? extends VFS> vfsImpl;
17   protected LocalCacheScope localCacheScope = LocalCacheScope.SESSION;
18   protected JdbcType jdbcTypeForNull = JdbcType.OTHER;
19   protected Set<String> lazyLoadTriggerMethods = new HashSet<String>(Arrays.asList(new String[] { "equals", "clone", "hashCode", "toString" }));
20   protected Integer defaultStatementTimeout;
21   protected Integer defaultFetchSize;
22   protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
23   protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;//指定mybatis如果自动映射列到字段和属性,PARTIAL会自动映射简单的没有嵌套的结果,FULL会自动映射任意复杂的结果
24   protected AutoMappingUnknownColumnBehavior autoMappingUnknownColumnBehavior = AutoMappingUnknownColumnBehavior.NONE;
25 
26   protected Properties variables = new Properties();
27   protected ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
28   protected ObjectFactory objectFactory = new DefaultObjectFactory();
29   protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
30 
31   protected boolean lazyLoadingEnabled = false;//是否延时加载,false则表示所有关联对象即使加载,true表示延时加载
32   protected ProxyFactory proxyFactory = new JavassistProxyFactory(); // #224 Using internal Javassist instead of OGNL
33 
34   protected String databaseId;
35 
36   protected Class<?> configurationFactory;
37 
38   protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
39   protected final InterceptorChain interceptorChain = new InterceptorChain();
40   protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
41   protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
42   protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry();
43 
44   protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");
45   protected final Map<String, Cache> caches = new StrictMap<Cache>("Caches collection");
46   protected final Map<String, ResultMap> resultMaps = new StrictMap<ResultMap>("Result Maps collection");
47   protected final Map<String, ParameterMap> parameterMaps = new StrictMap<ParameterMap>("Parameter Maps collection");
48   protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<KeyGenerator>("Key Generators collection");
49 
50   protected final Set<String> loadedResources = new HashSet<String>(); //已经加载过的resource(mapper)
51   protected final Map<String, XNode> sqlFragments = new StrictMap<XNode>("XML fragments parsed from previous mappers");
52 
53   protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<XMLStatementBuilder>();
54   protected final Collection<CacheRefResolver> incompleteCacheRefs = new LinkedList<CacheRefResolver>();
55   protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<ResultMapResolver>();
56   protected final Collection<MethodResolver> incompleteMethods = new LinkedList<MethodResolver>();
57 
58   protected final Map<String, String> cacheRefMap = new HashMap<String, String>();

而Configuration中的方法也差不多都是关于这些属性的set和get方法。由于属性太多,就不一一解析,现在只知道Configuration类是和mybatis的XML配置是对应的。加载的过程是SqlSessionFactoryBuilder根据xml配置的文件流,通过XMLConfigBuilder的parse方法进行解析得到一个Configuration对象。那么Configuration什么时候会用到呢?

原文地址:https://www.cnblogs.com/jackion5/p/9480456.html