MyBatis整合Spring的实现(6)中分析了方法propertiesElement,下面继续往下分析代码:
java
1 方法typeAliasesElementsql
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); } } } } }
这里代码的主要实现就是MyBatis整合Spring的实现(4)中二、3分析的,若是忘记,能够回顾一下,再也不深刻讨论。下面附上XML配置文件。mybatis
1.1 MyBatis全局配置XML文件app
<typeAliases> <typeAlias alias="hashMap" type="java.util.HashMap"/> <typeAlias alias="arraylist" type="java.util.ArrayList"/> <package name="cn.vansky.bo.user"/> <package name="cn.vansky.bo.menu"/> </typeAliases>
2 方法pluginElementui
private void pluginElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { String interceptor = child.getStringAttribute("interceptor"); Properties properties = child.getChildrenAsProperties(); Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance(); interceptorInstance.setProperties(properties); configuration.addInterceptor(interceptorInstance); } } }
前面章节中就没有分析拦截器,主要是拦截器的做用是在执行相应的SQL时,才会发挥做用,这里只是对象的实例化,没有过多的进行分析,下面附上分页拦截器配置。spa
2.1 MyBatis全局配置XML文件.net
<!-- - - - - - - 分页拦截器- - - - - - - - - --> <plugins> <plugin interceptor="cn.vansky.framework.core.orm.mybatis.plugin.page.PaginationInterceptor"> <property name="dialectClass" value="cn.vansky.framework.core.orm.mybatis.plugin.page.dialect.MySQLDialect"/> <property name="sqlPattern" value=".*findPage*.*"/> </plugin> </plugins>
3 方法objectFactoryElement
code
private void objectFactoryElement(XNode context) throws Exception { if (context != null) { String type = context.getStringAttribute("type"); Properties properties = context.getChildrenAsProperties(); ObjectFactory factory = (ObjectFactory) resolveClass(type).newInstance(); factory.setProperties(properties); configuration.setObjectFactory(factory); } }
这里做者没有进行过配置,因此在网上找了个解释:MyBatis 每次建立结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成。默认的对象工厂须要作的仅仅是实例化目标类,要么经过默认构造方法,要么在参数映射存在的时候经过参数构造方法来实例化。默认状况下,咱们不须要配置,mybatis会调用默认实现的objectFactory。 除非咱们要自定义ObjectFactory的实现, 那么咱们才须要去手动配置。orm
3.1 MyBatis全局配置XML文件xml
<objectFactory type="org.mybatis.example.ExampleObjectFactory"> <property name="someProperty" value="100"/> </objectFactory>
4 方法objectWrapperFactoryElement
private void objectWrapperFactoryElement(XNode context) throws Exception { if (context != null) { String type = context.getStringAttribute("type"); ObjectWrapperFactory factory = (ObjectWrapperFactory) resolveClass(type).newInstance(); configuration.setObjectWrapperFactory(factory); } }
做者没有使用此属性,也没有进行深刻研究,因此这里不作讨论,有兴趣的能够,本身研究,知道的童鞋也能够在评论中回复做用。
总结:
这里为何要一会儿讲4个方法呢?主要是这4个方法的代码都不难,只是获取相应的对象放入Configuration(全局配置类)中,相信童鞋们本身看看代码就能懂了。