根据mybatis框架加载流程
1 private void parseConfiguration(XNode root) {
2 try {
3 //解析子节点的properties文件
4 propertiesElement(root.evalNode("properties"));
5 //加载用户自定义配置
6 Properties settings = settingsAsProperties(root.evalNode("settings"));
7 //加载vfs虚拟文件系统配置
8 loadCustomVfs(settings);
9 // 解析子节点typeAliases 别名
10 typeAliasesElement(root.evalNode("typeAliases"));
11 //解析子节点plugins 插件
12 pluginElement(root.evalNode("plugins"));
13 //解析子节点objectFactory mybatis为结果创建对象时都会用到objectFactory
14 objectFactoryElement(root.evalNode("objectFactory"));
15 //解析子节点的封装对象
16 objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
17 reflectorFactoryElement(root.evalNode("reflectorFactory"));
18 //解析settings标签的文件配置
19 settingsElement(settings);
20 //配置运行环境
21 environmentsElement(root.evalNode("environments"));
22 //解析子节点配置数据库供应商
23 databaseIdProviderElement(root.evalNode("databaseIdProvider"));
24 //解析对象类型处理器,处理Java的pojo类型与数据库类型的映射
25 typeHandlerElement(root.evalNode("typeHandlers"));
26 //解析子节点的mappers
27 mapperElement(root.evalNode("mappers"));
28 } catch (Exception e) {
29 throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
30 }
31 }
<typeAliases>标签配置为,在mapper中可以使用别名
1 <typeAliases> 2 <!-- 单个别名的定义 alias:别名,type:别名映射的类型 --> 3 <typeAlias type="com.ssc.demo.mybatis.pojo.Subject" alias="testAlias"></typeAlias> 4 <!-- 批量别名定义 指定包路径,自动扫描包下边的pojo,定义别名,别名默认为类名(首字母小写或大写) --> 5 <package name="com.ssc.demo.mybatis.pojo"/> 6 </typeAliases>
typeAliasesElement方法为
1 private void typeAliasesElement(XNode parent) { 2 if (parent != null) { 3 for (XNode child : parent.getChildren()) { 4 //如果子节点为 package 属性 5 if ("package".equals(child.getName())) { 6 //获取包名 7 String typeAliasPackage = child.getStringAttribute("name"); 8 //将包名下的类自动扫描pojo,定义别名 9 configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage); 10 } else { 11 //如果子节点为 typeAlias 12 String alias = child.getStringAttribute("alias"); 13 String type = child.getStringAttribute("type"); 14 try { 15 //创建类实例 16 Class<?> clazz = Resources.classForName(type); 17 if (alias == null) { 18 //如果别名为空,则默认为类名为别名 19 typeAliasRegistry.registerAlias(clazz); 20 } else { 21 typeAliasRegistry.registerAlias(alias, clazz); 22 } 23 } catch (ClassNotFoundException e) { 24 throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e); 25 } 26 } 27 } 28 } 29 }
在mybatis源码的Configuration类中,对系统常用的配置配置了别名:
1 //事务方式别名 2 typeAliasRegistry.registerAlias("JDBC",JdbcTransactionFactory.class); 3 typeAliasRegistry.registerAlias("MANAGED",ManagedTransactionFactory.class); 4 //数据源类型别名 5 typeAliasRegistry.registerAlias("JNDI",JndiDataSourceFactory.class); 6 typeAliasRegistry.registerAlias("POOLED", 7 PooledDataSourceFactory.class); 8 typeAliasRegistry.registerAlias("UNPOOLED",UnpooledDataSourceFactory.class); 9 //缓存策略别名 10 typeAliasRegistry.registerAlias("PERPETUAL",PerpetualCache.class); 11 typeAliasRegistry.registerAlias("FIFO",FifoCache.class); 12 typeAliasRegistry.registerAlias("LRU",LruCache.class); typeAliasRegistry.registerAlias("SOFT", SoftCache.class); typeAliasRegistry.registerAlias("WEAK", WeakCache.class); 13 //数据库标识别名 14 typeAliasRegistry.registerAlias("DB_VENDOR", 15 VendorDatabaseIdProvider.class); 16 //语言驱动类别名 17 typeAliasRegistry.registerAlias("XML",XMLLanguageDriver.class); 18 typeAliasRegistry.registerAlias("RAW",RawLanguageDriver.class); 19 //日志类别名 20 typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class); 21 typeAliasRegistry.registerAlias("COMMONS_LOGGTNG",JakartmCommonsLogginglmpl.class); 22 typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class); 23 typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class); 24 typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class); 25 typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class); 26 typeAliasRegistry.registerAlias("NO_LOGGING",NoLoggingImpl.class); 27 //动态代理别名 28 typeAliasRegistry.registerAlias("CGLIB",CglibProxyFactory.class); 29 typeAliasRegistry.registerAlias("JAVASSIST",JavassistProxyFactory.class);