第一种就是作者之前写过的,就是通过编写是实现类然后在实现类里面实现接口的方法,然后在applicationcontex.xml文件中注册创建一个该实现类的bean对象,然后在该对象中注入SQLSession的以来即可
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <!-- 读取数据库的相关配置文件 --> <context:property-placeholder location="classpath:config/jdbc.properties"/> <!-- 创建数据源DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClass}"></property> <property name="username" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <!-- 最大连接数 --> <property name="maxActive" value="10"></property> <!-- 最大空闲数 --> <property name="maxIdle" value="5"></property> </bean> <!-- 创建一个SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 关联连接池 --> <property name="dataSource" ref="dataSource"></property> <!--加载sql映射文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 创建一个CustomerMapperImpl对象,然后向里面注入SQLSessionFactory的对象 --> <bean id="customerMapper" class="cn.ssm1234.dao.impl.CustomerMapperimpl"> <!-- 关联或者输入sqlSeesionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
第二种简化了操作实现类的操作,该方法中,我们直接注册创建mapper的接口的一个bean对象,然后向里面注入SQLSession的以来即可
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <!-- 读取数据库的相关配置文件 --> <context:property-placeholder location="classpath:config/jdbc.properties"/> <!-- 创建数据源DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClass}"></property> <property name="username" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <!-- 最大连接数 --> <property name="maxActive" value="10"></property> <!-- 最大空闲数 --> <property name="maxIdle" value="5"></property> </bean> <!-- 创建一个SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 关联连接池 --> <property name="dataSource" ref="dataSource"></property> <!--加载sql映射文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 配置Mapper接口 --> <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- 关联Mapper接口 --> <property name="mapperInterface" value="cn.ssm1234.dao.CustomerMapper"></property> <!-- 关联SqlSessionfactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
第三种就是最简便,这里我们可以直接创建一个mapper接口的扫描器,这样我们只要定义扫描器所扫描的包,那样我们就可以一次性全部扫描出我们要用马屁拍二接口,而不需要没创建一个mapper接口就去创建该mapper接口的对象
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <!-- 读取数据库的相关配置文件 --> <context:property-placeholder location="classpath:config/jdbc.properties"/> <!-- 创建数据源DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClass}"></property> <property name="username" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <!-- 最大连接数 --> <property name="maxActive" value="10"></property> <!-- 最大空闲数 --> <property name="maxIdle" value="5"></property> </bean> <!-- 创建一个SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 关联连接池 --> <property name="dataSource" ref="dataSource"></property> <!--加载sql映射文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- Mapper接口的扫描器 --> <!--如果使用Mapper接口包扫描,那每个Mapper接口在spring容器中的id名称为类名:例如CustomerMapper就叫做customerMapper(类名首字母小写) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 配置Mapper接口包所在的路径 --> <property name="basePackage" value="cn.ssm1234.dao"></property> </bean> <!-- 开启Spring的IOC注解扫描 --> <context:component-scan base-package="cn.ssm1234"></context:component-scan> </beans>
这里我们总结一下,在mybatis与Spring的整合过程中,必须的两步就是
1.连接数据库的相关操作
2.配置SQLSession对象
其次就是上述的三种整合方案选择。