MyBatis+Spring基于接口编程的原理分析

来源:互联网 发布:php artisan 编辑:程序博客网 时间:2024/05/14 08:32
  对于整合Spring及Mybatis不作详细介绍,可以参考: MyBatis 3 User Guide Simplified Chinese.pdf,贴出我的主要代码如下: 
UserMapper Interface: 
Java代码  收藏代码
  1. package org.denger.mapper;  
  2.   
  3. import org.apache.ibatis.annotations.Param;  
  4. import org.apache.ibatis.annotations.Select;  
  5. import org.denger.po.User;  
  6.   
  7. public interface UserMapper {  
  8.   
  9.     @Select("select * from tab_uc_account where id=#{userId}")  
  10.     User getUser(@Param("userId") Long userId);  
  11. }  


application-context.xml: 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  6.   
  7.   
  8.     <!-- Provided by annotation-based configuration  -->  
  9.     <context:annotation-config/>  
  10.   
  11.     <!--JDBC Transaction  Manage -->  
  12.     <bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">  
  13.         <constructor-arg>  
  14.             <ref bean="dataSource" />  
  15.         </constructor-arg>  
  16.     </bean>  
  17.   
  18.     <!-- The JDBC c3p0 dataSource bean-->  
  19.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  20.         <property name="driverClass" value="com.mysql.jdbc.Driver" />  
  21.         <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/noah" />  
  22.         <property name="user" value="root" />  
  23.         <property name="password" value="123456" />  
  24.     </bean>  
  25.   
  26.     <!--MyBatis integration with Spring as define sqlSessionFactory  -->  
  27.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  28.         <property name="dataSource" ref="dataSource" />  
  29.     </bean>  
  30.   
  31.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  32.         <property name="sqlSessionFactory"  ref="sqlSessionFactory"/>  
  33.         <property name="basePackage" value="org.denger.mapper"></property>  
  34.     </bean>  
  35. </beans>  

test Class: 
Java代码  收藏代码
  1. package org.denger.mapper;  
  2.   
  3. import org.junit.Assert;  
  4. import org.junit.Test;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.test.context.ContextConfiguration;  
  7. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;  
  8.   
  9. @ContextConfiguration(locations = { "/application-context.xml"})  
  10. public class UserMapperTest extends AbstractJUnit4SpringContextTests{  
  11.   
  12.     @Autowired  
  13.     public UserMapper userMapper;  
  14.       
  15.     @Test  
  16.     public void testGetUser(){  
  17.         Assert.assertNotNull(userMapper.getUser(300L));  
  18.     }  
  19. }  


实现原理分析 
     对于以上极其简单代码看上去并无特殊之处,主要亮点在于 UserMapper 居然不用实现类,而且在调用 getUser 的时候,也是使用直接调用了UserMapper实现类,那么Mybatis是如何去实现 UserMapper的接口的呢? 
    可能你马上能想到的实现机制就是通过动态代理方式,好吧,看看MyBatis整个的代理过程吧。 
    
    首先在Spring的配置文件中看到下面的Bean: 
   
Xml代码  收藏代码
  1. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  2.         <property name="sqlSessionFactory"  ref="sqlSessionFactory"/>  
  3.         <property name="basePackage" value="org.denger.mapper"></property>  
  4.     </bean>  

   以上的MapperScannerConfigurer class的注释中描述道: 
   从base 包中搜索所有下面所有 interface,并将其注册到 Spring Bean容器中,其注册的class bean是MapperFactoryBean。 
   好吧,看看它的注册过程,下面方法来从 MapperScannerConfigurer中的Scanner类中抽取,下面方法在初始化以上application-content.xml文件时就会进行调用。 主要用于是搜索 base packages 下的所有mapper class,并将其注册至 spring 的 benfinitionHolder中。 
  
