spring和mybatis整合

记录,帮助梳理一下.

二者的整合,其实实质就是将mybatis用到的组件交给spring来管理.

1. 与mybatis相关.

与mybatis相关的主要有:

  1. 数据库连接.这里面与数据库密码,用户名等有关.
  2. 会话工厂,需要用到数据库连接,基于mybatis的配置,mapper扫描路径等.
  3. mapper代理相关.负责从mapper包中扫描接口,并自动创建代理对象,在spring容器中注册.

下面是一个典型的applicationContext.xml文件.

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="          
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd          
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd          
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd          
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
      http://www.springframework.org/schema/task  
      http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
    
     
      
    "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
    package="com"/>  

    "dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        "driverClassName" value="com.mysql.jdbc.Driver" />
        "url" value="jdbc:mysql://xxx.xxx.xxx.xxx:3306/xx" />
        "username" value="xxx" />
        "password" value="xxxx" />
    
    
    "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        "dataSource" ref="dataSource" />
        "mapperLocations" value="classpath:com/xxx/email/test/*.xml"> 
                
        "configLocation" value="classpath:mybatis-config.xml" />
    

    
    "org.mybatis.spring.mapper.MapperScannerConfigurer">
        "basePackage" value="com.xx.email.test" />
        "sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    

 

可以看到,spring负责了数据源的配置,会话工厂的配置,mapper的扫描以及代理类的实例化;

2. spring相关

搭建spring框架就是导入包就可以.再就是获取bean的时候,需要显式地引入applicationContext.xml文件(换个文件名也可以)

你可能感兴趣的:(-----------,【排雷日志】,●,Web基础)