---
title: "spring+mybatis"
author: "xcgh"
date: "2013-07-10T13:43:47Z"
tags: []
categories: ["编程语言"]
slug: "spring-mybatis"
draft: false
lastmod: "2013-07-10T13:43:47Z"
id: 1902463
url: "https://xcgh.iteye.com/blog/1902463"
---

//读取spring配置文件  
//根据src\\test\\java\\的路径得到配置文件路径  
BeanFactory beanFactory = new ClassPathXmlApplicationContext(配置文件);  
//通过spring配置文件生成的beanFactory获得SqlSessionFactory  
SqlSessionFactory factory = (SqlSessionFactory) beanFactory  
                .getBean("sqlSessionFactory");

 

&lt;?xml version="1.0" encoding="utf-8"?&gt;   
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"   
    xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)"   
    xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)"   
    xmlns:context="[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)"    
    xsi:schemaLocation="   
         [http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)    
         [http://www.springframework.org/schema/context/spring-context-3.0.xsd](http://www.springframework.org/schema/context/spring-context-3.0.xsd)     
     [http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)    
     [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd](http://www.springframework.org/schema/beans/spring-beans-3.0.xsd)   
     [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)    
     [http://www.springframework.org/schema/tx/spring-tx-3.0.xsd](http://www.springframework.org/schema/tx/spring-tx-3.0.xsd)   
     [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)    
     [http://www.springframework.org/schema/aop/spring-aop-3.0.xsd](http://www.springframework.org/schema/aop/spring-aop-3.0.xsd)"&gt; 

 &lt;!-- 使用apache的DBCP连接池  --&gt;  
 &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  destroy-method="close"&gt;  
  &lt;!-- Connection Info --&gt;  
  &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;  
  &lt;property name="url" value="oracle链接" /&gt;  
  &lt;property name="username" value="账号" /&gt;  
  &lt;property name="password" value="密码" /&gt;

  &lt;!-- Connection Pooling DBCP --&gt;  
  &lt;property name="initialSize" value="5" /&gt;  
  &lt;property name="maxActive" value="100" /&gt;  
  &lt;property name="maxIdle" value="30" /&gt;  
  &lt;property name="maxWait" value="1000" /&gt;  
  &lt;property name="poolPreparedStatements" value="true" /&gt;  
  &lt;property name="defaultAutoCommit" value="false" /&gt;  
 &lt;/bean&gt;

 &lt;!-- 这里的dataSource要与sqlSessionFactory的dataSource一致，否则事务无效 --&gt;  
 &lt;bean id="transactionManager"  
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
  &lt;property name="dataSource" ref="dataSource" /&gt;  
 &lt;/bean&gt;

 &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;  
  &lt;property name="dataSource" ref="dataSource" /&gt;  
  &lt;property name="mapperLocations" value="classpath\*:META-INF/mappers/\*.xml" /&gt;  
 &lt;/bean&gt;

&lt;/beans&gt;