Mybatis多数据源调用
环境:intellij15,jdk1.8,maven3
参考:http://www.jb51.net/article/104053.htm
2、 在jdbc.properties文件中添加数据库连接
#============================================================================ # postgresql #============================================================================ jdbc.postgresql.driver = org.postgresql.Driver jdbc.postgresql.url = jdbc:postgresql://127.0.0.1:5432/cz?characterEncoding=UTF-8 jdbc.postgresql.username = postgres jdbc.postgresql.password = ****** #============================================================================ # oracle #============================================================================ jdbc.oracle.driver=oracle.jdbc.driver.OracleDriver jdbc.oracle.url=jdbc:oracle:thin:@smart-dafeng:1521:orcl jdbc.oracle.username=df jdbc.oracle.password=******
3、 在applicationContext-dao.xml中添加数据库连接及multipleDataSource的bean
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--获取数据库配置文件--> <context:property-placeholder location="classpath:config/jdbc.properties"/> <!--设置数据源c3p0--> <bean id="oracleDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.oracle.driver}"/> <property name="jdbcUrl" value="${jdbc.oracle.url}"/> <property name="user" value="${jdbc.oracle.username}"/> <property name="password" value="${jdbc.oracle.password}"/> <property name="maxPoolSize" value="50"/> <property name="minPoolSize" value="2"/> <property name="maxIdleTime" value="60"/> </bean> <!--设置数据源c3p0--> <bean id="postgresqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.postgresql.driver}"/> <property name="jdbcUrl" value="${jdbc.postgresql.url}"/> <property name="user" value="${jdbc.postgresql.username}"/> <property name="password" value="${jdbc.postgresql.password}"/> <property name="maxPoolSize" value="50"/> <property name="minPoolSize" value="2"/> <property name="maxIdleTime" value="60"/> </bean> <bean id="multipleDataSource" class="com.elin4it.ssm.utils.MultipleDataSource"> <property name="defaultTargetDataSource" ref="postgresqlDataSource"/> <property name="targetDataSources"> <map> <entry key="postgresqlDataSource" value-ref="postgresqlDataSource"/> <entry key="oracleDataSource" value-ref="oracleDataSource"/> </map> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="multipleDataSource"/> </bean> <!-- mybatis.spring自动映射 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.elin4it.ssm.mapper"/> </bean> <!-- 自动扫描,多个包以 逗号分隔 --> <context:component-scan base-package="com.elin4it.ssm"/> <aop:aspectj-autoproxy/> </beans>
4、 在com.elin4it.ssm.utils添加MultipleDataSource类
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * Created by Administrator on 2018/3/21. */ public class MultipleDataSource extends AbstractRoutingDataSource { private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>(); public static void setDataSourceKey(String dataSource) { dataSourceKey.set(dataSource); } @Override protected Object determineCurrentLookupKey() { return dataSourceKey.get(); } }
5、 将applicationContext-transaction.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">--> <!--<property name="dataSource" ref="dataSource"/>--> <!--</bean>--> <!--<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">--> <!--<tx:attributes>--> <!--<tx:method name="find*" propagation="REQUIRED"/>--> <!--<tx:method name="update*" propagation="REQUIRED"/>--> <!--<tx:method name="delete*" propagation="REQUIRED"/>--> <!--<tx:method name="add*" propagation="REQUIRED"/>--> <!--</tx:attributes>--> <!--</tx:advice>--> <!--<aop:config>--> <!--<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.elinzhou.ixxs.service.*.*(..))"/>--> <!--</aop:config>--> </beans>
6、 在UserServiceImpl中添加数据库选择语句:MultipleDataSource.setDataSourceKey("oracleDataSource");
完成代码如下所示:
package com.elin4it.ssm.service; import com.elin4it.ssm.mapper.StudentsMapper; import com.elin4it.ssm.pojo.Students; import com.elin4it.ssm.utils.MultipleDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by Administrator on 2018/3/21. */ @Service public class UserServiceImpl implements UserService { //User接口 此处注入UserMapper时需要在UserMapper接口上添加<span style="font-family: Arial, Helvetica, sans-serif;">@Component进行依赖注入</span> @Autowired private StudentsMapper userMapper; public Students findUser() throws Exception { // 此处切换数据库,输出不同结果 // MultipleDataSource.setDataSourceKey("postgresqlDataSource"); MultipleDataSource.setDataSourceKey("oracleDataSource"); //调用mapper类中的selectByExample方法,如果传入类型为null,则表示无条件查找 Students users = userMapper.selectByPrimaryKey(2); return users; } }
7、 在pom中添加oracle数据库依赖:
<!-- 数据库驱动 --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency>
8、 运行。Oracle数据库输出结果:
修改UserServiceImpl.java代码后,Postgresql数据库输出结果: