這篇文章主要是說明了Spring3和MyBatis的整合方法,之所以要整合就是將MyBatis創建factory等各種繁冗的工作以及對session的管理、事務的管理交由spring容器幫我們管理,省心省力。
這個項目是在我的前幾篇文章的基礎上加入spring的內容,關於MyBatis的項目如何建立請查閱我之前的關於MyBatis的帖子。
整個的項目結構如下:
- 先把包都導入吧,下面是所有需要的包,spring的包最噁心,共計需要18個包,而且從spring官網下載的zip裏面僅僅有spring的jar包,引用的其他相關的包沒有,好不容易終於湊齊了,全部都在最後面的示例項目中
- 建立applicationContext.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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 配置c3p0數據源 -->
- <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
- <property name="user" value="root" />
- <property name="password" value="root" />
- <property name="initialPoolSize" value="10" />
- <property name="minPoolSize" value="5" />
- <property name="maxPoolSize" value="30" />
- <property name="acquireIncrement" value="10" />
- <property name="maxIdleTime" value="10" />
- <property name="maxStatements" value="0" />
- </bean>
- <!-- 配置SessionFactory,使用上面的c3p0數據源作爲數據庫連接 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="c3p0DataSource"/>
- </bean>
- <!-- 聲明式事務管理 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="c3p0DataSource" />
- </bean>
- <!-- 配置Mapper,相當於dao -->
- <bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="org.qiuqiu.dao.PersonMapper"/>
- <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
- </bean>
- </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
這三個必須對應,否則會報以下錯誤:
- Exception in thread "main" java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ...
- at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:594)
- at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:436)
- at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:428)
- at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:188)
- at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:51)
- at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:37)
- at $Proxy5.selectByExample(Unknown Source)<span style="white-space: normal; background-color: #ffffff;"> </span>
3.測試類,如下:
- package org.qiuqiu.test;
- import java.util.List;
- import org.qiuqiu.dao.PersonMapper;
- import org.qiuqiu.vo.Person;
- import org.qiuqiu.vo.PersonExample;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SpringTest {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- PersonMapper pm = ctx.getBean("personMapper", PersonMapper.class);
- PersonExample example = new PersonExample();
- example.createCriteria().andAgeLessThan(10);
- List<Person> list = pm.selectByExample(example);
- for (Person p : list) {
- System.out.println(p.getName() + " " + p.getAge() + " "+ p.getPassword());
- }
- }
- }