Mybatis源码学习笔记(六)配置简介之objectFactory、plugins、mappers

来源:互联网 发布:java工程师职业路线 编辑:程序博客网 时间:2024/05/16 13:05

配置简介之objectFactory、plugins、mappers

1、objectFactory的作用:

MyBatis 每次创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成。默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认构造方法,要么在参数映射存在的时候通过参数构造方法来实例化。

默认情况下,我们不需要配置,mybatis会调用默认实现的objectFactory。 除非我们要自定义ObjectFactory的实现, 那么我们才需要去手动配置。

1.1)自定义objectFactory:

自定义ObjectFactory只需要去继承DefaultObjectFactory(是ObjectFactory接口的实现类),并重写其方法即可。

1.2)配置objectFactory:

<configuration>    ......    <objectFactory type="org.mybatis.example.ExampleObjectFactory">        <property name="someProperty" value="100"/>    </objectFactory>    ......  </configuration

2、plugin:

plugins 是一个可选配置。

mybatis中的plugin其实就是个interceptor, 它可以拦截Executor 、ParameterHandler 、ResultSetHandler 、StatementHandler 的部分方法,处理我们自己的逻辑。

Executor就是真正执行sql语句的东西,

ParameterHandler 是处理我们传入参数的,还记得前面讲TypeHandler的时候提到过,mybatis默认帮我们实现了不少的typeHandler, 当我们不显示配置typeHandler的时候,mybatis会根据参数类型自动选择合适的typeHandler执行,其实就是ParameterHandler 在选择。

ResultSetHandler 就是处理返回结果的。

2.1)自定义一个plugin

需要去实现Interceptor接口

2.2)配置plugin

<configuration>    ......    <plugins>      <plugin interceptor="org.mybatis.example.ExamplePlugin">        <property name="someProperty" value="100"/>      </plugin>    </plugins>    ......  </configuration>

3、mappers

mappers 节点下,配置我们的mapper映射文件, 所谓的mapper映射文件,就是让mybatis 用来建立数据表和javabean映射的一个桥梁。在我们实际开发中,通常一个mapper文件对应一个dao接口, 这个mapper可以看做是dao的实现。所以,mappers必须配置。

<configuration>    ......    <mappers>      <!-- 第一种方式:通过resource指定 -->    <mapper resource="xml/userDao-mapping.xml"/>     <!-- 第二种方式, 通过class指定接口,进而将接口与对应的xml文件形成映射关系             不过,使用这种方式必须保证 接口与mapper文件同名(不区分大小写),              我这儿接口是UserDao,那么意味着mapper文件为UserDao.xml      <mapper class="com.example.demo.dao.UserDao"/>      -->      <!-- 第三种方式,直接指定包,自动扫描,与方法二同理       <package name="com.example.demo.dao"/>      -->      <!-- 第四种方式:通过url指定mapper文件位置      <mapper url="file://........"/>       -->  </mappers>    ......  </configuration>

源码解析:

//解析objectFactory节点方法private void objectFactoryElement(XNode context) throws Exception {    if (context != null) {      //读取type属性的值, 接下来进行实例化ObjectFactory, 并set进 configuration      String type = context.getStringAttribute("type");      //读取propertie的值, 根据需要可以配置, mybatis默认实现的objectFactory没有使用properties      Properties properties = context.getChildrenAsProperties();      ObjectFactory factory = (ObjectFactory) resolveClass(type).newInstance();      factory.setProperties(properties);      //configuration对象存储的就是mybatis配置文件的配置信息      configuration.setObjectFactory(factory);    }  }
//解析plugin节点private void pluginElement(XNode parent) throws Exception {    if (parent != null) {      for (XNode child : parent.getChildren()) {      //获取plugin节点中的interceptor属性        String interceptor = child.getStringAttribute("interceptor");        Properties properties = child.getChildrenAsProperties();        //在定义一个interceptor的时候,需要去实现Interceptor        Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();        interceptorInstance.setProperties(properties);        configuration.addInterceptor(interceptorInstance);      }    }  }
//mybatis核心 解析mappers节点private void mapperElement(XNode parent) throws Exception {    if (parent != null) {      for (XNode child : parent.getChildren()) {        if ("package".equals(child.getName())) {        //如果mappers节点的子节点是package, 那么就扫描package下的文件, 注入进configuration          String mapperPackage = child.getStringAttribute("name");          configuration.addMappers(mapperPackage);        } else {         //resource, url, class 这三种只能配置其中之一          String resource = child.getStringAttribute("resource");          String url = child.getStringAttribute("url");          String mapperClass = child.getStringAttribute("class");          //下面就是根据 resource url class 这三种配置做出的不同的读取方式          //通过resource指定          if (resource != null && url == null && mapperClass == null) {            ErrorContext.instance().resource(resource);            InputStream inputStream = Resources.getResourceAsStream(resource);            //mapper映射文件都是通过XMLMapperBuilder解析            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());            //解析mapper的映射文件            mapperParser.parse();          } else if (resource == null && url != null & mapperClass == null) {          // 通过url指定            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指定            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.");          }        }      }    }  }
阅读全文
0 0
原创粉丝点击