MyBatis整合Spring的實現(7)

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>

方法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(全局配置類)中,相信童鞋們本身看看代碼就能懂了。

相關文章
相關標籤/搜索