mybatis源码配置文件解析
mybatis源码配置文件解析
一、解析properties标签
mybatis作为日常开发的常用ORM框架,在开发中起着很重要的作用,了解其源码对日常的开发有很大的帮助。源码版本为:3-3.4.x,可自行到github进行下载。
从这篇文章开始逐一分析mybatis的核心配置文件(mybatis-config.xml),今天先来看properties标签的解析过程。
概述
在单独使用mybatis的时候,mybatis的核心配置文件(mybatis-config.xml)就显的特别重要,是整个mybatis运行的基础,只有把配置文件中的各个标签正确解析后才可以正确使用mybatis,下面看properties标签的配置,properties标签的作用就是加载properties文件或者property标签,下面看其具体配置,实例如下
<properties resource="org/mybatis/example/config.properties">
<property name="username" value="tuser"/>
<property name="password" value="ABCD1234"/>
</properties>
上面是配置的properties标签的配置,在标签中配置了resource属性和property子标签。下面看具体的解析流程,这里分析properties标签的解析过程,启动流程暂不说,直接看解析的代码。
详述
上面,看到了properties标签的配置,下面看其解析方法,这里只粘贴部分代码,下面是parseConfiguration方法的代码,
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
//解析properties标签
propertiesElement(root.evalNode("properties"));
//解析settings标签
Properties settings = settingsAsProperties(root.evalNode("settings"));
loadCustomVfs(settings);
//解析别名标签,例<typeAlias alias="user" type="cn.com.bean.User"/>
typeAliasesElement(root.evalNode("typeAliases"));
//解析插件标签
pluginElement(root.evalNode("plugins"));
//解析objectFactory标签,此标签的作用是mybatis每次创建结果对象的新实例时都会使用ObjectFactory,如果不设置
//则默认使用DefaultObjectFactory来创建,设置之后使用设置的
objectFactoryElement(root.evalNode("objectFactory"));
//解析objectWrapperFactory标签
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析reflectorFactory标签
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
//解析environments标签
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
//解析<mappers>标签
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
从上面的代码中可以找到下面的代码,即为解析的代码,
propertiesElement(root.evalNode("properties"));
这个方法就是解析properties标签,下面看具体的解析过程。
1、解析子标签和属性
/**
* 解析mybatis-config.xml文件中的properties标签
*<properties resource="org/mybatis/example/config.properties">
*<property name="username" value="dev_user"/>
*<property name="password" value="F2Fa3!33TYyg"/>
*</properties>
*解析步骤:
*1、解析配置的property标签,放到defaults中;
*2、解析resource或url属性,放到defaults中;
*3、获取configuration中的variables变量值,放到defaults中
* @param context
* @throws Exception
*/
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
//1、读取properties标签中的property标签<property name="" value=""/>
Properties defaults = context.getChildrenAsProperties();
//2、读取properties标签中的resource、url属性
String resource = context.getStringAttribute("resource");
String url = context.getStringAttribute("url");
//resource和url属性不能同时出现在properties标签中
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.");
}
//如果resource不为空,则解析为properties,放到defaults中,由于defaults是key-value结构,所以会覆盖相同key的值
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {//如果url不为空,则解析为properties,放到defaults中,由于defaults是key-value结构,所以会覆盖相同key的值
defaults.putAll(Resources.getUrlAsProperties(url));
}
//3、获得configuration中的variables变量的值,此变量可以通过SqlSessionFactoryBuilder.build()传入properties属性值
Properties vars = configuration.getVariables();
//如果调用build的时候传入了properties属性,放到defaults中
if (vars != null) {
defaults.putAll(vars);
}
//放到parser和configuration对象中
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}
从上面的解析过程可以看到,首先解析properties标签的子标签,也就是property标签,通过下面的方法获得,
//1、读取properties标签中的property标签<property name="" value=""/>
Properties defaults = context.getChildrenAsProperties();
解析property标签,并放到Properties对象中。那么是如何放到Properties对象中的那,在getChildrenAsProperties方法中,
public Properties getChildrenAsProperties() {
Properties properties = new Properties();
for (XNode child : getChildren()) {
String name = child.getStringAttribute("name");
String value = child.getStringAttribute("value");
if (name != null && value != null) {
properties.setProperty(name, value);
}
}
return properties;
}
可以看出是循环property标签,获得其name和value属性,并放入properties对象中。
接着解析properties的resource和url属性,如下
//2、读取properties标签中的resource、url属性
String resource = context.getStringAttribute("resource");
String url = context.getStringAttribute("url");
分别获得resource和url属性,这里这两个属性都是一个路径。
2、处理属性
下面看这个判断,
//resource和url属性不能同时出现在properties标签中
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标签中,resource和url属性不能同时出现。
2.1、处理resource和url属性
下面看resource和url属性的处理,这里resource和url两个属性都是代表的一个路径,所以这里肯定是需要读取相应路径下的文件。
//如果resource不为空,则解析为properties,放到defaults中,由于defaults是key-value结构,所以会覆盖相同key的值
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {//如果url不为空,则解析为properties,放到defaults中,由于defaults是key-value结构,所以会覆盖相同key的值
defaults.putAll(Resources.getUrlAsProperties(url));
}
下面看对resource的处理,调用的Resources.getResourceAsProperties(resource))方法,对resource进行处理,
public static Properties getResourceAsProperties(String resource) throws IOException {
Properties props = new Properties();
InputStream in = getResourceAsStream(resource);
props.load(in);
in.close();
return props;
}
从上面的代码可以看出是要转化为InputStream,最后放到Properties对象中,这里加载文件的详细过程,后面再详细分析。
下面看对url的处理,调用Resources.getUrlAsProperties(url)方法,对url进行处理,
public static Properties getUrlAsProperties(String urlString) throws IOException {
Properties props = new Properties();
InputStream in = getUrlAsStream(urlString);
props.load(in);
in.close();
return props;
}
上面的代码依然是把url代表的文件处理成Properties对象。
2.3、处理已添加的Properties
在上面处理完property子标签、resource和url属性后,还进行了下面的处理,即从configuration中获得properties,
//3、获得configuration中的variables变量的值,此变量可以通过SqlSessionFactoryBuilder.build()传入properties属性值
Properties vars = configuration.getVariables();
//如果调用build的时候传入了properties属性,放到defaults中
if (vars != null) {
defaults.putAll(vars);
}
如果configuration中已经存在properties信息,则取出来,放到defaults中。
2.4、放入configuration对象中
经过上面的处理,最后把所有的properties信息放到configuration中,
//放到parser和configuration对象中
parser.setVariables(defaults);
configuration.setVariables(defaults);
把defaults放到了configuration的variables属性中,代表的是整个mybatis环境中所有的properties信息。这个信息可以在mybatis的配置文件中使用${key}使用,比如,${username},则会从configuration的variables中寻找key为username的属性值,并完成自动属性值替换。
小结
上面分析了properties标签的解析过程,先解析property标签,然后是resource、url属性,最后是生成SqlSessionFactory的使用调用SqlSessionFactoryBuilder的build方法,传入的properties,从上面的解析过程,可以知道如果存在重复的键,那么最先解析的会被后面解析的覆盖掉,也就是解析过程是:property子标签—>resource—>url—>开发者设置的,那么覆盖过程为:开发者设置的—>url—>resource—>property子标签,优先级最高的为开发者自己设置的properties属性。
二、解析settings标签
概述
在mybatis的核心配置文件(mybatis-config.xml)文件中,有关于settings标签的配置,如下
<settings>
<!-- 设置日志输出为LOG4J -->
<setting name="logImpl" value="STDOUT_LOGGING" />
<!--将以下画线方式命名的数据库列映射到 Java 对象的驼峰式命名属性中-->
<setting name= "mapUnderscoreToCamelCase" value="true" />
</settings>
上面只简单的给出settings标签的配置,settings标签配置在
详述
上面,看到了settings标签的配置方式,下面看其解析过程,在XMLConfigBuilder类中的parseConfiguration方法中有关于该标签的解析,
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
//解析properties标签
propertiesElement(root.evalNode("properties"));
//解析settings标签,1、把<setting>标签解析为Properties对象
Properties settings = settingsAsProperties(root.evalNode("settings"));
/*2、对<settings>标签中的<setting>标签中的内容进行解析,这里解析的是<setting name="vfsImpl" value=",">
* VFS是mybatis中用来表示虚拟文件系统的一个抽象类,用来查找指定路径下的资源。上面的key为vfsImpl的value可以是VFS的具体实现,必须
* 是权限类名,多个使用逗号隔开,如果存在则设置到configuration中的vfsImpl属性中,如果存在多个,则设置到configuration中的仅是最后一个
* */
loadCustomVfs(settings);
//解析别名标签,例<typeAlias alias="user" type="cn.com.bean.User"/>
typeAliasesElement(root.evalNode("typeAliases"));
//解析插件标签
pluginElement(root.evalNode("plugins"));
//解析objectFactory标签,此标签的作用是mybatis每次创建结果对象的新实例时都会使用ObjectFactory,如果不设置
//则默认使用DefaultObjectFactory来创建,设置之后使用设置的
objectFactoryElement(root.evalNode("objectFactory"));
//解析objectWrapperFactory标签
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析reflectorFactory标签
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
//解析environments标签
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
//解析<mappers>标签
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
上面便是parseConfiguration方法,在此方法中下面的方法对settings进行了解析,
//解析settings标签,1、把<setting>标签解析为Properties对象
Properties settings = settingsAsProperties(root.evalNode("settings"));
调用settingsAsProperties方法,从方法名中可以看出要把settings标签中的内容解析到Proerties对象中,因为settings标签中是name-value的配置,刚好解析到Properties中以键值对的形式存储。下面是settingsAsProperties方法,
private Properties settingsAsProperties(XNode context) {
if (context == null) {
return new Properties();
}
//把<setting name="" value="">标签解析为Properties对象
Properties props = context.getChildrenAsProperties();
// Check that all settings are known to the configuration class
MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
//如果获取的配置的<setting name="" value="">信息,name不在metaConfig中,则会抛出异常
//这里metaConfig中的信息是从Configuration类中解析出来的,包含set方法的属性
//所以在配置<setting>标签的时候,其name值可以参考configuration类中的属性,配置为小写
for (Object key : props.keySet()) {
//从metaConfig的relector中的setMethods中判断是否存在该属性,setMethods中存储的是可写的属性,
//所以这里要到setMethods中进行判断
if (!metaConfig.hasSetter(String.valueOf(key))) {
throw new BuilderException("The setting " + key + " is not known. Make sure you spelled it correctly (case sensitive).");
}
}
return props;
}
1、解析子标签
解析子标签也就是settings标签中的setting标签,使用下面的方法进行解析,
//把<setting name="" value="">标签解析为Properties对象
Properties props = context.getChildrenAsProperties();
调用了getChildrenAsProperties方法,
public Properties getChildrenAsProperties() {
Properties properties = new Properties();
for (XNode child : getChildren()) {
String name = child.getStringAttribute("name");
String value = child.getStringAttribute("value");
if (name != null && value != null) {
properties.setProperty(name, value);
}
}
return properties;
}
该方法就是解析
我们再看上面的settingsAsProperties方法,调用上述getChildrenAsProperties方法获得Properties对象后又进行了其他操作。
2、校验setting标签中的name值是否存在
2.1、获得setting标签中的所有name值
在本文开篇提到一个问题,setting标签中的name值怎么配置,答案是可以参考mybatis的官方文档,在官方文档中有详细的解释,再有就是分析源码,继续往下看。
在settingsAsProperties方法中看下面一行代码,
// Check that all settings are known to the configuration class
MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
上面这行代码就解析了setting标签中的name可以配置的所有值。再看代码上的注释,是不是豁然开朗。该方法有两个参数,一个是Configuration.class,一个是localReflectorFactory,看localReflectorFactory,
private final ReflectorFactory localReflectorFactory = new DefaultReflectorFactory();
使用了DefaultReflectorFactory,看其默认构造方法
默认构造方法仅初始化了classCacheEnabled和relectorMap两个属性。后过来继续看MetaClass.forClass方法,
public static MetaClass forClass(Class<?> type, ReflectorFactory reflectorFactory) {
return new MetaClass(type, reflectorFactory);
}
该方法返回的是一个MetaClass的对象,
private MetaClass(Class<?> type, ReflectorFactory reflectorFactory) {
this.reflectorFactory = reflectorFactory;
this.reflector = reflectorFactory.findForClass(type);
}
重点看reflectorFactory.findForClass方法,这里reflectorFactory是DefaultReflectorFactory的一个实例。下面是DefaultReflectorFactory的findForClass方法,
@Override
public Reflector findForClass(Class<?> type) {
if (classCacheEnabled) {
// synchronized (type) removed see issue #461
Reflector cached = reflectorMap.get(type);
if (cached == null) {
cached = new Reflector(type);
reflectorMap.put(type, cached);
}
return cached;
} else {
return new Reflector(type);
}
}
上面方法中,重点看new Reflector(type)这句方法,
public Reflector(Class<?> clazz) {
type = clazz;
//解析默认的构造方法,及无参构造方法
addDefaultConstructor(clazz);
//解析clazz中的get方法,这里的clazz指的是Configuration.class
addGetMethods(clazz);
//解析clazz中的set方法,这里的clazz指的是Configuration.class
addSetMethods(clazz);
addFields(clazz);
readablePropertyNames = getMethods.keySet().toArray(new String[getMethods.keySet().size()]);
writeablePropertyNames = setMethods.keySet().toArray(new String[setMethods.keySet().size()]);
for (String propName : readablePropertyNames) {
caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
}
for (String propName : writeablePropertyNames) {
caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
}
}
此方法完成的功能是解析clazz(包含其父类)的构造方法、getXX方法、setXX方法、字段,通过一个类的Class对象获取。
addDefaultConstructor(clazz)如下,
private void addDefaultConstructor(Class<?> clazz) {
//获得该类的声明的构造方法
Constructor<?>[] consts = clazz.getDeclaredConstructors();
//对构造方法进行循环
for (Constructor<?> constructor : consts) {
//判断构造方法的参数是否为0,为0代表为默认的无参构造方法
if (constructor.getParameterTypes().length == 0) {
//如果是私有的(修饰符为private),这里需要设置可见。
if (canAccessPrivateMethods()) {
try {
constructor.setAccessible(true);
} catch (Exception e) {
// Ignored. This is only a final precaution, nothing we can do.
}
}
if (constructor.isAccessible()) {
this.defaultConstructor = constructor;
}
}
}
}
上面方法获得传入的Class对象所以构造方法,把默认的无参构造方法赋给defaultConstructor。
addGetMethods(clazz)如下,
private void addGetMethods(Class<?> cls) {
Map<String, List<Method>> conflictingGetters = new HashMap<String, List<Method>>();
//使用反射的放上获得cls的所有方法
Method[] methods = getClassMethods(cls);
//把所有的方法放入conflictingGetters中,key为属性名,value为List<Method>
for (Method method : methods) {
//方法的参数大于0,则结束本次循环,因为这里解析的是get方法,get方法默认不应该有参数
if (method.getParameterTypes().length > 0) {
continue;
}
String name = method.getName();
//如果以get或is开头,且方法名称分别大于3和2,则说明是get方法
if ((name.startsWith("get") && name.length() > 3)
|| (name.startsWith("is") && name.length() > 2)) {
//通过方法名转化为属性名,如,getUserName--userName
name = PropertyNamer.methodToProperty(name);
addMethodConflict(conflictingGetters, name, method);
}
}
/**处理一个属性多个get方法的情况,即conflictingGetter方法中一个key对应的value的长度大于1的情况,如下
*key propertyName
*value list<Method> 其长度大于1
*/
resolveGetterConflicts(conflictingGetters);
}
获取所有以get和is开头的方法,调用addMethodConflict方法,这里的方法名直译过来是添加冲突的方法,这里冲突怎么理解,我们看addMethodConflict方法,
private void addMethodConflict(Map<String, List<Method>> conflictingMethods, String name, Method method) {
//根据字段名取方法
List<Method> list = conflictingMethods.get(name);
if (list == null) {
list = new ArrayList<Method>();
conflictingMethods.put(name, list);
}
list.add(method);
}
这里是根据get和is开头的方法获取属性名作为键值,并且使用list作为value进行存储,为什么使用list那,我们看下面的方法
public void getUser(){}
public User getuser(){}
public List<User> getUser(){}
public void getUser(String id){}
上面三个方法都会以user为键进行存储,但是其方法名是一样的,所以这里要存储为list,即存储多个Method对象。
我们知道一个字段的属性的get或set方法,不可能出现上面的情况,所以针对上面的情况需要做处理,这里调用resolveGetterConflicts(conflicttingGetters),
private void resolveGetterConflicts(Map<String, List<Method>> conflictingGetters) {
//遍历conflictingGetters
for (Entry<String, List<Method>> entry : conflictingGetters.entrySet()) {
Method winner = null;
String propName = entry.getKey();
//循环value这里value是一个List<Method>类型
for (Method candidate : entry.getValue()) {
if (winner == null) {
winner = candidate;
continue;
}
//获得get方法的返回值类型
Class<?> winnerType = winner.getReturnType();
Class<?> candidateType = candidate.getReturnType();
//如果winnerType和candidateType相等,
if (candidateType.equals(winnerType)) {
if (!boolean.class.equals(candidateType)) {
throw new ReflectionException(
"Illegal overloaded getter method with ambiguous type for property "
+ propName + " in class " + winner.getDeclaringClass()
+ ". This breaks the JavaBeans specification and can cause unpredictable results.");
} else if (candidate.getName().startsWith("is")) {
winner = candidate;
}
} else if (candidateType.isAssignableFrom(winnerType)) {
// OK getter type is descendant
} else if (winnerType.isAssignableFrom(candidateType)) {
winner = candidate;
} else {
throw new ReflectionException(
"Illegal overloaded getter method with ambiguous type for property "
+ propName + " in class " + winner.getDeclaringClass()
+ ". This breaks the JavaBeans specification and can cause unpredictable results.");
}
}
addGetMethod(propName, winner);
}
}
上面的方法处理了上面提到的一个属性存在多个get方法的情况,最后调用addGetMethod方法,
private void addGetMethod(String name, Method method) {
if (isValidPropertyName(name)) {
getMethods.put(name, new MethodInvoker(method));
Type returnType = TypeParameterResolver.resolveReturnType(method, type);
getTypes.put(name, typeToClass(returnType));
}
}
上面的方法把信息放到了getMethods和getTyps中,分别存储了get方法和返回值。
上面分析了Reflector中的addGetMethods方法,addSetMethods方法和其处理过程类似,最终把set方法和返回值放到了setMethods和setTypes中。
addFileds(clazz)方法即是处理clazz中的属性,
private void addFields(Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (canAccessPrivateMethods()) {
try {
field.setAccessible(true);
} catch (Exception e) {
// Ignored. This is only a final precaution, nothing we can do.
}
}
if (field.isAccessible()) {
//检查是否存在set方法,如果不存在添加该field
if (!setMethods.containsKey(field.getName())) {
// issue #379 - removed the check for final because JDK 1.5 allows
// modification of final fields through reflection (JSR-133). (JGB)
// pr #16 - final static can only be set by the classloader
int modifiers = field.getModifiers();
if (!(Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers))) {
addSetField(field);
}
}
//检查是否存在get方法,如果不存在添加该field
if (!getMethods.containsKey(field.getName())) {
addGetField(field);
}
}
}
//添加父类的field
if (clazz.getSuperclass() != null) {
addFields(clazz.getSuperclass());
}
}
获得field之后,判断是否在getMethods和setMethods中,如果不在则进行添加,只看addSetField方法,
private void addSetField(Field field) {
if (isValidPropertyName(field.getName())) {
setMethods.put(field.getName(), new SetFieldInvoker(field));
Type fieldType = TypeParameterResolver.resolveFieldType(field, type);
setTypes.put(field.getName(), typeToClass(fieldType));
}
}
从上面看到如果一个field不存在set方法,则生成一个SetFieldInvoker把该对象放入setMethods,从这里可以看出一个setting配置的name值在configuration中可以没有set方法。同理也可以没有get方法。
上面分析完了settingsAsProperties方法中的下面这行代码,
MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
把Configuration中的构造方法、get方法、set方法、field放入了metaConfig中的reflector对象中的下列属性
private final String[] readablePropertyNames;
private final String[] writeablePropertyNames;
private final Map<String, Invoker> setMethods = new HashMap<String, Invoker>();
private final Map<String, Invoker> getMethods = new HashMap<String, Invoker>();
private final Map<String, Class<?>> setTypes = new HashMap<String, Class<?>>();
private final Map<String, Class<?>> getTypes = new HashMap<String, Class<?>>();
private Constructor<?> defaultConstructor;
2.2、校验配置的setting标签中的name是否存在
上面分析完了MetaClass.forClass方法,下面看如何对setting标签配置的name进行校验
for (Object key : props.keySet()) {
//从metaConfig的relector中的setMethods中判断是否存在该属性,setMethods中存储的是可写的属性,
//所以这里要到setMethods中进行判断
if (!metaConfig.hasSetter(String.valueOf(key))) {
throw new BuilderException("The setting " + key + " is not known. Make sure you spelled it correctly (case sensitive).");
}
}
遍历从setting标签解析出来的Properties对象,调用metaConfig.hasSetter方法,
public boolean hasSetter(String name) {
PropertyTokenizer prop = new PropertyTokenizer(name);
if (prop.hasNext()) {
if (reflector.hasSetter(prop.getName())) {
MetaClass metaProp = metaClassForProperty(prop.getName());
return metaProp.hasSetter(prop.getChildren());
} else {
return false;
}
} else {
return reflector.hasSetter(prop.getName());
}
}
看hasSetter的定义
public boolean hasSetter(String propertyName) {
return setMethods.keySet().contains(propertyName);
}
可以看到是判断setMethods是否存在该key,也就是已set方法为表标准,只要在setMethods中,便可以在
小结
上面分析了mybatis的核心配置文件中
三、解析typeAliases标签
概述
在mybatis核心配置文件(mybatis-config.xml)中有关typeAliases的配置如下,
<typeAliases>
<package name="cn.com.mybatis.bean"></package>
<typeAlias name="user" type="cn.com.mybatis.bean.User"></typeAlias>
</typeAliases>
上面给出了两种配置typeAlias的放式,一种是配置package标签,一种是typeAlias表。
我上面的配置是有问题的,在测试的时候一直报下面的错误,
上面的问题困扰了笔者好久,没找到原因,因为解析typeAliases标签的源码中找不到任何的原因,最后排查日志,原来是在加载核心配置文件的时候要把配置和mybatis的dtd文件进行验证,这里是验证出错了,具体的错误是typeAlias标签必须在package标签的前边,也就是标签是有顺序的。把配置改为下面的顺序,程序正常,
<typeAliases>
<typeAlias alias="user" type="cn.com.mybatis.bean.User"></typeAlias>
<package name="cn.com.mybatis.bean"/>
</typeAliases>
1、配置标签
package cn.com.mybatis.bean;
import org.apache.ibatis.type.Alias;
//配置别名为myMenu
@Alias(value="myMenu")
public class Menu {
private String menuId;
private String menuName;
private String url;
}
上面为Menu类配置了别名,在扫描该包的时候会使用自定义的别名,不会使用mybatis默认的别名规则(Class.getSimpleName())
2、配置标签
这种配置是单独为某个类配置别名,其中alias属性可以不配置,不配置则使用mybatis默认的别名规则,如下
<typeAlias alias="MyUser" type="cn.com.mybatis.bean.User"></typeAlias>
上面看了typeAlias的两种配置方式,那么何为typeAlias,意思就是给一个类配置一个别名,如这里有一个cn.com.mybatis.bean.User类,可以为其配置别名为MyUser,
那么在配置文件中便可以使用别名代替类的全限类名,目的是简便。这里需要注意的是配置的别名的使用范围仅限于mybatis的配置文件中(包含核心配置文件和Mpper映射文件)
详述
上面,了解了typeAlias的配置及作用,下面看mybatis是如何解析的。
在XMLConfigBuilder类中的parseConfiguration方法,
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
//解析properties标签
propertiesElement(root.evalNode("properties"));
//解析settings标签,1、把<setting>标签解析为Properties对象
Properties settings = settingsAsProperties(root.evalNode("settings"));
/*2、对<settings>标签中的<setting>标签中的内容进行解析,这里解析的是<setting name="vfsImpl" value=",">
* VFS是mybatis中用来表示虚拟文件系统的一个抽象类,用来查找指定路径下的资源。上面的key为vfsImpl的value可以是VFS的具体实现,必须
* 是权限类名,多个使用逗号隔开,如果存在则设置到configuration中的vfsImpl属性中,如果存在多个,则设置到configuration中的仅是最后一个
* */
loadCustomVfs(settings);
//解析别名标签,例<typeAlias alias="user" type="cn.com.bean.User"/>
typeAliasesElement(root.evalNode("typeAliases"));
//解析插件标签
pluginElement(root.evalNode("plugins"));
//解析objectFactory标签,此标签的作用是mybatis每次创建结果对象的新实例时都会使用ObjectFactory,如果不设置
//则默认使用DefaultObjectFactory来创建,设置之后使用设置的
objectFactoryElement(root.evalNode("objectFactory"));
//解析objectWrapperFactory标签
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析reflectorFactory标签
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
//解析environments标签
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
//解析<mappers>标签
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
从上面可以看出typeAliasesElement方法,此方法用来解析typeAliases标签及其子标签,
private void typeAliasesElement(XNode parent) {
if (parent != null) {
for (XNode child : parent.getChildren()) {
//1、解析package标签
if ("package".equals(child.getName())) {
String typeAliasPackage = child.getStringAttribute("name");
configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
} else {
//2、解析typeAlias标签
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);
}
}
}
}
}
typeAliasesElement方法会分别解析typeAliases标签的package和typeAlias子标签。通过上面的分析知道在配置的时候
1、解析标签
下面看typeAliasesElement方法中对package标签的解析,
if ("package".equals(child.getName())) {
String typeAliasPackage = child.getStringAttribute("name");
configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
}
从上面可以看到获取
configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
可以看到从Configuration中获得TypeAliasRegistry,然后调用其registerAliases方法,
public void registerAliases(String packageName){
registerAliases(packageName, Object.class);
}
又调用另外一个方法,如下,
/**
*
* 为包下的所有java bean注册别名
* @param packageName
* @param superType
*/
public void registerAliases(String packageName, Class<?> superType){
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
//把该包下的所有类进行加载,把其Class对象放到resolverUtil的matches中
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);
}
}
}
上面方法的作用是遍历给的的包名,把该包下的所有的类进行加载,并放到resolverUtil中的matches中,这里具体的遍历方法暂且不看。遍历完成后取出resolverUtil中的所有Class对象,只要不是匿名类、接口则执行registerAlias方法,
public void registerAlias(Class<?> type) {
//获得类的简单类名,如cn.com.mybatis.bean.User 则其简单名称为User
String alias = type.getSimpleName();
//判断类上是否存在@Alias注解
Alias aliasAnnotation = type.getAnnotation(Alias.class);
//如果存在@Alias注解,则使用注解上配置的value属性作为别名
if (aliasAnnotation != null) {
alias = aliasAnnotation.value();
}
registerAlias(alias, type);
}
看上面的方法,上面的方法先获得Class的简单类名,
//获得类的简单类名,如cn.com.mybatis.bean.User 则其简单名称为User
String alias = type.getSimpleName();
然后会判断类上是否有@Alias注解,如果有则取其value值作为类的别名,
//判断类上是否存在@Alias注解
Alias aliasAnnotation = type.getAnnotation(Alias.class);
//如果存在@Alias注解,则使用注解上配置的value属性作为别名
if (aliasAnnotation != null) {
alias = aliasAnnotation.value();
}
进行上面的判断,存在@Alias注解,使用其value值作为别名,否则使用类的简单类名(Class.getSimpleName()),然后执行registerAlias方法,
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);
}
上面的代码会把别名转化为英文的小写作为存入的key,使用对应的Class存入TYPE_ALIASES中。如果已经注册过该key则会抛出异常,也就是不允许重复注册或者相同的key是无法覆盖的。这里还有一个问题,如果我们配置的是别名中含有大写,那么注册的时候是小写的,在使用的时候是用配置的还是用注册的,例,上面的例子,
package cn.com.mybatis.bean;
import org.apache.ibatis.type.Alias;
//配置别名为myMenu
@Alias(value="myMenu")
public class Menu {
private String menuId;
private String menuName;
private String url;
}
这里配置的是myMenu,注册的确实下面的
可以看到注册之后的是mymenu。其实在使用的时候是大小写不敏感的,在匹配的时候会统一转化为小写,这样就可以对应TYPE_ALIASES中已注册的别名。
2、解析标签
上面分析了
解析
public void registerAlias(Class<?> type) {
//获得类的简单类名,如cn.com.mybatis.bean.User 则其简单名称为User
String alias = type.getSimpleName();
//判断类上是否存在@Alias注解
Alias aliasAnnotation = type.getAnnotation(Alias.class);
//如果存在@Alias注解,则使用注解上配置的value属性作为别名
if (aliasAnnotation != null) {
alias = aliasAnnotation.value();
}
registerAlias(alias, type);
}
该方法前面已分析,会判断配置的类是否含有@Alias注解,如果有则使用注解上的value值。这里存在一个问题,如果在
在使用
下面看registerAlias(alias,type)方法,
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);
}
此方法上面分析过,如果存在相同的key会抛异常,最终存入TYPE_ALIASES中。
小结
本章分析了mybatis核心配置文件(mybatis-config.xml)的
另在写Mapper映射文件和核心配置文件的时候会使用一些自定义的别名,这些别名是怎么注册的那,在Configuration、TypeAliasRegistry类中进行了注册,如下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);
}
在TypeAliasRegistry中注册了下面的别名,
//默认的构造方法,初始化系统内置的别名
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);
}
上面两个类注册了系统内置的别名,在核心配置文件和Mapper映射文件中可使用,mybatis会自动映射其注册类型,且大小写不区分。
四、解析plugins标签
概述
在mybatis的核心配置文件(mybatis-config.xml)文件中,有关plugins的配置如下,
<!-- 拦截器 -->
<plugins>
<plugin interceptor="cn.com.mybatis.plugins.MyInterceptor" />
</plugins>
在mybatis的plugins叫做插件,其实也可以理解为拦截器。在plugins标签中配置plugin子标签,plugin子标签可以配置多个,多个子标签是有顺序的。除了上面的配置方式在每个plugin下还可以配置property子标签,
<!-- 拦截器 -->
<plugins>
<plugin interceptor="cn.com.mybatis.plugins.MyInterceptor" />
<plugin interceptor="cn.com.mybatis.plugins.MyInterceptor2">
<property name="name" value="mybatis"/>
<property name="age" value="10"/>
</plugin>
</plugins>
上面的配置在第二个拦截器中新增了两个属性name和age,这两个属性会被解析为Properties对象。
在上面可以看到一个重要的配置是plugin标签中的interceptor属性,该属性配置的是一个拦截器类,对应该类有什么要求那,一个普通的类就可以吗,答案是就是一个普通的类,但必须满足下面的几个条件,才可以被mybatis作为插件使用,
- 实现mybatis中的Interceptor接口,并重写其中的三个方法;
在类上声明@Intercepts注解,该注解标明该拦截器拦截的方法,一个例子如下,
@Intercepts({@Signature(type=Executor.class,method=”query”,args= {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
拦截的是实现了Executor接口的query方法,后面的args配置的该方法的入参。在mybatis中默认拦截以下接口的方法,
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)
Executor(执行器)、ParameterHandler(参数处理器)、ResultSetHandler(结果集处理器)、StatementHandler(SQL语法构建器)在mybatis中是四个顶级接口,贯穿了mybatis执行sql的全过程,从这里可以看出mybatis的拦截器的强大。
下面看下我的拦截器MyInterceptor的源码,
package cn.com.mybatis.plugins;
import java.util.Properties;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
@Intercepts({@Signature(type=Executor.class,method="query",args= {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MyInterceptor implements Interceptor{
/**
* incation 封装了拦截的类,方法,方法参数
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
// TODO Auto-generated method stub
//执行被拦截的类中的方法
Object o=invocation.proceed();
return o;
}
/**
* 返回一个JDK代理对象
*/
@Override
public Object plugin(Object target) {
// TODO Auto-generated method stub
return Plugin.wrap(target, this);
}
/**
* 允许在配置文件中配置一些属性即
* <plugin interceptor="">
* <property name ="" value =""/>
* </plugin>
*/
@Override
public void setProperties(Properties properties) {
// TODO Auto-generated method stub
}
}
详述
上面,了解了plugis标签的基本配置及使用,下面看mybatis是如何解析的。
在XMLConfigBuilder类中的parseConfiguration方法
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
//解析properties标签
propertiesElement(root.evalNode("properties"));
//解析settings标签,1、把<setting>标签解析为Properties对象
Properties settings = settingsAsProperties(root.evalNode("settings"));
/*2、对<settings>标签中的<setting>标签中的内容进行解析,这里解析的是<setting name="vfsImpl" value=",">
* VFS是mybatis中用来表示虚拟文件系统的一个抽象类,用来查找指定路径下的资源。上面的key为vfsImpl的value可以是VFS的具体实现,必须
* 是权限类名,多个使用逗号隔开,如果存在则设置到configuration中的vfsImpl属性中,如果存在多个,则设置到configuration中的仅是最后一个
* */
loadCustomVfs(settings);
//解析别名标签,例<typeAlias alias="user" type="cn.com.bean.User"/>
typeAliasesElement(root.evalNode("typeAliases"));
//解析插件标签
pluginElement(root.evalNode("plugins"));
//解析objectFactory标签,此标签的作用是mybatis每次创建结果对象的新实例时都会使用ObjectFactory,如果不设置
//则默认使用DefaultObjectFactory来创建,设置之后使用设置的
objectFactoryElement(root.evalNode("objectFactory"));
//解析objectWrapperFactory标签
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析reflectorFactory标签
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
//解析environments标签
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
//解析<mappers>标签
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
从上面的代码中,我们找到下面这行解析plugins标签的代码,
//解析插件标签
pluginElement(root.evalNode("plugins"));
上面折行代码即在解析配置文件中的plugis标签,其定义如下
/**
* 解析<plugin>标签
* @param parent
* @throws Exception
*/
private void pluginElement(XNode parent) throws Exception {
if (parent != null) {
for (XNode child : parent.getChildren()) {
//1、获得interceptor属性
String interceptor = child.getStringAttribute("interceptor");
//2、把plugin标签下的property标签的内容解析为Properties对象
Properties properties = child.getChildrenAsProperties();
//3、使用配置的interceptor生成一个实例,配置的interceptor需要实现interceptor接口
Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
//4、执行interceptor的setProperties方法,把配置的properties标签中的值设置到setProperties中
interceptorInstance.setProperties(properties);
//5、添加interceptor
configuration.addInterceptor(interceptorInstance);
}
}
}
1、解析标签
从上面的代码中可以看到在循环plugin标签,取得interceptor配置的类的全限类名,然后获得plugin标签下的property标签的内容,调用getChildrenAsProperties方法,
public Properties getChildrenAsProperties() {
Properties properties = new Properties();
for (XNode child : getChildren()) {
String name = child.getStringAttribute("name");
String value = child.getStringAttribute("value");
if (name != null && value != null) {
properties.setProperty(name, value);
}
}
return properties;
}
解析完property标签后,下面会生成interceptor实例。
2、生成interceptor实例
上面解析完plugin标签的内容后会生成interceptor实例,且调用其setProperty方法,
//使用配置的interceptor生成一个实例,配置的interceptor需要实现interceptor接口
Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
//执行interceptor的setProperties方法,把配置的properties标签中的值设置到setProperties中
interceptorInstance.setProperties(properties);
从上面的代码可以看到把配置的interceptor全限类名解析为一个Class对象,然后调用其默认的构造方法获得一个实例,然后调用了其setProperties方法,即把在标签中配置的property属性值注入到interceptor实例中,在我的例子中即调用下面的方法,
/**
* 允许在配置文件中配置一些属性即
* <plugin interceptor="">
* <property name ="" value =""/>
* </plugin>
*/
@Override
public void setProperties(Properties properties) {
// TODO Auto-generated method stub
this.properties=properties;
}
这样就可以把标签中的内容转化到interceptor实例中的属性。
3、添加到configuration中
经过上面的两个步骤的分析,拦截器已经从配置文件中的配置转化为了一个实例,下面就是要放入核心配置类configuration中,
configuration.addInterceptor(interceptorInstance);
看addInterceptor方法,该方法是configuration类中的方法,
public void addInterceptor(Interceptor interceptor) {
interceptorChain.addInterceptor(interceptor);
}
调用了interceptorChain的addInterceptor方法,interceptorChain使用下面的方式进行初始化,
//拦截器链
protected final InterceptorChain interceptorChain = new InterceptorChain();
其addInterceptor方法如下,
可以看到最终放到了interceptors中,这是一个ArrayList。
最终完成了解析plugins标签并把拦截器实例放到configuration中的过程。
小结
本章分析了mybatis中plugins标签的解析过程,该标签的解析过程相对简单,重点是拦截器背后的原理,是如何起到拦截作用,待下一章分析。
五、解析mappers标签
概述
在mybatis的核心配置文件(mybatis-config.xml)中,有关mappers的配置如下,
<mappers>
<!-- <mapper resource="cn/com/mybatis/dao/UserMapper.xml"/>
<mapper resource="cn/com/mybatis/dao/MenuMapper.xml"/> -->
<!--第二种做法 -->
<package name="cn.com.mybatis.dao" />
</mappers>
从上面的配置文件,可以看到配置mappers文件有两种方式,一种是配置mapper标签,另一种是配置package标签。从配置的内容上来看,其配置的方式也是存在差别,配置mapper标签配置的是一个xml文件,该文件中存在相关的sql语句;配置package标签配置的是一个包的权限路径(在spring和mybatis结合的时候使用了此种方式),该包表示的是mapper的接口文件。
最终上面的两种方式都会被解析到mybatis的configuration类中,供用户使用。如果存在重复配置mybatis会如何处理,下面在分析过程中会解答该问题。
详述
上面了解了
在XMLConfigBuilder类中的parseConfiguration方法
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
//解析properties标签
propertiesElement(root.evalNode("properties"));
//解析settings标签,1、把<setting>标签解析为Properties对象
Properties settings = settingsAsProperties(root.evalNode("settings"));
/*2、对<settings>标签中的<setting>标签中的内容进行解析,这里解析的是<setting name="vfsImpl" value=",">
* VFS是mybatis中用来表示虚拟文件系统的一个抽象类,用来查找指定路径下的资源。上面的key为vfsImpl的value可以是VFS的具体实现,必须
* 是权限类名,多个使用逗号隔开,如果存在则设置到configuration中的vfsImpl属性中,如果存在多个,则设置到configuration中的仅是最后一个
* */
loadCustomVfs(settings);
//解析别名标签,例<typeAlias alias="user" type="cn.com.bean.User"/>
typeAliasesElement(root.evalNode("typeAliases"));
//解析插件标签
pluginElement(root.evalNode("plugins"));
//解析objectFactory标签,此标签的作用是mybatis每次创建结果对象的新实例时都会使用ObjectFactory,如果不设置
//则默认使用DefaultObjectFactory来创建,设置之后使用设置的
objectFactoryElement(root.evalNode("objectFactory"));
//解析objectWrapperFactory标签
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析reflectorFactory标签
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
//解析environments标签
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
//解析<mappers>标签
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
在该方法的最下方,看下面这行代码
//解析<mappers>标签
mapperElement(root.evalNode("mappers"));
上面这行代码便是解析mappers标签的方法的调用。看其方法定义,
/**
* 解析<mappers>标签,在此标签中可以配置<mapper>和<package>两种标签,其中<mapper>标签可以配置resource、url、class三种属性,
* 这里的三种属性,仅可以同时出现一个;<package>标签只需要配置包名即可。
* @param parent
* @throws Exception
*/
private void mapperElement(XNode parent) throws Exception {
if (parent != null) {
for (XNode child : parent.getChildren()) {
//1、解析package标签,获得name属性即包名
if ("package".equals(child.getName())) {
String mapperPackage = child.getStringAttribute("name");
//扫描包名,把
configuration.addMappers(mapperPackage);
} else {//2、解析<mapper>标签,标签中可以配置resource、url、class三个属性,但只能配置其中一个。
String resource = child.getStringAttribute("resource");
String url = child.getStringAttribute("url");
String mapperClass = child.getStringAttribute("class");
if (resource != null && url == null && mapperClass == null) {
ErrorContext.instance().resource(resource);
InputStream inputStream = Resources.getResourceAsStream(resource);
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
/**
* 处理mapper文件和对应的接口
*/
mapperParser.parse();
} else if (resource == null && url != null && mapperClass == null) {
ErrorContext.instance().resource(url);
InputStream inputStream = Resources.getUrlAsStream(url);
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
mapperParser.parse();
} else if (resource == null && url == null && mapperClass != null) {
Class<?> mapperInterface = Resources.classForName(mapperClass);
configuration.addMapper(mapperInterface);
} else {
throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
}
}
}
}
}
通过上面代码的分析及在配置文件中的配置,解析
1、解析package子标签
从上面的方法也就是mapperElement方法中,可以知道在解析
//1、解析package标签,获得name属性即包名
if ("package".equals(child.getName())) {
String mapperPackage = child.getStringAttribute("name");
//扫描包名,把
configuration.addMappers(mapperPackage);
}
上面的代码,解析出package子标签中的包名,调用了configuration.addMappers方法,
public void addMappers(String packageName) {
mapperRegistry.addMappers(packageName);
}
调用了mapperRegistry.addMappers方法,
/**
* @since 3.2.2
*/
public void addMappers(String packageName) {
addMappers(packageName, Object.class);
}
下面看addMappers方法,
public void addMappers(String packageName, Class<?> superType) {
//解析packageName下的class文件
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
//处理解析好的mapper接口文件
for (Class<?> mapperClass : mapperSet) {
addMapper(mapperClass);
}
}
上面的方法首先会解析指定包下的class文件,看下面的解析过程,
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
看find方法,
public ResolverUtil<T> find(Test test, String packageName) {
//把包名中的“.”替换成“/”
String path = getPackagePath(packageName);
try {
//获得包路径下的所有文件名称
List<String> children = VFS.getInstance().list(path);
for (String child : children) {
if (child.endsWith(".class")) {
addIfMatching(test, child);
}
}
} catch (IOException ioe) {
log.error("Could not read package: " + packageName, ioe);
}
return this;
}
遍历包下的所有class文件,调用addIfMatching方法,
@SuppressWarnings("unchecked")
protected void addIfMatching(Test test, String fqn) {
try {
String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.');
ClassLoader loader = getClassLoader();
if (log.isDebugEnabled()) {
log.debug("Checking to see if class " + externalName + " matches criteria [" + test + "]");
}
Class<?> type = loader.loadClass(externalName);
if (test.matches(type)) {
matches.add((Class<T>) type);
}
} catch (Throwable t) {
log.warn("Could not examine class '" + fqn + "'" + " due to a " +
t.getClass().getName() + " with message: " + t.getMessage());
}
}
加载class文件,判断是否符合test.matches,该方法如下,
/** Returns true if type is assignable to the parent type supplied in the constructor. */
@Override
public boolean matches(Class<?> type) {
return type != null && parent.isAssignableFrom(type);
}
如果符合条件则放入matches中,matches定义在ResolverUtil中。回到addMappers方法中,find方法结束后调用下面的方法,获取matches中的值,
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
然后循环解析mapperSet,
//处理解析好的mapper接口文件
for (Class<?> mapperClass : mapperSet) {
addMapper(mapperClass);
}
解析过程如下,
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {//判断是否为接口
if (hasMapper(type)) {//如果knownMappers中已经存在该type,则抛出异常
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
//把type放入knownMappers中,其value为一个MapperProxyFactory对象
knownMappers.put(type, new MapperProxyFactory<T>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
//对mapper文件进行解析,
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
//具体的解析过程,1、先解析对应的XML映射文件,2、再解析接口方法中的注解信息
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {//如果解析失败,则删除knowMapper中的信息
knownMappers.remove(type);
}
}
}
}
把mapper接口类封装为MapperProxyFactory对象,并放入knownMappers中,接着对接口类进行解析,如果解析失败会把刚才放入knownMappers中的值从knownMappers中移除。下面看如何解析接口类(解析对应的XML文件),
public void parse() {
String resource = type.toString();
if (!configuration.isResourceLoaded(resource)) {
//解析和接口同名的xml文件,前提是存在该文件,如果不存在该文件要怎么解析那?答案是解析接口中方法上的注解
/**
* 解析和接口同名的xml配置文件,最终要做的是把xml文件中的标签,转化为mapperStatement,
* 并放入mappedStatements中
*
*/
loadXmlResource();
configuration.addLoadedResource(resource);
assistant.setCurrentNamespace(type.getName());
//解析接口上的@CacheNamespace注解
parseCache();
parseCacheRef();
//获得接口中的所有方法,并解析方法上的注解
Method[] methods = type.getMethods();
for (Method method : methods) {
try {
// issue #237
if (!method.isBridge()) {
//解析方法上的注解
parseStatement(method);
}
} catch (IncompleteElementException e) {
configuration.addIncompleteMethod(new MethodResolver(this, method));
}
}
}
parsePendingMethods();
}
上面的解析分为两个过程,首先解析对应的XML映射文件,再解析方法上的注解。
1.1、解析xml文件
下面看如何继续对应的XML文件,
loadXmlResource();
看如何解析xml文件,
private void loadXmlResource() {
// Spring may not know the real resource name so we check a flag
// to prevent loading again a resource twice
// this flag is set at XMLMapperBuilder#bindMapperForNamespace
if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
//解析对应的XML映射文件,其名称为接口类+"."+xml,即和接口类同名且在同一个包下。
String xmlResource = type.getName().replace('.', '/') + ".xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
} catch (IOException e) {
// ignore, resource is not required
}
if (inputStream != null) {
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
//解析xml映射文件
xmlParser.parse();
}
}
}
首先确定XML映射文件的位置,和接口类同名且在同一个包下。如下的例子,
确定好对应的映射文件位置,接着便是解析该xml文件,
if (inputStream != null) {
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
//解析xml映射文件
xmlParser.parse();
}
解析过程如下,
public void parse() {
if (!configuration.isResourceLoaded(resource)) {
//解析mapper文件中的<mapper>标签及其子标签
configurationElement(parser.evalNode("/mapper"));
configuration.addLoadedResource(resource);
bindMapperForNamespace();
}
parsePendingResultMaps();
parsePendingCacheRefs();
parsePendingStatements();
}
解析的过程在解析
1.2、解析接口中方法上的注解
上面解析了接口对于的XML文件,下面看如何解析接口中的方法,
//获得接口中的所有方法,并解析方法上的注解
Method[] methods = type.getMethods();
for (Method method : methods) {
try {
// issue #237
if (!method.isBridge()) {
//解析方法上的注解
parseStatement(method);
}
} catch (IncompleteElementException e) {
configuration.addIncompleteMethod(new MethodResolver(this, method));
}
看parseStatement方法,
void parseStatement(Method method) {
Class<?> parameterTypeClass = getParameterType(method);
LanguageDriver languageDriver = getLanguageDriver(method);
//获得方法上的注解,并生成SqlSource
SqlSource sqlSource = getSqlSourceFromAnnotations(method, parameterTypeClass, languageDriver);
if (sqlSource != null) {
Options options = method.getAnnotation(Options.class);
//生成mappedStatementId,为接口的权限类名+方法名。从这里可以得出同一个接口或namespace中不允许有同名的方法名或id
final String mappedStatementId = type.getName() + "." + method.getName();
Integer fetchSize = null;
Integer timeout = null;
StatementType statementType = StatementType.PREPARED;
ResultSetType resultSetType = ResultSetType.FORWARD_ONLY;
SqlCommandType sqlCommandType = getSqlCommandType(method);
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
boolean flushCache = !isSelect;
boolean useCache = isSelect;
KeyGenerator keyGenerator;
String keyProperty = "id";
String keyColumn = null;
if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
// first check for SelectKey annotation - that overrides everything else
SelectKey selectKey = method.getAnnotation(SelectKey.class);
if (selectKey != null) {
keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method), languageDriver);
keyProperty = selectKey.keyProperty();
} else if (options == null) {
keyGenerator = configuration.isUseGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
} else {
keyGenerator = options.useGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
keyProperty = options.keyProperty();
keyColumn = options.keyColumn();
}
} else {
keyGenerator = NoKeyGenerator.INSTANCE;
}
if (options != null) {
if (FlushCachePolicy.TRUE.equals(options.flushCache())) {
flushCache = true;
} else if (FlushCachePolicy.FALSE.equals(options.flushCache())) {
flushCache = false;
}
useCache = options.useCache();
fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE ? options.fetchSize() : null; //issue #348
timeout = options.timeout() > -1 ? options.timeout() : null;
statementType = options.statementType();
resultSetType = options.resultSetType();
}
String resultMapId = null;
ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class);
if (resultMapAnnotation != null) {
String[] resultMaps = resultMapAnnotation.value();
StringBuilder sb = new StringBuilder();
for (String resultMap : resultMaps) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(resultMap);
}
resultMapId = sb.toString();
} else if (isSelect) {
resultMapId = parseResultMap(method);
}
assistant.addMappedStatement(
mappedStatementId,
sqlSource,
statementType,
sqlCommandType,
fetchSize,
timeout,
// ParameterMapID
null,
parameterTypeClass,
resultMapId,
getReturnType(method),
resultSetType,
flushCache,
useCache,
// TODO gcode issue #577
false,
keyGenerator,
keyProperty,
keyColumn,
// DatabaseID
null,
languageDriver,
// ResultSets
options != null ? nullOrEmpty(options.resultSets()) : null);
}
}
从上面的代码,可以看出最终调用了assistant.addMappedStatement方法,该方法会把注解信息封装为MappedStatement对象,放入configuration中。详细过程,后面分析。
2、解析mapper子标签
上面分析了mybatis解析
//2、解析<mapper>标签,标签中可以配置resource、url、class三个属性,但只能配置其中一个。
String resource = child.getStringAttribute("resource");
String url = child.getStringAttribute("url");
String mapperClass = child.getStringAttribute("class");
if (resource != null && url == null && mapperClass == null) {
ErrorContext.instance().resource(resource);
InputStream inputStream = Resources.getResourceAsStream(resource);
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
/**
* 处理mapper文件和对应的接口
*/
mapperParser.parse();
} else if (resource == null && url != null && mapperClass == null) {
ErrorContext.instance().resource(url);
InputStream inputStream = Resources.getUrlAsStream(url);
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
mapperParser.parse();
} else if (resource == null && url == null && mapperClass != null) {
Class<?> mapperInterface = Resources.classForName(mapperClass);
configuration.addMapper(mapperInterface);
} else {
throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
}
前边说过,在
小结
本章分析了mybatis解析
六、解析mappers标签(解析resource和url属性)
概述
在上篇文章中分析了
下面看部分代码,
if (resource != null && url == null && mapperClass == null) {
ErrorContext.instance().resource(resource);
InputStream inputStream = Resources.getResourceAsStream(resource);
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
/**
* 处理mapper文件和对应的接口
*/
mapperParser.parse();
}
可以看到调用了XMLMapperBuilder的parse方法,
public void parse() {
if (!configuration.isResourceLoaded(resource)) {
//1、解析mapper文件中的<mapper>标签及其子标签,并设置CurrentNamespace的值,供下面第2步使用
configurationElement(parser.evalNode("/mapper"));
//这里想loadResoures中设置的是XML映射文件的路径
configuration.addLoadedResource(resource);
//2、绑定Mapper接口,并解析对应的XML映射文件
bindMapperForNamespace();
}
parsePendingResultMaps();
parsePendingCacheRefs();
parsePendingStatements();
}
从上面的代码中可以看出首先解析resource资源说代表的XML映射文件,然后解析XML;映射文件中的namespace配置的接口。
详述
通过上面的分析知道,解析resource配置的XML映射文件,分为两步,第一步就是解析XML映射文件的内容;第二步是解析XML映射文件中配置的namespace属性,也就是对于的Mapper接口。
1、解析XML映射文件
看如何解析XML映射文件内容,也就是下面的这行代码,
configurationElement(parser.evalNode("/mapper"));
看具体的实现,
/**
* 解析XML映射文件的内容
* @param context
*/
private void configurationElement(XNode context) {
try {
//获得namespace属性
String namespace = context.getStringAttribute("namespace");
if (namespace == null || namespace.equals("")) {
throw new BuilderException("Mapper's namespace cannot be empty");
}
//设置到currentNamespace中
builderAssistant.setCurrentNamespace(namespace);
//解析<cache-ref>标签
cacheRefElement(context.evalNode("cache-ref"));
//二级缓存标签
cacheElement(context.evalNode("cache"));
parameterMapElement(context.evalNodes("/mapper/parameterMap"));
resultMapElements(context.evalNodes("/mapper/resultMap"));
sqlElement(context.evalNodes("/mapper/sql"));
//解析select、insert、update、delete子标签
buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
} catch (Exception e) {
throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
}
}
上面方法解析XML映射文件的内容,其中有个和二级缓存相关的配置,即
在XML映射文件中可以配置上面的这些标签,也就是上面方法中解析的内容。重点看解析select、update、delete、select。也就是下面这行代码
//解析select、insert、update、delete子标签
buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
其方法定义如下,
private void buildStatementFromContext(List<XNode> list) {
if (configuration.getDatabaseId() != null) {
buildStatementFromContext(list, configuration.getDatabaseId());
}
buildStatementFromContext(list, null);
}
这里会校验databaseId,如果自定义配置了,则使用自定义的,否则使用默认的,看方法buildStatementFromContext方法
private void buildStatementFromContext(List<XNode> list, String requiredDatabaseId) {
for (XNode context : list) {
final XMLStatementBuilder statementParser = new XMLStatementBuilder(configuration, builderAssistant, context, requiredDatabaseId);
try {
statementParser.parseStatementNode();
} catch (IncompleteElementException e) {
configuration.addIncompleteStatement(statementParser);
}
}
}
调用XMLStatementBuilder的parseStatementNode方法
/**
* 解析select、update、delete、insert标签
*/
public void parseStatementNode() {
String id = context.getStringAttribute("id");
String databaseId = context.getStringAttribute("databaseId");
if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {
return;
}
Integer fetchSize = context.getIntAttribute("fetchSize");
Integer timeout = context.getIntAttribute("timeout");
String parameterMap = context.getStringAttribute("parameterMap");
String parameterType = context.getStringAttribute("parameterType");
Class<?> parameterTypeClass = resolveClass(parameterType);
String resultMap = context.getStringAttribute("resultMap");
String resultType = context.getStringAttribute("resultType");
String lang = context.getStringAttribute("lang");
LanguageDriver langDriver = getLanguageDriver(lang);
Class<?> resultTypeClass = resolveClass(resultType);
String resultSetType = context.getStringAttribute("resultSetType");
StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType", StatementType.PREPARED.toString()));
ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
String nodeName = context.getNode().getNodeName();
SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
//查询语句默认开启一级缓存,这里默认是true
boolean useCache = context.getBooleanAttribute("useCache", isSelect);
boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false);
// Include Fragments before parsing
XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant);
includeParser.applyIncludes(context.getNode());
// Parse selectKey after includes and remove them.
processSelectKeyNodes(id, parameterTypeClass, langDriver);
// Parse the SQL (pre: <selectKey> and <include> were parsed and removed)
//生成SqlSource,这里分两种,DynamicSqlSource和RawSqlSource
SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
String resultSets = context.getStringAttribute("resultSets");
String keyProperty = context.getStringAttribute("keyProperty");
String keyColumn = context.getStringAttribute("keyColumn");
KeyGenerator keyGenerator;
//例,id="selectUser"
//这里的keyStatementId=selectUser!selectKey
String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX;
//keyStatementId=cn.com.dao.userMapper.selectUser!selectKey
keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true);
if (configuration.hasKeyGenerator(keyStatementId)) {
keyGenerator = configuration.getKeyGenerator(keyStatementId);
} else {
keyGenerator = context.getBooleanAttribute("useGeneratedKeys",
configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType))
? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
}
builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
resultSetTypeEnum, flushCache, useCache, resultOrdered,
keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
}
上面的代码主要是解析标签中的各种属性,那么标签中可以配置哪些属性那,下面看select标签的属性,详情可参见https://www.w3cschool.cn/mybatis/f4uw1ilx.html
上面是select标签中可以配置的属性列表。
上面的代码重点看以下重点
二级缓存
下面看和缓存相关的
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
//查询语句默认开启一级缓存,这里默认是true
boolean useCache = context.getBooleanAttribute("useCache", isSelect);
这里仅针对select查询语句使用缓存,这里的默认不会刷新缓存flushCache为false,默认开启缓存useCache为ture,这里的缓存指的是一级缓存,经常说的mybatis一级缓存,一级缓存是sqlSession级别的。
看完了一级缓存,下面看SqlSource的内容
SqlSource
下面是SqlSource相关的,
//生成SqlSource,这里分两种,DynamicSqlSource和RawSqlSource
SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
上面是生成SqlSource的过程,
@Override
public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
return builder.parseScriptNode();
}
看parseScriptNode方法
public SqlSource parseScriptNode() {
MixedSqlNode rootSqlNode = parseDynamicTags(context);
SqlSource sqlSource = null;
if (isDynamic) {
//含义${}符合的为DynamicSqlSource
sqlSource = new DynamicSqlSource(configuration, rootSqlNode);
} else {
//不含有${}的为rawSqlSource
sqlSource = new RawSqlSource(configuration, rootSqlNode, parameterType);
}
return sqlSource;
}
从上面的代码可以看到在映射文件中根据参数占位符的标识符(${}、#{})分为DynamicSqlSource和RawSqlSource。具体如何判断,后面详细分析。
addMappedStatement
最后看builderAssistant.addMappedStatement方法,
public MappedStatement addMappedStatement(
String id,
SqlSource sqlSource,
StatementType statementType,
SqlCommandType sqlCommandType,
Integer fetchSize,
Integer timeout,
String parameterMap,
Class<?> parameterType,
String resultMap,
Class<?> resultType,
ResultSetType resultSetType,
boolean flushCache,
boolean useCache,
boolean resultOrdered,
KeyGenerator keyGenerator,
String keyProperty,
String keyColumn,
String databaseId,
LanguageDriver lang,
String resultSets) {
if (unresolvedCacheRef) {
throw new IncompleteElementException("Cache-ref not yet resolved");
}
//cn.com.dao.UserMapper.selectUser
id = applyCurrentNamespace(id, false);
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType)
.resource(resource)
.fetchSize(fetchSize)
.timeout(timeout)
.statementType(statementType)
.keyGenerator(keyGenerator)
.keyProperty(keyProperty)
.keyColumn(keyColumn)
.databaseId(databaseId)
.lang(lang)
.resultOrdered(resultOrdered)
.resultSets(resultSets)
.resultMaps(getStatementResultMaps(resultMap, resultType, id))
.resultSetType(resultSetType)
.flushCacheRequired(valueOrDefault(flushCache, !isSelect))
.useCache(valueOrDefault(useCache, isSelect))
.cache(currentCache);
ParameterMap statementParameterMap = getStatementParameterMap(parameterMap, parameterType, id);
if (statementParameterMap != null) {
statementBuilder.parameterMap(statementParameterMap);
}
MappedStatement statement = statementBuilder.build();
/*向mappedStatements字段中加入MappedStatement,这里会加入两个key
* cn.com.dao.UserMapper.selectUser statement
* selectUser statement
* 每次都会插入上面的两种key,两种key对应的value都是同一个statement
*
*/
configuration.addMappedStatement(statement);
return statement;
}
该方法主要完成的功能是生成MappedStatement,且放入configuration中。
2、解析namespace属性
上面分析了解析XML映射文件的内容的过程,最后的结果是把XML映射文件中的select、update、insert、delete标签的内容解析为MappedStatement。下面看解析XML映射文件中的namespace属性,
//2、绑定Mapper接口,并解析对应的XML映射文件
bindMapperForNamespace();
上面我给的注释是绑定接口并解析对应的XML映射文件,这个方法没有参数,怎么绑定具体的接口并解析对应的映射文件那,
private void bindMapperForNamespace() {
String namespace = builderAssistant.getCurrentNamespace();
if (namespace != null) {
Class<?> boundType = null;
try {
//加载类,这里加载的是mapper文件中配置的namespace配置的接口
boundType = Resources.classForName(namespace);
} catch (ClassNotFoundException e) {
//ignore, bound type is not required
}
if (boundType != null) {
if (!configuration.hasMapper(boundType)) {//判断该接口是否被加载过,在mapperRegistry中的knowsMapper中判断
// Spring may not know the real resource name so we set a flag
// to prevent loading again this resource from the mapper interface
// look at MapperAnnotationBuilder#loadXmlResource
//把该Mapper接口作为已加载的资源存放到loadedResources中,loadedResources存放的是已加载的mapper接口的路径,和第一步设置的XML映射文件路径不是同一个值。
configuration.addLoadedResource("namespace:" + namespace);
//把该接口放到mapperRegistry中的knowsMapper中,并解析该接口,根据loadedResources判定是否需要解析该Mapper接口
configuration.addMapper(boundType);
}
}
}
}
获得builderAssistant.getCurrentNamespace(),在解析XML映射文件时,第一步便是设置该属性,这里用到的便是上一步中设置的那个XML映射文件中的namespace属性值。获得该接口的名称,判断是否生成过MapperProxyFactory,即放入过knownMappers中,看configuration.hasMapper方法,
public boolean hasMapper(Class<?> type) {
return mapperRegistry.hasMapper(type);
}
public <T> boolean hasMapper(Class<T> type) {
return knownMappers.containsKey(type);
}
如果在knownMappers中,则不进行解析,如果不在才进行下面的逻辑处理,调用configuration.addLoadedResource方法,放入loadedResources中,标识在第一步已经解析过对应的XML映射文件;调用configuration.addMapper方法,解析该接口,这个过程和在
小结
本章分析了mappers标签中mapper子标签中resource和url属性的解析过程,首先解析对应的XML映射文件,解析的结果为MappedStatement对象,然后解析其namespace对应的接口,解析的结果为MapperProxyFactory对象。
七、解析mappers标签(解析class属性)
概述
在mybatis的核心配置文件中配置mappers标签有以下方式,
<mappers>
<mapper class="cn.com.mybatis.dao.UserMapper"/>
</mappers>
上面这种方式便是mapper标签的class属性配置方式,其解析部分过程如下,
else if (resource == null && url == null && mapperClass != null) {
Class<?> mapperInterface = Resources.classForName(mapperClass);
configuration.addMapper(mapperInterface);
} else {
throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
}
可以看到主要是调用了configuration.addMapper方法,和上篇文章中解析namespace调用的方法是一致的。看其具体实现
public <T> void addMapper(Class<T> type) {
mapperRegistry.addMapper(type);
}
下方分析mapperRegistry.addMapper方法。
详述
mapperRegistry.addMapper方法的定义如下,
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {//判断是否为接口
if (hasMapper(type)) {//如果knownMappers中已经存在该type,则抛出异常
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
//1、把type放入knownMappers中,其value为一个MapperProxyFactory对象
knownMappers.put(type, new MapperProxyFactory<T>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
//2、对mapper文件及注解进行解析,初始化了sqlAnnotationTypessqlProviderAnnotationTypes两个变量
//具体的解析过程,1、先解析对应的XML映射文件,2、再解析接口方法中的注解信息
/**sqlAnnotationTypes.add(Select.class);
sqlAnnotationTypes.add(Insert.class);
sqlAnnotationTypes.add(Update.class);
sqlAnnotationTypes.add(Delete.class);
sqlProviderAnnotationTypes.add(SelectProvider.class);
sqlProviderAnnotationTypes.add(InsertProvider.class);
sqlProviderAnnotationTypes.add(UpdateProvider.class);
sqlProviderAnnotationTypes.add(DeleteProvider.class);
*
*/
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {//3、如果解析失败,则删除knowMapper中的信息
knownMappers.remove(type);
}
}
}
}
该方法主要分为下面几个步骤。
1、检查是否解析过接口
首先会判断knowMappers中是否已经存在该接口,如果存在则会抛出异常
if (hasMapper(type)) {//如果knownMappers中已经存在该type,则抛出异常
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
如果不存在则放入knownMappers中,
//1、把type放入knownMappers中,其value为一个MapperProxyFactory对象
knownMappers.put(type, new MapperProxyFactory<T>(type));
继续解析对应的映射文件及接口方法注解
2、解析接口对应的映射文件及接口方法注解
上面把mapper接口放入了knownMappers中,接着需要解析映射文件及注解,
//2、对mapper文件及注解进行解析,初始化了sqlAnnotationTypessqlProviderAnnotationTypes两个变量
//具体的解析过程,1、先解析对应的XML映射文件,2、再解析接口方法中的注解信息
/**sqlAnnotationTypes.add(Select.class);
sqlAnnotationTypes.add(Insert.class);
sqlAnnotationTypes.add(Update.class);
sqlAnnotationTypes.add(Delete.class);
sqlProviderAnnotationTypes.add(SelectProvider.class);
sqlProviderAnnotationTypes.add(InsertProvider.class);
sqlProviderAnnotationTypes.add(UpdateProvider.class);
sqlProviderAnnotationTypes.add(DeleteProvider.class);
*
*/
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
上面的代码,生成了一个MapperAnnotationBuilder实例,
public MapperAnnotationBuilder(Configuration configuration, Class<?> type) {
String resource = type.getName().replace('.', '/') + ".java (best guess)";
this.assistant = new MapperBuilderAssistant(configuration, resource);
this.configuration = configuration;
this.type = type;
sqlAnnotationTypes.add(Select.class);
sqlAnnotationTypes.add(Insert.class);
sqlAnnotationTypes.add(Update.class);
sqlAnnotationTypes.add(Delete.class);
sqlProviderAnnotationTypes.add(SelectProvider.class);
sqlProviderAnnotationTypes.add(InsertProvider.class);
sqlProviderAnnotationTypes.add(UpdateProvider.class);
sqlProviderAnnotationTypes.add(DeleteProvider.class);
}
给sqlAnnotationTypes和sqlProviderAnnotationTypes进行了赋值。
下面看具体的解析过程,
parser.parse();
MapperAnnotationBuilder的parse方法如下,
public void parse() {
String resource = type.toString();
if (!configuration.isResourceLoaded(resource)) {//判断是否加载过该Mapper接口
//解析和接口同名的xml文件,前提是存在该文件,如果不存在该文件要怎么解析那?答案是解析接口中方法上的注解
/**
* 1、解析和接口同名的xml配置文件,最终要做的是把xml文件中的标签,转化为mapperStatement,
* 并放入mappedStatements中
*
*/
loadXmlResource();
configuration.addLoadedResource(resource);
assistant.setCurrentNamespace(type.getName());
//解析接口上的@CacheNamespace注解
parseCache();
parseCacheRef();
//2、获得接口中的所有方法,并解析方法上的注解
Method[] methods = type.getMethods();
for (Method method : methods) {
try {
// issue #237
if (!method.isBridge()) {
//解析方法上的注解
parseStatement(method);
}
} catch (IncompleteElementException e) {
configuration.addIncompleteMethod(new MethodResolver(this, method));
}
}
}
parsePendingMethods();
}
首先判断是否加载过该资源,
if (!configuration.isResourceLoaded(resource)) {
}
只有未加载过,才会执行该方法的逻辑,否则该方法执行完毕。
public boolean isResourceLoaded(String resource) {
return loadedResources.contains(resource);
}
从loadResources中进行判断,判断是否解析过该Mapper接口,答案是没有解析过,则会继续解析。
1.1、解析对应的XML文件
首先会解析XML文件,调用下面的方法,
//解析和接口同名的xml文件,前提是存在该文件,如果不存在该文件要怎么解析那?答案是解析接口中方法上的注解
/**
* 1、解析和接口同名的xml配置文件,最终要做的是把xml文件中的标签,转化为mapperStatement,
* 并放入mappedStatements中
*
*/
loadXmlResource();
看loadXmlResource方法
/**
* 解析mapper配置文件
*/
private void loadXmlResource() {
// Spring may not know the real resource name so we check a flag
// to prevent loading again a resource twice
// this flag is set at XMLMapperBuilder#bindMapperForNamespace
if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
//解析对应的XML映射文件,其名称为接口类+"."+xml,即和接口类同名且在同一个包下。
String xmlResource = type.getName().replace('.', '/') + ".xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
} catch (IOException e) {
// ignore, resource is not required
}
if (inputStream != null) {
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
//解析xml映射文件
xmlParser.parse();
}
}
}
首先进行了判断,进入if判断,看判断上的注解
// Spring may not know the real resource name so we check a flag
// to prevent loading again a resource twice
// this flag is set at XMLMapperBuilder#bindMapperForNamespace
第一句注解没理解什么意思,第二句的意思是方式两次加载资源,第三句是说明了该标识是在XMLMapperBuilder类中的bindMapperForNamespace中进行的设置,如下
为什么这样设置,后面会总结mapper的加载流程详细说明该问题。
判断之后寻找相应的XML映射文件,映射文件的文件路径如下,
//解析对应的XML映射文件,其名称为接口类+"."+xml,即和接口类同名且在同一个包下。
String xmlResource = type.getName().replace('.', '/') + ".xml";
从上面可以看出Mapper接口文件和XML映射文件在同一个包下,且文件名称相同(扩展名不同)。接着便是解析XML映射文件的逻辑。
if (inputStream != null) {
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
//解析xml映射文件
xmlParser.parse();
}
该逻辑和《mybatis源码配置文件解析之五:解析mappers标签(解析XML映射文件)》的过程是一样的,调用XMLMapperBuilder的parse方法进行解析,解析的结果为MapperStatement对象。
1.2、解析接口方法上的注解
上面是解析接口对应的XML映射文件,解析完成之后,还要解析接口方法上的注解,因为mybatis的sql配置有两种方式,一种是通过XML映射文件,另一种便是注解(当SQL比较复杂建议使用映射文件的方式),下面看解析注解的过程,
//2、获得接口中的所有方法,并解析方法上的注解
Method[] methods = type.getMethods();
for (Method method : methods) {
try {
// issue #237
if (!method.isBridge()) {
//解析方法上的注解
parseStatement(method);
}
} catch (IncompleteElementException e) {
configuration.addIncompleteMethod(new MethodResolver(this, method));
}
}
通过反射的方式获得接口中的所有方法,遍历方法执行parseStatement方法
void parseStatement(Method method) {
Class<?> parameterTypeClass = getParameterType(method);
LanguageDriver languageDriver = getLanguageDriver(method);
//获得方法上的注解,并生成SqlSource
SqlSource sqlSource = getSqlSourceFromAnnotations(method, parameterTypeClass, languageDriver);
if (sqlSource != null) {
Options options = method.getAnnotation(Options.class);
//生成mappedStatementId,为接口的权限类名+方法名。从这里可以得出同一个接口或namespace中不允许有同名的方法名或id
final String mappedStatementId = type.getName() + "." + method.getName();
Integer fetchSize = null;
Integer timeout = null;
StatementType statementType = StatementType.PREPARED;
ResultSetType resultSetType = ResultSetType.FORWARD_ONLY;
SqlCommandType sqlCommandType = getSqlCommandType(method);
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
boolean flushCache = !isSelect;
boolean useCache = isSelect;
KeyGenerator keyGenerator;
String keyProperty = "id";
String keyColumn = null;
if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
// first check for SelectKey annotation - that overrides everything else
SelectKey selectKey = method.getAnnotation(SelectKey.class);
if (selectKey != null) {
keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method), languageDriver);
keyProperty = selectKey.keyProperty();
} else if (options == null) {
keyGenerator = configuration.isUseGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
} else {
keyGenerator = options.useGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
keyProperty = options.keyProperty();
keyColumn = options.keyColumn();
}
} else {
keyGenerator = NoKeyGenerator.INSTANCE;
}
if (options != null) {
if (FlushCachePolicy.TRUE.equals(options.flushCache())) {
flushCache = true;
} else if (FlushCachePolicy.FALSE.equals(options.flushCache())) {
flushCache = false;
}
useCache = options.useCache();
fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE ? options.fetchSize() : null; //issue #348
timeout = options.timeout() > -1 ? options.timeout() : null;
statementType = options.statementType();
resultSetType = options.resultSetType();
}
String resultMapId = null;
ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class);
if (resultMapAnnotation != null) {
String[] resultMaps = resultMapAnnotation.value();
StringBuilder sb = new StringBuilder();
for (String resultMap : resultMaps) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(resultMap);
}
resultMapId = sb.toString();
} else if (isSelect) {
resultMapId = parseResultMap(method);
}
assistant.addMappedStatement(
mappedStatementId,
sqlSource,
statementType,
sqlCommandType,
fetchSize,
timeout,
// ParameterMapID
null,
parameterTypeClass,
resultMapId,
getReturnType(method),
resultSetType,
flushCache,
useCache,
// TODO gcode issue #577
false,
keyGenerator,
keyProperty,
keyColumn,
// DatabaseID
null,
languageDriver,
// ResultSets
options != null ? nullOrEmpty(options.resultSets()) : null);
}
}
注解的解析和解析XML映射文件的方式是一样的,解析的属性是一致的。需要注意下面的注解
@SelectProvider(type=BaseUserProvider.class,method="getUser")
该注解的意思是定义select语句的提供者,需要配置type和method,即提供类的Class对象和相应的方法(返回一个字符串)
3、解析失败回退
如果在继续过程中失败或抛出异常,则进行回退,回退的意思是从knownMappers中删除该类型。
finally {
if (!loadCompleted) {//3、如果解析失败,则删除knowMapper中的信息
knownMappers.remove(type);
}
}
因为Mapper解析的过程有两个结果一个是放入到configuration.knownMappers中的MapperProxyFactory对象,一个是放入到configuration.mappedStatements中MappedStatement对象,由于生产MappedStatement对象失败,所以要回退生成MapperProxyFactory对象过程。
小结
本章分析了mybatis解析
还没有评论,来说两句吧...