Java代码  收藏代码
  1. /** 
  2. * Calls the parent search that will search and register all the candidates. Then the 
  3. * registered objects are post processed to set them as MapperFactoryBeans 
  4. */  
  5. @Override  
  6. protected Set<BeanDefinitionHolder> doScan(String... basePackages) {  
  7.         //#1  
  8.     Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);  
  9.     if (beanDefinitions.isEmpty()) {  
  10.             logger.warn("No MyBatis mapper was found in '" + MapperScannerConfigurer.this.basePackage  
  11.                         + "' package. Please check your configuration.");  
  12.     } else {  
  13.          //#2  
  14.          for (BeanDefinitionHolder holder : beanDefinitions) {  
  15.                 GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();  
  16.             if (logger.isDebugEnabled()) {  
  17.                 logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '"+ definition.getBeanClassName() + "' mapperInterface");  
  18.             }  
  19.            //#3  
  20.           definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName());  
  21.           definition.setBeanClass(MapperFactoryBean.class);  
  22.      }  
  23.     return beanDefinitions;  
  24. }  
    #1: 了解Spring初始化Bean过程的人可能都知道,Spring 首先会将需要初始化的所有class先通过BeanDefinitionRegistry进行注册,并且将该Bean的一些属性信息(如scope、className、beanName等)保存至BeanDefinitionHolder中;Mybatis这里首先会调用Spring中的ClassPathBeanDefinitionScanner.doScan方法,将所有Mapper接口的class注册至BeanDefinitionHolder实例中,然后返回一个Set<BeanDefinitionHolder>,其它包含了所有搜索到的Mapper class BeanDefinitionHolder对象。 

    #2: 首先,由于 #1:  中注册的都是接口class, 可以肯定的是接口是不能直接初始化的;实际 #2: 中for循环中替换当前所有 holder的 className为 MapperFactoryBean.class,并且将 mapper interface的class name setter 至 MapperFactoryBean 属性为 mapperInterface 中,也就是 #3: 代码所看到的。 
  再看看 MapperFactoryBean,它是直接实现了 Spring 的 FactoryBean及InitializingBean 接口。其实既使不看这两个接口,当看MapperFactoryBean的classname就知道它是一个专门用于创建 Mapper 实例Bean的工厂。 

  至此,已经完成了Spring与mybatis的整合的初始化配置的过程。 

    接着,当我在以上的 Test类中,需要注入 UserMapper接口实例时,由于mybatis给所有的Mapper 实例注册都是一个MapperFactory的工厂,所以产生UserMapper实现仍需要 MapperFactoryBean来进行创建。接下来看看 MapperFactoryBean的处理过程。 
    先需要创建Mapper实例时,首先在 MapperFactoryBean中执行的方法是: 
 
Java代码  收藏代码
  1. /** 
  2.      * {@inheritDoc} 
  3.      */  
  4.     public void afterPropertiesSet() throws Exception {  
  5.         Assert.notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");  
  6.         Assert.notNull(this.mapperInterface, "Property 'mapperInterface' is required");  
  7.   
  8.         Configuration configuration = this.sqlSession.getConfiguration();  
  9.         if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {  
  10.             configuration.addMapper(this.mapperInterface);  
  11.         }  
  12.     }  
    上面方法中先会检测当前需要创建的 mapperInterface在全局的 Configuration中是否存在,如果不存在则添加。呆会再说他为什么要将 mapperInterface 添加至 Configuration中。该方法调用完成之后,就开始调用 getObject方法来产生mapper实例,看到MapperFactoryBean.getObject的该方法代码如下: 
  
Java代码  收藏代码
  1. /** 
  2.     * {@inheritDoc} 
  3.     */  
  4.    public T getObject() throws Exception {  
  5.        return this.sqlSession.getMapper(this.mapperInterface);  
  6.    }  
    通过Debug可以看到 sqlSession及mapperInterface对象: 
 
    到目前为止我们的 UserMapper实例实际上还并未产生; 再进入org.mybatis.spring.SqlSessionTemplate中的getMapper方法,该方法将 this及mapper interface class 作为参数传入 org.apache.ibatis.session.Configuration的getMapper() 方法中,代码如下: 
     
