`
xcgh
  • 浏览: 75534 次
  • 来自: ...
社区版块
存档分类
最新评论

spring+mybatis

    博客分类:
  • java
 
阅读更多

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

 

<?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
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation=" 
         http://www.springframework.org/schema/context  
         http://www.springframework.org/schema/context/spring-context-3.0.xsd   
     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"> 

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

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

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

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="mapperLocations" value="classpath*:META-INF/mappers/*.xml" />
 </bean>

</beans>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics