Mybatis入門實例(四)——MyBatis與Spring 3.X的整合

這篇文章主要是說明了Spring3和MyBatis的整合方法,之所以要整合就是將MyBatis創建factory等各種繁冗的工作以及對session的管理、事務的管理交由spring容器幫我們管理,省心省力。

這個項目是在我的前幾篇文章的基礎上加入spring的內容,關於MyBatis的項目如何建立請查閱我之前的關於MyBatis的帖子。

 

整個的項目結構如下:

  1. 先把包都導入吧,下面是所有需要的包,spring的包最噁心,共計需要18個包,而且從spring官網下載的zip裏面僅僅有spring的jar包,引用的其他相關的包沒有,好不容易終於湊齊了,全部都在最後面的示例項目中
  2. 建立applicationContext.xml文件,放在類路徑下面,內容如下:
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.   
  8.     <!-- 配置c3p0數據源 -->  
  9.     <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  10.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>  
  11.         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />  
  12.         <property name="user" value="root" />  
  13.         <property name="password" value="root" />  
  14.         <property name="initialPoolSize" value="10" />  
  15.         <property name="minPoolSize" value="5" />  
  16.         <property name="maxPoolSize" value="30" />  
  17.         <property name="acquireIncrement" value="10" />  
  18.         <property name="maxIdleTime" value="10" />  
  19.         <property name="maxStatements" value="0" />  
  20.     </bean>  
  21.       
  22.     <!-- 配置SessionFactory,使用上面的c3p0數據源作爲數據庫連接 -->  
  23.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  24.         <property name="dataSource" ref="c3p0DataSource"/>  
  25.     </bean>  
  26.     <!-- 聲明式事務管理 -->  
  27.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  28.         <property name="dataSource" ref="c3p0DataSource" />  
  29.     </bean>  
  30.     <!-- 配置Mapper,相當於dao -->  
  31.     <bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  32.         <property name="mapperInterface" value="org.qiuqiu.dao.PersonMapper"/>  
  33.         <property name="sqlSessionFactory" ref="sqlSessionFactory"/>  
  34.     </bean>  
  35.       
  36. </beans>  
 上面的配置文件我就不多說了,一個是數據源,連接數據庫用的,第二個是讓Spring管理session用的,第三個是聲明式事務,最後一個就是dao了。
有一點必須注意,interface、mapper file、mapper namespace這三個必須對應(並不是完全一樣,而是對應),正確的寫法如下:
interface:org.qiuqiu.dao.PersonMapper
mapper file:org/qiuqiu/dao/PersonMapper.xml
mapper namespace:org.qiuqiu.dao.PersonMapper
這三個必須對應,否則會報以下錯誤:
Java代碼  收藏代碼
  1. Exception in thread "main" java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ...  
  2. at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:594)  
  3. at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:436)  
  4. at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:428)  
  5. at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:188)  
  6. at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:51)  
  7. at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:37)  
  8. at $Proxy5.selectByExample(Unknown Source)<span style="white-space: normal; background-color: #ffffff;"> </span>  

3.測試類,如下:
Java代碼  收藏代碼
  1. package org.qiuqiu.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.qiuqiu.dao.PersonMapper;  
  6. import org.qiuqiu.vo.Person;  
  7. import org.qiuqiu.vo.PersonExample;  
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  10.   
  11. public class SpringTest {  
  12.   
  13.     public static void main(String[] args) {  
  14.     ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  15.     PersonMapper pm = ctx.getBean("personMapper", PersonMapper.class);  
  16.     PersonExample example = new PersonExample();  
  17.     example.createCriteria().andAgeLessThan(10);  
  18.     List<Person> list = pm.selectByExample(example);  
  19.     for (Person p : list) {  
  20.         System.out.println(p.getName() + "  " + p.getAge() + "  "+ p.getPassword());  
  21.     }  
  22.     }  
  23.   
  24. }  
 附件中會包含所有的jar包以及數據庫表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章