Mybatis整合Spring,mybatisspring

來源:互聯網
上載者:User

Mybatis整合Spring,mybatisspring

根據官方的說法,在ibatis3,也就是Mybatis3問世之前,Spring3的開發工作就已經完成了,所以Spring3中還是沒有對Mybatis3的支援。因此由Mybatis社區自己開發了一個Mybatis-Spring用來滿足Mybatis使用者整合Spring的需求。下面就將通過Mybatis-Spring來整合Mybatis跟Spring的用法做一個簡單的介紹。MapperFactoryBean       首先,我們需要從Mybatis官網上下載Mybatis-Spring的jar包添加到我們項目的類路徑下,當然也需要添加Mybatis的相關jar包和Spring的相關jar包。我們知道在Mybatis的所有操作都是基於一個SqlSession的,而SqlSession是由SqlSessionFactory來產生的,SqlSessionFactory又是由SqlSessionFactoryBuilder來產生的。但是Mybatis-Spring是基於SqlSessionFactoryBean的。在使用Mybatis-Spring的時候,我們也需要SqlSession,而且這個SqlSession是內嵌在程式中的,一般不需要我們直接存取。SqlSession也是由SqlSessionFactory來產生的,但是Mybatis-Spring給我們封裝了一個SqlSessionFactoryBean,在這個bean裡面還是通過SqlSessionFactoryBuilder來建立對應的SqlSessionFactory,進而擷取到對應的SqlSession。通過SqlSessionFactoryBean我們可以通過對其指定一些屬性來提供Mybatis的一些配置資訊。所以接下來我們需要在Spring的applicationContext設定檔中定義一個SqlSessionFactoryBean。 Xml代碼 複製代碼 收藏代碼1.<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  2.       <property name="dataSource" ref="dataSource" />  3.       <property name="mapperLocations"  4.              value="classpath:com/tiantian/ckeditor/mybatis/mappers/*Mapper.xml" />  5.       <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model" />  6.</bean>          在定義SqlSessionFactoryBean的時候,dataSource屬性是必須指定的,它表示用於串連資料庫的資料來源。當然,我們也可以指定一些其他的屬性,下面簡單列舉幾個:• mapperLocations:它表示我們的Mapper檔案存放的位置,當我們的Mapper檔案跟對應的Mapper介面處於同一位置的時候可以不用指定該屬性的值。•configLocation:用於指定Mybatis的設定檔位置。如果指定了該屬性,那麼會以該設定檔的內容作為配置資訊構建對應的SqlSessionFactoryBuilder,但是後續屬性指定的內容會覆蓋該設定檔裡面指定的對應內容。• typeAliasesPackage:它一般對應我們的實體類所在的包,這個時候會自動取對應包中不包括包名的簡單類名作為包括包名的別名。多個package之間可以用逗號或者分號等來進行分隔。• typeAliases:數群組類型,用來指定別名的。指定了這個屬性後,Mybatis會把這個類型的簡短名稱作為這個類型的別名,前提是該類上沒有標註@Alias註解,否則將使用該註解對應的值作為此種類型的別名。Xml代碼 複製代碼 收藏代碼1.<property name="typeAliases">  2.    <array>  3.        <value>com.tiantian.mybatis.model.Blog</value>  4.        <value>com.tiantian.mybatis.model.Comment</value>  5.    </array>  6.</property>   • plugins:數群組類型,用來指定Mybatis的Interceptor。• typeHandlersPackage:用來指定TypeHandler所在的包,如果指定了該屬性,SqlSessionFactoryBean會自動把該包下面的類註冊為對應的TypeHandler。多個package之間可以用逗號或者分號等來進行分隔。• typeHandlers:數群組類型,表示TypeHandler。        接下來就是在Spring的applicationContext檔案中定義我們想要的Mapper對象對應的MapperFactoryBean了。通過MapperFactoryBean可以擷取到我們想要的Mapper對象。MapperFactoryBean實現了Spring的FactoryBean介面,所以MapperFactoryBean是通過FactoryBean介面中定義的getObject方法來擷取對應的Mapper對象的。在定義一個MapperFactoryBean的時候有兩個屬性需要我們注入,一個是Mybatis-Spring用來產生實現了SqlSession介面的SqlSessionTemplate對象的sqlSessionFactory;另一個就是我們所要返回的對應的Mapper介面了。       定義好相應Mapper介面對應的MapperFactoryBean之後,我們就可以把我們對應的Mapper介面注入到由Spring管理的bean對象中了,比如Service bean對象。這樣當我們需要使用到相應的Mapper介面時,MapperFactoryBean會從它的getObject方法中擷取對應的Mapper介面,而getObject內部還是通過我們注入的屬性調用SqlSession介面的getMapper(Mapper介面)方法來返回對應的Mapper介面的。這樣就通過把SqlSessionFactory和相應的Mapper介面交給Spring管理實現了Mybatis跟Spring的整合。Spring的applicationContext.xml設定檔: Xml代碼 複製代碼 收藏代碼1.<?xml version="1.0" encoding="UTF-8"?>  2.<beans xmlns="http://www.springframework.org/schema/beans"  3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  4.    xmlns:mvc="http://www.springframework.org/schema/mvc"  5.    xsi:schemaLocation="http://www.springframework.org/schema/beans  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  7.     http://www.springframework.org/schema/context  8.   http://www.springframework.org/schema/context/spring-context-3.0.xsd  9.     http://www.springframework.org/schema/mvc  10.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  11.    <context:component-scan base-package="com.tiantian.mybatis"/>  12.    <context:property-placeholder location="classpath:config/jdbc.properties"/>  13.    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  14.       destroy-method="close">  15.       <property name="driverClassName" value="${jdbc.driver}" />  16.       <property name="url" value="${jdbc.url}" />  17.       <property name="username" value="${jdbc.username}" />  18.       <property name="password" value="${jdbc.password}" />  19.    </bean>  20.   21.    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  22.       <property name="dataSource" ref="dataSource" />  23.       <property name="mapperLocations" value="classpath:com/tiantian/mybatis/mapper/*.xml"/>  24.       <property name="typeAliasesPackage" value="com.tiantian.mybatis.model" />  25.    </bean>  26.   27.    <bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  28.       <property name="mapperInterface"  29.           value="com.tiantian.mybatis.mapper.BlogMapper" />  30.       <property name="sqlSessionFactory" ref="sqlSessionFactory" />  31.    </bean>  32.   33.</beans>   BlogMapper.xml檔案: Xml代碼 複製代碼 收藏代碼1.<?xml version="1.0" encoding="UTF-8" ?>  2.<!DOCTYPE mapper  3.  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  4.  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  5.<mapper namespace="com.tiantian.mybatis.mapper.BlogMapper">  6.<!--  新增記錄  -->  7.    <insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id">  8.        insert into t_blog(title,content,owner) values(#{title},#{content},#{owner})  9.    </insert>  10.<!--  查詢單條記錄  -->  11.    <select id="selectBlog" parameterType="int" resultMap="BlogResult">  12.       select * from t_blog where id = #{id}  13.    </select>  14.<!--  修改記錄  -->  15.    <update id="updateBlog" parameterType="Blog">  16.        update t_blog set title = #{title},content = #{content},owner = #{owner} where id = #{id}  17.    </update>  18.<!--  查詢所有記錄  -->  19.    <select id="selectAll" resultType="Blog">  20.        select * from t_blog  21.    </select>  22.<!--  刪除記錄  -->  23.    <delete id="deleteBlog" parameterType="int">  24.       delete from t_blog where id = #{id}  25.    </delete>  26.     27.</mapper>   

下載源碼 利用上面的方法進行整合的時候,我們有一個Mapper就需要定義一個對應的MapperFactoryBean,當我們的Mapper比較少的時候,這樣做也還可以,但是當我們的Mapper相當多時我們再這樣定義一個個Mapper對應的MapperFactoryBean就顯得速度比較慢了。為此Mybatis-Spring為我們提供了一個叫做MapperScannerConfigurer的類,通過這個類Mybatis-Spring會自動為我們註冊Mapper對應的MapperFactoryBean對象。

       如果我們需要使用MapperScannerConfigurer來幫我們自動掃描和註冊Mapper介面的話我們需要在Spring的applicationContext設定檔中定義一個MapperScannerConfigurer對應的bean。對於MapperScannerConfigurer而言有一個屬性是我們必須指定的,那就是basePackage。basePackage是用來指定Mapper介面檔案所在的基包的,在這個基包或其所有子包下面的Mapper介面都將被搜尋到。多個基包之間可以使用逗號或者分號進行分隔。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.