Java代码  收藏代码
  1. /** 
  2.      * {@inheritDoc} 
  3.      */  
  4.     public <T> T getMapper(Class<T> type) {  
  5.         return getConfiguration().getMapper(type, this);  
  6.     }  

   于是,再进入 org.apache.ibatis.session.Configuration.getMapper中代码如下: 
   
Java代码  收藏代码
  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {  
  2.     return mapperRegistry.getMapper(type, sqlSession);  
  3.   }  
    其中 mapperRegistry保存着当前所有的 mapperInterface class. 那么它在什么时候将 mapperinterface class 保存进入的呢?其实就是在上面的 afterPropertiesSet 中通过 configuration.addMapper(this.mapperInterface) 添加进入的。 
    再进入 org.apache.ibatis.binding.MapperRegistry.getMapper方法,代码如下:
Java代码  收藏代码
  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {  
  2.   //首先判断当前knownMappers是否存在mapper interface class.因为前面说到 afterPropertiesSet 中已经将当前的 mapperinterfaceclass 添加进入了。  
  3.     if (!knownMappers.contains(type))  
  4.       throw new BindingException("Type " + type + " is not known to the MapperRegistry.");  
  5.     try {  
  6.       return MapperProxy.newMapperProxy(type, sqlSession);  
  7.     } catch (Exception e) {  
  8.       throw new BindingException("Error getting mapper instance. Cause: " + e, e);  
  9.     }  
  10.   }  

    嗯,没错,看到 MapperProxy.newMapperProxy后可以肯定的是它确实采用的代理模式,再进入一看究竟吧: 
  
Java代码  收藏代码
  1. public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {  
  2.     ClassLoader classLoader = mapperInterface.getClassLoader();  
  3.     Class[] interfaces = new Class[]{mapperInterface};  
  4.     MapperProxy proxy = new MapperProxy(sqlSession);  
  5.     return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);  
  6.   }  

    
    JDK的动态代理就不说了,至此,基本Mybatis已经完成了Mapper实例的整个创建过程,也就是你在具体使用 UserMapper.getUser 时,它实际上调用的是 MapperProxy,因为此时 所返回的 MapperProxy是实现了 UserMapper接口的。只不过 MapperProxy拦截了所有对userMapper中方法的调用,如下: 
 
Java代码  收藏代码
  1. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  2.    try {  
  3.     //如果调用不是 object 中默认的方法(如equals之类的)  
  4.      if (!OBJECT_METHODS.contains(method.getName())) {  
  5.        //因为当前MapperProxy代理了所有 Mapper,所以他需要根据当前拦截到的方法及代理对象获取 MapperInterface  class,也就是我这里的 UserMapper.class  
  6.        final Class declaringInterface = findDeclaringInterface(proxy, method);  
  7.        //然后再根据UserMapper.class、及当前调用的Method(也就是getUser)及SqlSession构造一个 MapperMethod,在这里面会获取到 getUser方法上的 @Select() 的SQL,然后再通过 sqlSession来进行执行  
  8.        final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);  
  9.        //execute执行数据库操作,并返回结果集  
  10.        final Object result = mapperMethod.execute(args);  
  11.        if (result == null && method.getReturnType().isPrimitive()) {  
  12.          throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");  
  13.        }  
  14.        return result;  
  15.      }  
  16.    } catch (SQLException e) {  
  17.      e.printStackTrace();  
  18.    }  
  19.    return null;  
  20.  }  

     为什么说它拦截了呢?可以看到, 它并没有调用 method.invoke(object)方法,因为实际上 MapperProxy只是动态的 implement 了UserMapper接口,但它没有真正实现 UserMapper中的任何方法。至于结果的返回,它也是通过 MapperMethod.execute 中进行数据库操作来返回结果的。 说白了,就是一个实现了 MapperInterface 的 MapperProxy 实例被MapperProxy代理了,可以debug看看 userMapper实例就是 MapperProxy。


原文:http://denger.iteye.com/blog/1060588