Spring和Mybatis整合的步骤

             

1.先配置Spring的XML文件:applicatoinContext.xml
    在配置文件里面配置好连接池这里用的是C3P0需要导jar包  (注:Spring+Mybatis整合包也需要导入)
            C3P0jar包:

Spring和Mybatis整合的步骤

C3P0 Bean配置:

Spring和Mybatis整合的步骤

另一种方法配置C3P0 使用配置文件: (教案里使用的DBCP 把CLASS改成c3P0的就可以了参照上图)

Spring和Mybatis整合的步骤

Spring和Mybatis整合的步骤


2.创建Mybatis的SqlSessionFactory 工厂 的Bean 里面加载核心配置文件SqlMapConfig.xml和数据源dataSource

Spring和Mybatis整合的步骤

3.使用Mapper动态代理开发 (此方法只适用于单个Mapper或者不多的情况不然要一直创建很麻烦下面会有解决方案)

Spring和Mybatis整合的步骤

 3.1此方法为Mapper批量注入Bean扫描:

Spring和Mybatis整合的步骤


注:还是要遵守Mybatis的Mapper代理开发规则Spring和Mybatis整合的步骤  xml配置文件和Java文件的文件名一致。

 4.Mybatis全局配置里必须要连接上Mapper.xml的地址:

Spring和Mybatis整合的步骤

5.Mapper.xml    里nameSpace为相同路径下相同名的接口地址 :

Spring和Mybatis整合的步骤

6.测试代码:

Spring和Mybatis整合的步骤

ApplicatoinContext加载Spring配置文件 getBean为Bean ID 如果是批量配置的 名称为类名小写