码迷,mamicode.com
首页 > 数据库 > 详细

Spring MyBatis Oracle 多数据源

时间:2016-08-06 08:36:55      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

数据源1

<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@//IP地址1:1521/数据库名1" />
        <property name="username" value="用户名" />
        <property name="password" value="密码" />
</bean>
<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource1" />
        <property name="configLocation" value="classpath:myBatis/myBatis-Configuration1.xml" />
</bean>
<bean id="mapper1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zxg.dao1" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1" />
</bean>
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource1" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager1" />

 

数据源2

<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@//IP地址2:1521/数据库名2" />
        <property name="username" value="用户名" />
        <property name="password" value="密码" />
</bean>
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource2" />
        <property name="configLocation" value="classpath:myBatis/myBatis-Configuration2.xml" />
</bean>
<bean id="mapper2" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zxg.dao2" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" />
</bean>
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource2" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager2" />

注:xx1Mapper.java  和  xx1Mapper.xml      、 xx2Mapper.java 和 xx2Mapper.xml

     上下文中不能出现同名的

结果:操作db1 调用 mapper1 ,操作db2 调用 mapper2

 

Spring MyBatis Oracle 多数据源

标签:

原文地址:http://www.cnblogs.com/zno2/p/4488401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!