心莫浮躁~踏踏實實走,一步一個腳印,就算不學習,玩,能幹嗎呢?人生就是那樣,要找點有意思,打發時間的事情來作,而鑽研技術,動腦動手的過程,仍是比其餘工做更有意思些~ so,努力啥的都是強迫本身作自覺得努力正確的事情,想幹嗎就幹嗎把! java
--WZYmysql
1、Spring整合mybatis思路 spring
很是簡單,這裏先回顧一下mybatis最基礎的根基,sql
mybatis,有兩個配置文件數據庫
全局配置文件SqlMapConfig.xml(配置數據源,全局變量,加載映射文件等東西)apache
映射文件xxxMapper.xml,用來對輸入參數輸出參數,數據庫語句作配置的。spring-mvc
mybatis配置好以後的使用步驟session
一、獲取sqlMapConfig.xml的位置而後進行加載mybatis
二、經過sqlMapConfig.xml中的內容建立出sqlsessionFactory對象mvc
三、而後經過sqlsessionFactory對象建立出sqlsession對象
四、有了sqlsession對象就能夠進行相應的操做了。
集成思路
有了上面的一些知識回顧,那麼就有思路讓spring繼承mabatis了。
一、讓spring來管理數據源信息,sqlMapConfig.xml中就不須要加載數據源了。交給spring管理
二、讓spring經過單例方式管理SqlSessionFactory,只須要一個SqlSessionFactory幫咱們生成sqlsession便可。也就是須要sqlsession對象就讓sqlsessionfactory生成。因此是單例方式。
三、讓spring建立sqlsession bean。也就是經過SqlSessionFactory建立SqlSession,
四、若是是使用mapper代理開發的方式,那麼持久層的mapper都須要由spring進行管理,spring和mybatis整合生成mapper代理對象。
2、工程搭建
2.一、建立java工程
2.二、添加jar包
Mybatis的核心和依賴包
數據庫驅動包
spring的包
junit包
spring和mybatis整合包
dbcp鏈接池
2.三、
2.四、添加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>
2.五、映射配置文件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>
2.六、整合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>
2.七、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
2.八、總觀,就編寫了四個配置文件,和一個javabean
3、開發原始dao
上面是一個通用的配置,咱們在使用mybatis時,有兩個模式,一種就是原始dao的方式,一種就是使用mapper代理的方式,這裏就介紹原始dao是如何集成spring的
StudengDao:接口
StudentDaoImpl:實現類
使用原始dao開發的缺點就是隻能經過selectOne或者selectList等操做,而不是直接調用映射配置文件中的方法,不能一目瞭然。
spring中配置StudentDao
不少人這裏會有疑問,以爲在spring中配置了StudengDao這個bean,可是咱們根本沒用在StudengDaoImpl中用set方法讓其自動注入呀?緣由就在StudengDaoImpl中繼承了sqlSessionDaoSupport這個類,這個類幫咱們作了,因此,咱們直接經過this.getSqlSession()獲取對象便可。
測試:
4、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> -->
測試:
5、總結
就這樣結束了,spring集成Mybatis,很簡單,有什麼對象都由spring來建立便可。注意原始dao和mapper這兩種開發方式的不一樣。結果都市同樣的,方式不一樣而已。這個在第一節講Mybatis就已經講解過了,這裏不在陳述,下一章節講解一下Mybatis的逆向工程