Spring - MyBatis
思路:
SqlSessionFactory -> SqlSession ->StudentMapper ->CRUD
可以发现 ,MyBatis最终是通过SqlSessionFactory来操作数据库,
Spring整合MyBatis 其实就是 将MyBatis的SqlSessionFactory 交给Spring
SM整合步骤:
1.jar
mybatis-spring.jar spring-tx.jar spring-jdbc.jar spring-expression.jar
spring-context-support.jar spring-core.jar spring-context.jar
spring-beans.jar spring-aop.jar spring-web.jar commons-logging.jar
commons-dbcp.jar ojdbc.jar(或mysql) mybatis.jar log4j.jar commons-pool.jar
2.类-表
3.MyBatis配置文件conf.xml
4.通过mapper.xml将 类、表建立映射关系
5.
之前使用MyBatis: conf.xml ->SqlSessionFacotry
现在整合的时候,需要通过Spring管理SqlSessionFacotry ,因此 产生qlSessionFacotry 所需要的数据库信息 不在放入conf.xml 而需要放入spring配置文件中
配置Spring配置文件(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 加载db.properties文件 -->
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>
<!-- 第一种方式生成mapper对象
<bean id="studentMapper" class="org.lanqiao.dao.impl.StudentDaoImpl">
将SPring配置的sqlSessionFactory 对象交给mapper(dao)
<property name="sqlSessionFactory" ref="sqlSessionFacotry"></property>
</bean>
-->
<!-- 第二种方式生成mapper对象
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.lanqiao.mapper.StudentMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFacotry"></property>
</bean>
-->
<!-- 第三种方式生成mapper对象(批量产生多个mapper)
批量产生Mapper对在SpringIOC中的 id值 默认就是 首字母小写接口名 (首字母小写的接口名=id值)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFacotry"></property>
<!--指定批量产生 哪个包中的mapper对象
-->
<property name="basePackage" value="org.lanqiao.mapper"></property>
</bean>
<bean id="studentService" class="org.lanqiao.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
<!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 在SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory -->
<bean id="sqlSessionFacotry" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis配置文件
<property name="configLocation" value="classpath:conf.xml"></property>
-->
<!-- 加载mapper.xml路径 -->
<property name="mapperLocations" value="org/lanqiao/mapper/*.xml"></property>
</bean>
</beans>
6.使用Spring-MyBatis整合产物开发程序
目标:通过spring产生mybatis最终操作需要的动态mapper对象(StudentMapper对象)
Spring产生动态mapper对象 有3种方法:
a.第一种方式
DAO层实现类,继承 SqlSessionDaoSupport类
SqlSessionDaoSupport类提供了一个属性 SqlSession
b.第二种方式
就是省略掉第一种方式的实现类
直接MyBatis提供的 Mapper实现类:org.mybatis.spring.mapper.MapperFactoryBean
缺点:每个mapper都需要一个配置一次
c.第三种方式
批量配置 实现类