properties解析
接着看一下propertiesElement(root.evalNode("properties"))方法,這句讀取的是<configuration>下的<properties>節點,代碼實作為:
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
Properties defaults = context.getChildrenAsProperties();
String resource = context.getStringAttribute("resource");
String url = context.getStringAttribute("url");
if (resource != null && url != null) {
throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
}
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {
defaults.putAll(Resources.getUrlAsProperties(url));
}
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}
我們可以先來看下三種配置的方式
第一種XML直接配置參數
<properties>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/>
</properties>
第二種xml配置外部properties檔案
<properties resource="properties/db.properties" />
第三種通過API進行java代碼進行配置
static {
try {
ssf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("config.xml"),buildInitProperties());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}
private static Properties buildInitProperties(){
Properties properties = new Properties();
properties.put("driver", "com.mysql.jdbc.Driver");
properties.put("url", "jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8");
properties.put("username", "root");
properties.put("password", "root");
return properties;
}
我們回到第一段代碼 。 看到第4行~第7行的代碼指定了MyBatis的<properties>标簽下不能同時指定"resource"屬性和"url"屬性。
接着第9行~第13行的代碼将.properties資源解析為Properties類,最後将Properties類設定到XPathParser和Configuration的variables屬性中,variables是一個Propreties變量。
總結配置優先級
第三種(API配置)> 第二種引用外部properties檔案 > 第一種xml直接配置name,value;
類型别名解析(typeAliases)
跳過loadCustomVfs(settings)直接看typeAliasesElement(root.evalNode("typeAliases"))這行,前者後面詳細講解,後者是用于定義類型的别名的,儲存了所有預設的類型與自定義類型,存儲變量引用不可改變。解析的是<configuration>下的<typeAliases>标簽,用過MyBatis的應該很熟悉。看一下源碼實作:
private void typeAliasesElement(XNode parent) {
if (parent != null) {
for (XNode child : parent.getChildren()) {
if ("package".equals(child.getName())) {
String typeAliasPackage = child.getStringAttribute("name");
configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
} else {
String alias = child.getStringAttribute("alias");
String type = child.getStringAttribute("type");
try {
Class<?> clazz = Resources.classForName(type);
if (alias == null) {
typeAliasRegistry.registerAlias(clazz);
} else {
typeAliasRegistry.registerAlias(alias, clazz);
}
} catch (ClassNotFoundException e) {
throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
}
}
}
}
}
從源碼實作中我們可以知道兩點,<typeAliases>标簽下可以定義<package>和<typeAlias>兩種标簽,但是看第4行和第7行的判斷,這是一段if...else...,是以可以知道<package>标簽和<typeAlias>标簽隻能定義其中的一種。首先看一下解析<package>标簽的代碼,第6行的registerAliases方法:
public void registerAliases(String packageName, Class<?> superType){
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses();
for(Class<?> type : typeSet){
// Ignore inner classes and interfaces (including package-info.java)
// Skip also inner classes. See issue #6
if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) {
registerAlias(type);
}
}
}
第3行根據路徑packageName尋找它下面的".class"檔案拿到所有的".class"檔案對應的類的Class,然後周遊所有的Class,做了三層判斷
- 必須不是匿名類
- 必須不是接口
- 必須不是成員類
此時此Class對應的類符合條件,會進行注冊,通過registerAlias方法進行注冊,看一下方法實作:
public void registerAlias(Class<?> type) {
String alias = type.getSimpleName();
Alias aliasAnnotation = type.getAnnotation(Alias.class);
if (aliasAnnotation != null) {
alias = aliasAnnotation.value();
}
registerAlias(alias, type);
}
第2行擷取Class的simpleName,simpleName指的是移除了包名的名稱,比如aa.bb.cc.Mail,getSimpleName()擷取的就是Mail。
第3行擷取類上面的注解Alias,如果Alias注解中有定義value屬性且指定了值,那麼第4行~第6行的判斷優先取這個值作為Class的别名。
第7行注冊别名:
public void registerAlias(String alias, Class<?> value) {
if (alias == null) {
throw new TypeException("The parameter alias cannot be null");
}
// issue #748
String key = alias.toLowerCase(Locale.ENGLISH);
if (TYPE_ALIASES.containsKey(key) && TYPE_ALIASES.get(key) != null && !TYPE_ALIASES.get(key).equals(value)) {
throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + TYPE_ALIASES.get(key).getName() + "'.");
}
TYPE_ALIASES.put(key, value);
}
其實就做了兩步操作:
- 将alias全部小寫
- 将alias以及Class對象放到TYPE_ALIASES中,TYPE_ALIASES是一個HashMap
這樣一個流程,就将<package>标簽name屬性路徑下的Class(如果符合要求),全部放到了HashMap中以供使用。
接着看一下<typeAlias>标簽的解析,也就是前面說的else部分:
String alias = child.getStringAttribute("alias");
String type = child.getStringAttribute("type");
try {
Class<?> clazz = Resources.classForName(type);
if (alias == null) {
typeAliasRegistry.registerAlias(clazz);
} else {
typeAliasRegistry.registerAlias(alias, clazz);
}
} catch (ClassNotFoundException e) {
throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
}
這裡先解析<typeAlias>中的alias屬性,再解析<typeAlias>中的type屬性,當然alias也可以不定義,不定義走的就是第6行的registerAlias方法,定義走的就是第8行的registerAlias方法,這兩個重載的registerAlias方法前面也都說過了,就不說了。
總結!!!
這裡可以配置多個package和多個typeAlias一起,也可以單個配置。如果有注解(@Alias)定義都取注解名稱,否則取類名小寫作為Key.自定義的Key也是取類小寫,否則異常(後面再看)。
如果配置目前有同樣的Key(alias)但是存儲的類型不一樣則會抛出異常
初始化類的時候預設配置了一些基礎的類型轉換
public TypeAliasRegistry() {
registerAlias("string", String.class);
registerAlias("byte", Byte.class);
registerAlias("long", Long.class);
registerAlias("short", Short.class);
registerAlias("int", Integer.class);
registerAlias("integer", Integer.class);
registerAlias("double", Double.class);
registerAlias("float", Float.class);
registerAlias("boolean", Boolean.class);
registerAlias("byte[]", Byte[].class);
registerAlias("long[]", Long[].class);
registerAlias("short[]", Short[].class);
registerAlias("int[]", Integer[].class);
registerAlias("integer[]", Integer[].class);
registerAlias("double[]", Double[].class);
registerAlias("float[]", Float[].class);
registerAlias("boolean[]", Boolean[].class);
registerAlias("_byte", byte.class);
registerAlias("_long", long.class);
registerAlias("_short", short.class);
registerAlias("_int", int.class);
registerAlias("_integer", int.class);
registerAlias("_double", double.class);
registerAlias("_float", float.class);
registerAlias("_boolean", boolean.class);
registerAlias("_byte[]", byte[].class);
registerAlias("_long[]", long[].class);
registerAlias("_short[]", short[].class);
registerAlias("_int[]", int[].class);
registerAlias("_integer[]", int[].class);
registerAlias("_double[]", double[].class);
registerAlias("_float[]", float[].class);
registerAlias("_boolean[]", boolean[].class);
registerAlias("date", Date.class);
registerAlias("decimal", BigDecimal.class);
registerAlias("bigdecimal", BigDecimal.class);
registerAlias("biginteger", BigInteger.class);
registerAlias("object", Object.class);
registerAlias("date[]", Date[].class);
registerAlias("decimal[]", BigDecimal[].class);
registerAlias("bigdecimal[]", BigDecimal[].class);
registerAlias("biginteger[]", BigInteger[].class);
registerAlias("object[]", Object[].class);
registerAlias("map", Map.class);
registerAlias("hashmap", HashMap.class);
registerAlias("list", List.class);
registerAlias("arraylist", ArrayList.class);
registerAlias("collection", Collection.class);
registerAlias("iterator", Iterator.class);
registerAlias("ResultSet", ResultSet.class);
}
建立Configuration的時候也預設初始化了一些參數
public Configuration() {
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);
}
對于這些資料,我們可以直接使用registerAlias方法的第一個參數對應的字元串而不需要定義這些typeAlias。
<typeAliases>
<typeAlias type="model.Mail"/> <!-- 指定的類 自動轉換類限定名 也可在類上注解配置@Alias("Mail")-->
<!-- <typeAlias alias="Mail" type="model.Mail"/> --><!-- 指定的類 自動轉換類限定名 配置别名-->
<!-- <package name="model"/> --><!-- 包掃描 -->
</typeAliases>
配置方式!
<select id="selectMailList" resultMap="mail">
select <include refid="fields" /> from mail;
</select>
可以直接在XML映射中直接使用resultType和parameterType的屬性值為定義的javaBean的完全限定名。