在前邊闡述了單獨使用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,
1
2
3
4
5
6
7
8
9
10
11
|
<?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,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?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&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的配置文件完成,可以進行測試,測試代碼如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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集成的一種方式,我們會發現在生成代理的時候如果有多個映射器類,則需要配置多次,比較麻煩,下篇文章使用另一種方式。
以上所述是小編給大家介紹的MyBatis如何使用(三),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/teach/archive/2016/07/25/5703321.html