如何使用mybatis《三》,使用mybatis《三》

來源:互聯網
上載者:User

如何使用mybatis《三》,使用mybatis《三》

在前邊闡述了單獨使用mybatis的方法,在實際開發過程中mybatis經常和spring一起使用,即mybatis和spring進行整合,現在我們來看如何整合。

mybatis和spring進行整合需要用到整合套件:mybatis-spring-1.1.1.jar,此包提供mybatis和spring整合的支援,把此包匯入到項目的lib目錄下。

我們先看mybatis單獨使用的時候的過程,mybatis設定檔==》讀取設定檔==》操作資料庫,具體的使用方法可參照前兩篇文章。

下面進行mybatis和spring的整合,

一、mybatis設定檔

在和spring做整合時mybatis的設定檔中有些配置不再需要了,spring會使用它自己的。如資料來源,下面看下mybatis的設定檔,MybatisConfiguration.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>         <typeAlias alias="Message" type="com.cn.imooc.entity.Message"/>     </typeAliases>     <mappers>         <mapper resource="com/cn/mappers/message.xml"/>     </mappers> </configuration>

上面的設定檔配置了別名和mappers對應檔,和之前的設定檔相比,可以看出沒有了關於資料來源的資訊,這裡在mybatis的設定檔中不再需要配置資料來源,需要在spring的設定檔中配置。

二、spring設定檔

既然和spring做整合,那麼必須匯入spring的包,關於spring的包可以從前面的文章中獲得;匯入spring的包之後,就需要配置spring的設定檔,我們把spring的設定檔放在src下,名字為spring-application.xml,

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    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.0.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">        <bean id="address" class="com.cn.test.spring.Address"></bean>         <!-- 引入jdbc設定檔 -->       <!--    <context:property-placeholder location="jdbc.properties"/>       -->     <!--1、建立jdbc資料來源 -->       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>       <property name="url" value="jdbc:mysql://127.0.0.1:3306/weixin?useUnicode=true&amp;characterEncoding=UTF-8" />       <property name="username" value="root"/>       <property name="password" value="123456"/>             </bean>       <!--2、sqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:MybatisConfiguration.xml"></property> <!-- <property name="mapperLocations" value="classpath:com/cn/mappers/message.xml"></property> --> </bean> <bean id="messageMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.cn.inter.IMessageOperation" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>

首先,我們配置了一個資料來源,這裡如果引入了context的命名空間,可以使用<context:property-placeholder location="jdbc.properties"/>,引入src下的設定檔。

其次,配置了sqlSessionFactoryBean,這裡使用sqlSessionFactoryBean產生sqlSessionFactory(在mybatis中sqlSessionFactory由sqlSessionFactoryBuilder產生)。要通過sqlSessionFactroyBean產生sqlSessionFactroy有以下幾個屬性,

dataSource  即剛才配置的資料來源,指定產生sqlSessionFactory使用的資料來源

configLocation  這個屬性指定mybatis的設定檔的路徑,在本例中我們使用了src下的MybatisConfiguration.xml,如果在此檔案中配置了mappers對應檔,則不需要第三個屬性,如果沒配置對應檔則需要第三個屬性;假如,在MybatisConfiguration.xml檔案中沒有配置對應檔,也沒有配置mapperLocations屬性,則對應檔必須必須和映射器類在同一個包下,且對應檔和映射器類必須名字相同。

mapperLocations  指定mappers對應檔,這個屬性可以配置為一個list的值

最後,使用動態代理產生訪問資料庫的代碼,MapperFactoryBean作為一個工廠類,可以用來產生訪問資料庫的動態代理,有兩種方式可以產生一個動態代理,這裡使用了mapperInterface和sqlSessionFactory兩個屬性,第一個指定映射器類的全限路徑,第二個就是上面的sqlSessionFactory;另一種方式是使用註解的方式。

至此,spring的設定檔完成,可以進行測試,測試代碼如下,

package com.cn.test.spring;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.support.SqlSessionDaoSupport;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cn.imooc.entity.Message;import com.cn.inter.IMessageOperation;public class TestSpringAndMybatis {            public static void main(String[] args) {        // TODO Auto-generated method stub        //獲得spring的配置        ClassPathXmlApplicationContext cpxac=new ClassPathXmlApplicationContext("spring-application.xml");        //獲得IMessageOperation介面類        IMessageOperation imo=(IMessageOperation)cpxac.getBean("messageMapper");                Message m=imo.selectMessageById("2");        System.out.println(m);            }}

上邊完成了mybatis和spring整合的一種方式,我們會發現在組建代理程式的時候如果有多個映射器類,則需要配置多次,比較麻煩,下篇文章使用另一種方式。

有不正之處歡迎指正!

謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.