在mybatis 中使用databaseIdProvider节点配置项(<databaseIdProvider type="DB_VENDOR">)时, 出现如下错误提示:
Exception in thread "main" :
### Error building SqlSession.
### The error may exist in SQL Mapper Configuration
### Cause: : Error parsing SQL Mapper Configuration. Cause: : Error resolving class. Cause: : Could not resolve type alias 'DB_VENDOR'. Cause: : Cannot find class: DB_VENDOR
at (:23)
at (:79)
at (:63)
at (:20)
Caused by: : Error parsing SQL Mapper Configuration. Cause: : Error resolving class. Cause: : Could not resolve type alias 'DB_VENDOR'. Cause: : Cannot find class: DB_VENDOR
at (:106)
at (:89)
at (:77)
... 2 more
Caused by: : Error resolving class. Cause: : Could not resolve type alias 'DB_VENDOR'. Cause: : Cannot find class: DB_VENDOR
at (:100)
at (:244)
at (:102)
... 4 more
Caused by: : Could not resolve type alias 'DB_VENDOR'. Cause: : Cannot find class: DB_VENDOR
at (:114)
at (:127)
at (:98)
... 6 more
Caused by: : Cannot find class: DB_VENDOR
at (:188)
at (:87)
at (:254)
at (:110)
... 8 more
原因是DB_VENDOR是从3.2.3版本开始使用的别名,3.2.3(不含)以前的板本不支持此名称,需要使用VENDOR。
从mybatis源代码中可以看出:
V3.2.3
package ;
public class Configuration {
public Configuration() {
("JDBC", );
("MANAGED", );
("JNDI", );
("POOLED", );
("UNPOOLED", );
("PERPETUAL", );
("FIFO", );
("LRU", );
("SOFT", );
("WEAK", );
("DB_VENDOR", );
("XML", );
("RAW", );
("SLF4J", );
("COMMONS_LOGGING", );
("LOG4J", );
("LOG4J2", );
("JDK_LOGGING", );
("STDOUT_LOGGING", );
("NO_LOGGING", );
("CGLIB", );
("JAVASSIST", );
();
();
}
}
而在3.2.3的xml文件初始化时,使用了VENDOR转换,可以同时支持老板本的VENDOR:
package ;
public class XMLConfigBuilder extends BaseBuilder {
private void databaseIdProviderElement(XNode context) throws Exception {
DatabaseIdProvider databaseIdProvider = null;
if (context != null) {
String type = ("type");
if ("VENDOR".equals(type)) type = "DB_VENDOR";// awful patch to keep backward compatibility
Properties properties = ();
databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance();
(properties);
}
}
}
而在3.2.3以前的代码中,则不支持DB_VENDOR:
V3.2.2
package ;
public class Configuration {
public Configuration() {
("JDBC", );
("MANAGED", );
("JNDI", );
("POOLED", );
("UNPOOLED", );
("PERPETUAL", );
("FIFO", );
("LRU", );
("SOFT", );
("WEAK",
("VENDOR", );
("XML", );
("RAW", );
("SLF4J", );
("COMMONS_LOGGING", );
("LOG4J", );
("JDK_LOGGING", );
("STDOUT_LOGGING", );
("NO_LOGGING", );
("CGLIB", );
("JAVASSIST", );
();
();
}
}
package ;
public class XMLConfigBuilder extends BaseBuilder {
private void databaseIdProviderElement(XNode context) throws Exception {
DatabaseIdProvider databaseIdProvider = null;
if (context != null) {
String type = ("type");
Properties properties = ();
databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance();
(properties);
}
Environment environment = ();
if (environment != null && databaseIdProvider != null) {
String databaseId = (());
(databaseId);
}
}
}
因此如果在使用旧的包,而又对应使用新的文档开发时,很可能引发此问题