1. 程式人生 > >Java框架學習_Mybatis(九)Mybatis整合Spring

Java框架學習_Mybatis(九)Mybatis整合Spring

所謂整合,就是將Mybatis中的類的建立交給Spring來管理(IOC),或者說是將Mybatis整個融入到Spring中,畢竟Mybatis只是後端的持久層框架,而Spring是全棧式框架。

官網教程:Mybatis-Spring整合


1、Mybatis整合Spring:

萬事先導包,這裡給出我的整個測試專案的原始碼:source code

這裡給出整合專案的基本架構:
在這裡插入圖片描述

Mybatis作為持久層框架,連線資料庫有兩種方法,第一種是正常配置,第二種是動態代理 ,正常配置就是按照Mybatis架構體系,來一步一步地配置,動態代理就是按照Mybatis的動態代理模式

,更加方便簡單(官網推薦動態代理)

既然是整合到Spring裡面,就必須遵循Spring的規矩,將Mybatis的類的建立全部交給Spring來管理(IOC)

Spring說Mybatis你那些配置太煩了,我給你提供幾個類,你把你的配置當作屬性交給我的這些類就行了

也就是下面的三個類:你會發現這三個類的屬性就是在Mybatis中配置的東西

在這裡插入圖片描述

而整合的關鍵,就是對這三個類的屬性進行配置的過程:applicationContext.xml

<?xml version="1.0" encoding="utf-8"?>
<beans
	xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 配置檔案放到properties=============================== --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置dbcp版連線池========================= --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></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"> <!-- 配置連線池, 之前都是寫在SqlMapConfig.xml裡面的=========== --> <property name="dataSource" ref="dataSource"></property> <!-- 載入Mybatis的核心配置檔案========================= --> <property name="configLocation" value="classpath:SqlMapConfig.xml"></property> <!-- 包掃描器,別名就是類的全稱,不區分大小寫,再寫pojo就不需要寫全路徑了,之前都是寫在SqlMapConfig.xml裡面的 --> <property name="typeAliasesPackage" value="cn.cupt.pojo"></property> </bean> <!-- 傳統的Dao方式:將userDaoImpl實現類交給spring管理 =================== --> <bean id="userDaoImpl" class="cn.cupt.mybatis.Dao.Impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 動態代理模式配置 ========================================== --> <!-- 第一種方式,配置一個介面 --> <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="oneMapper" parent="baseMapper"> <property name="mapperInterface" value="cn.cupt.mapper.UserMapper" /> </bean> <!-- 第二種方式,掃描整個包============================================= --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.cupt.mapper"></property> </bean> </beans>

配置好後你就可以去做一些測試,就像之前Spring通過對映找到相關的類然後呼叫方法一樣,這裡就是對上面三個類的方法的應用,主要還是動態代理的第二種:掃描包的用法,很簡單很關鍵(如果有多個包,用逗號隔開)
也就是說整合了之後Mybatis的核心包SqlMapConfig.xml完全可以刪掉,然後只要建立動態代理的Mapper包就行了,