Mybatis(六) Spring整合mybatis


      心莫浮躁~踏踏實實走,一步一個腳印,就算不學習,玩,能干嘛呢?人生就是那樣,要找點有意思,打發時間的事情來做,而鑽研技術,動腦動手的過程,還是比其他工作更有意思些~ so,努力啥的都是強迫自己做自以為努力正確的事情,想干嘛就干嘛把!    

                --WZY

一、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連接池

        mybatis和spring整合所需要的jar包

  

      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>
sqlMapConfig.xml

 

      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>
Student.xml

 

      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>
    
applicationContext.xml

  

      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.properties

 

      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> -->
View Code

 

      測試:

            

              

五、總結

      就這樣結束了,spring集成Mybatis,很簡單,有什么對象都由spring來創建即可。注意原始dao和mapper這兩種開發方式的不同。結果都市一樣的,方式不同而已。這個在第一節講Mybatis就已經講解過了,這里不在陳述,下一章節講解一下Mybatis的逆向工程

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM