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.第三種方式
批量配置 實作類