Mybatis學習(六)————— Spring整合mybatis
一、Spring整合mybatis思路
非常簡單,這裡先回顧一下mybatis最基礎的根基,
mybatis,有兩個配置檔案
全域性配置檔案SqlMapConfig.xml(配置資料來源,全域性變數,載入對映檔案等東西)
對映檔案xxxMapper.xml,用來對輸入引數輸出引數,資料庫語句做配置的。
mybatis配置好之後的使用步驟
1、獲取sqlMapConfig.xml的位置然後進行載入
2、通過sqlMapConfig.xml中的內容創建出sqlsessionFactory物件
3、然後通過sqlsessionFactory物件創建出sqlsession物件
4、有了sqlsession物件就可以進行相應的操作了。
整合思路
有了上面的一些知識回顧,那麼就有思路讓spring繼承mabatis了。
1、讓spring來管理資料來源資訊,sqlMapConfig.xml中就不需要載入資料來源了。交給spring管理
2、讓spring通過單例方式管理SqlSessionFactory,只需要一個SqlSessionFactory幫我們生成sqlsession即可。也就是需要sqlsession物件就讓sqlsessionfactory生成。所以是單例方式。
3、讓spring建立sqlsession bean。也就是通過SqlSessionFactory建立SqlSession,
4、如果是使用mapper代理開發的方式,那麼持久層的mapper都需要由spring進行管理,spring和mybatis整合生成mapper代理物件。
二、工程搭建
2.1、建立java工程
2.2、新增jar包
Mybatis的核心和依賴包
資料庫驅動包
spring的包
junit包
spring和mybatis整合包
dbcp連線池
2.3、
2.4、新增SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 取別名,或者別的其他全域性配置資訊,就在這裡編寫 --> <typeAliases> <!-- 別名為類名首字母大小寫都可以 --> <package name="com.wuhao.ms.domain"/> </typeAliases> <!-- 載入mapper對映檔案 --> <mappers> <mapper resource="classpath:Student.xml"/> </mappers> </configuration>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 取別名,或者別的其他全域性配置資訊,就在這裡編寫 --> <typeAliases> <!-- 別名為類名首字母大小寫都可以 --> <package name="com.wuhao.ms.domain"/> </typeAliases> <!-- 載入mapper對映檔案 --> <mappers> <mapper resource="classpath:Student.xml"/> </mappers> </configuration>
2.5、對映配置檔案Student.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace:名稱空間,對sql進行一個分類管理 --> <!-- 注意:namespace在mapper代理時,具有重要且特殊的作用 對應mapper介面的全限定名 --> <mapper namespace="test"> <select id="findStudentById" parameterType="int" resultType="com.wuhao.ms.domain.Student"> SELECT * FROM student WHERE sid = #{id} </select> </mapper>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace:名稱空間,對sql進行一個分類管理 --> <!-- 注意:namespace在mapper代理時,具有重要且特殊的作用 對應mapper介面的全限定名 --> <mapper namespace="test"> <select id="findStudentById" parameterType="int" resultType="com.wuhao.ms.domain.Student"> SELECT * FROM student WHERE sid = #{id} </select> </mapper>
2.6、整合spring的配置檔案applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 引用java配置檔案 --> <context:property-placeholder location="db.properties" /> <!-- 配置資料來源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis的全域性配置檔案的路徑 --> <property name="configLocation" value="SqlMapConfig.xml"></property> <!-- 資料來源 --> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 引用java配置檔案 --> <context:property-placeholder location="db.properties" /> <!-- 配置資料來源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis的全域性配置檔案的路徑 --> <property name="configLocation" value="SqlMapConfig.xml"></property> <!-- 資料來源 --> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
2.7、db.properties
db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 db.username=root db.password=root
db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 db.username=root db.password=root
2.8、總觀,就編寫了四個配置檔案,和一個javabean
三、開發原始dao
上面是一個通用的配置,我們在使用mybatis時,有兩個模式,一種就是原始dao的方式,一種就是使用mapper代理的方式,這裡就介紹原始dao是如何整合spring的
StudengDao:介面
StudentDaoImpl:實現類
使用原始dao開發的缺點就是隻能通過selectOne或者selectList等操作,而不是直接呼叫對映配置檔案中的方法,不能一目瞭然。
spring中配置StudentDao
很多人這裡會有疑問,覺得在spring中配置了StudengDao這個bean,但是我們根本沒用在StudengDaoImpl中用set方法讓其自動注入呀?原因就在StudengDaoImpl中繼承了sqlSessionDaoSupport這個類,這個類幫我們做了,所以,我們直接通過this.getSqlSession()獲取物件即可。
測試:
四、mapper方式開發
編寫mapper介面,StudentMapper.java
編寫mapper對映檔案 StudengMapper.xml,注意兩個要放在一起。名稱要相同
spring中生成mapper代理物件
<!-- spring建立mapper代理兩種方式 --> <!-- 第一種,單個配置,一個mapper就一個配置 --> <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- 指定要對映的mapper介面的全限定名 --> <property name="mapperInterface" value="com.wuhao.ms.mapper.StudentMapper"></property> <!-- 指定sqlSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 批量配置,這裡是批量配置mapper代理,那麼下面就不用配置id了。 我們想要獲取哪個mapper代理用這個格式:類名首字母小寫 --> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wuhao.ms.mapper"></property> </bean> -->
<!-- spring建立mapper代理兩種方式 --> <!-- 第一種,單個配置,一個mapper就一個配置 --> <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- 指定要對映的mapper介面的全限定名 --> <property name="mapperInterface" value="com.wuhao.ms.mapper.StudentMapper"></property> <!-- 指定sqlSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 批量配置,這裡是批量配置mapper代理,那麼下面就不用配置id了。 我們想要獲取哪個mapper代理用這個格式:類名首字母小寫 --> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wuhao.ms.mapper"></property> </bean> -->
測試:
五、總結
就這樣結束了,spring整合Mybatis,很簡單,有什麼物件都由spring來建立即可。注意原始dao和mapper這兩種開發方式的不同。結果都市一樣的,方式不同而已。這個在第一節講Mybatis就已經講解過了,這裡不在陳述,下一章節講解一下Mybatis的逆向工程