Mybatis与SpringMVC整合 源码分析

       之前项目中使用过SSM框架,但一直没有拿出时间进行深入的研究。

    本文主要针对持久层框架Mybatis以及与SpringMVC的整合进行源码分析。

      1.整合需要的Jar

   Mybatis与Spring整合的主要jar包有两个:mybatis-3.4.0.jarmybatis-spring-1.3.0.jar

    Maven的pom.xml配置文件:      


		
			org.mybatis
			mybatis
			3.4.0
		
		
			org.mybatis
			mybatis-spring
			1.3.0
		 
      2.SpringMVC配置文件

    Mybatis与SpringMVC的整合是在spring-mybatis.xml配置文件中进行声明。配置文件结构如下:

  

     其中spring-mvc.xml是SpringMVC的配置文件。

     spring-mybatis.xml的配置内容如下:



		
	
	
    	
      
    
	
	  
          
     
	
	
	  
          
          
          
          
          
          
          
          
          
          
          
          
          
          
     
    
    
     

	
	
		
		
		
		
	

	
		
	 

	
	
		
		
	

	
	
		
	

	
	

	
	
		
			
			
			

			
			
			

		
	
	
	
		
		
	

    Mybatis与Spring整合部分是红色字体标注的内容。

    SqlSessionFactoryBean:datasource、configuration和*mapper.xml的配置信息都会注入到其中。

       部分源码:

   private Resource configLocation;
  private Configuration configuration;
  private Resource[] mapperLocations;
  private DataSource dataSource;
  private TransactionFactory transactionFactory;
  private Properties configurationProperties;
  private SqlSessionFactoryBuilder sqlSessionFactoryBuilder;
  private SqlSessionFactory sqlSessionFactory;
  private String environment;
  private boolean failFast;
  private Interceptor[] plugins;
  private TypeHandler[] typeHandlers;
  private String typeHandlersPackage;
  private Class[] typeAliases;
  private String typeAliasesPackage;
  private Class typeAliasesSuperType;
  private DatabaseIdProvider databaseIdProvider;
  private Class vfs;
  private Cache cache;
  private ObjectFactory objectFactory;
  private ObjectWrapperFactory objectWrapperFactory;
         SqlSessionTemplate:实现了SqlSession接口,是Mybatis与数据库进行交互的主要类。

    部分源码:

 public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory)
  {
    this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());
  }

  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType)
  {
    this(sqlSessionFactory, executorType, 
      new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration
      ().getEnvironment().getDataSource(), true));
  }

  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator)
  {
    Assert.notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
    Assert.notNull(executorType, "Property 'executorType' is required");

    this.sqlSessionFactory = sqlSessionFactory;
    this.executorType = executorType;
    this.exceptionTranslator = exceptionTranslator;
    this.sqlSessionProxy = ((DisposableBean)Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader
      (), new Class[] { DisposableBean.class }, new SqlSessionInterceptor(this, null)));
  }
       可以看出SqlSessionTempalte利用 SqlSessionFactory,也就是上文配置的SqlSessionFactoryBean,最终生成了 SqlSession的实例化对象 sqlSessionProxy,使用的生成方式是Java Proxy机制。

     MapperScannerConfigurer:用来自动扫描basePackage目录下的Mapper接口,将每个Mapper接口进行封装生成MapperFactoryBean的实例。
       至此,Mybatis与SpringMVC已经整合完成。



你可能感兴趣的:(Mybatis,Spring)