配置详解:propertiess和environment
properties的使用用法:
envirements元素节点的使用方法:
在上一篇文章中说过,mybatis是通过XMLConfigBuilder来解析mybatis配置文件的,所以这篇主要讲对properties节点和environment节点的解析。
XMLConfigBuilder解析mybatis配置文件中能配置的元素节点:
private void parseConfiguration(XNode root) {
try {
//解析properties元素
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"));
//解析environments元素
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);
}
}
1、解析properties属性的方法:
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
//将子节点的 name 以及value属性set进properties对象
Properties defaults = context.getChildrenAsProperties();
//获取properties节点上 resource属性的值
String resource = context.getStringAttribute("resource");
//获取properties节点上 url属性的值
String url = context.getStringAttribute("url");
//判断当两个属性都不为空时,抛出异常,因为从外部指定配置属性时,resource和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.");
}
//把解析出的properties文件set进Properties对象
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {
defaults.putAll(Resources.getUrlAsProperties(url));
}
//将configuration对象中已配置的Properties属性与刚刚解析的融合
//configuration对象用有一系列的get/set方法, 也可以使用java代码直接配置
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
//把装有解析配置propertis对象set进解析器, 因为后面可能会用到
parser.setVariables(defaults);
//set进configuration对象
configuration.setVariables(defaults);
}
}
2、解析environment属性的方法:
private void environmentsElement(XNode context) throws Exception {
if (context != null) {
if (environment == null) {
//解析environments节点的default属性的值
//<environment id="development">, 只有enviroment节点有id属性
//environments 节点下可以拥有多个 environment子节点
//意思就是可以对应多个环境,比如开发环境,测试环境等, 由environments的default属性去选择对应的enviroment(根据environment指定的id)
environment = context.getStringAttribute("default");
}
for (XNode child : context.getChildren()) {
String id = child.getStringAttribute("id");
//isSpecial就是根据由environments的default属性去选择对应的enviroment
if (isSpecifiedEnvironment(id)) {
//事务, mybatis有两种:JDBC 和 MANAGED, 配置为JDBC则直接使用JDBC的事务,配置为MANAGED则是将事务托管给容器, 在本文例子中使用的是JDBC的事务。
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
//enviroment节点下面就是dataSource节点了,解析dataSource节点
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
DataSource dataSource = dsFactory.getDataSource();
Environment.Builder environmentBuilder = new Environment.Builder(id)
.transactionFactory(txFactory)
.dataSource(dataSource);
//将dataSource设置进configuration对象(和之前解析properties一样)
configuration.setEnvironment(environmentBuilder.build());
}
}
}
}
2.1)dataSource的解析方法:
private DataSourceFactory dataSourceElement(XNode context) throws Exception {
if (context != null) {
//dataSource的连接池
//type=”POOLED” :MyBatis会创建PooledDataSource实例(使用连接池的数据源)
//type=”UNPOOLED” :MyBatis会创建UnpooledDataSource实例(不使用连接池的数据源)
//type=”JNDI” :MyBatis会从JNDI服务上查找DataSource实例,然后返回使用
String type = context.getStringAttribute("type");
//子节点 name, value属性set进一个properties对象
Properties props = context.getChildrenAsProperties();
//创建dataSourceFactory
DataSourceFactory factory = (DataSourceFactory) resolveClass(type).newInstance();
factory.setProperties(props);
return factory;
}
throw new BuilderException("Environment declaration requires a DataSourceFactory.");
}