多数据源切换
第一数据源是mysql
1 connection.url=jdbc:mysql://127.0.0.1:3306/bmh?useUnicode=true&characterEncoding=utf8 2 connection.username=root 3 connection.password=bNVOqb7WKLX5Bjnw+LMv92taj25KOxDimXxILPQjw42wgv+1lHzOH8kr97xDwWdhpY67QuYCS7sWN4W46YbkFA==
datasource.properties
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- JNDI方式配置数据源 --> <!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> <!-- 数据源 --> <!--see https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="${connection.url}"/> <property name="username" value="${connection.username}"/> <property name="password" value="${connection.password}"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- 初始化连接大小、连接池最小空闲、最大使用连接数量 --> <property name="initialSize" value="${druid.initialSize}"/> <property name="minIdle" value="${druid.minIdle}"/> <property name="maxActive" value="${druid.maxActive}"/> <!-- 获取连接等待超时的时间 --> <property name="maxWait" value="${druid.maxWait}"/> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。 --> <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxPoolPreparedStatementPerConnectionSize}"/> <!-- 检测连接是否有效sql,要求是一个查询语句,如果为null,则后三个参数无效。 --> <property name="validationQuery" value="${druid.validationQuery}"/> <!-- 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 --> <property name="testWhileIdle" value="${druid.testWhileIdle}"/> <!-- 申请连接时执行 validationQuery检测连接是否有效,做了这个配置会降低性能。 --> <property name="testOnBorrow" value="${druid.testOnBorrow}"/> <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 --> <property name="testOnReturn" value="${druid.testOnReturn}"/> <!-- 间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}"/> <!-- 一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}"/> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="1800"/> <property name="logAbandoned" value="true"/> <!-- 配置监控统计拦截的filters --> <property name="filters" value="${druid.filters}"/> <!-- 慢查询sql打印 --> <property name="connectionProperties" value="${druid.connectionProperties}"/> </bean> <bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="SQL Server">sqlserver</prop> <prop key="DB2">db2</prop> <prop key="Oracle">oracle</prop> <prop key="MySQL">mysql</prop> </props> </property> </bean> <bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider"> <property name="properties" ref="vendorProperties"/> </bean> <bean id="sqlSessionFactory" class="com.ylzinfo.eva.core.persistence.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations"> <list> <value>classpath:/mappings/**/*.xml</value> </list> </property> <property name="plugins"> <bean class="com.ylzinfo.framework.sys.web.mybatis.MybatisSpringPageInterceptor"></bean> </property> <!-- 数据库id标识 --> <property name="databaseIdProvider" ref="databaseIdProvider"></property> <!-- 注册类型 --> <property name="typeAliasesPackage" value="com.ylzinfo.**.domain"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ylzinfo.**.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!--事务管理器配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="query*" propagation="REQUIRED" read-only="true"/> <tx:method name="read*" propagation="REQUIRED" read-only="true"/> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="list*" propagation="REQUIRED" read-only="true"/> <tx:method name="count*" propagation="REQUIRED" read-only="true"/> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="grant*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="un*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <!-- <tx:method name="*" propagation="REQUIRED" read-only="true"/>--> </tx:attributes> </tx:advice> <aop:config> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="pointcut" expression="execution(* com.ylzinfo..service..*Impl.*(..)) or execution(* com.ylzinfo..task..*Impl.*(..))"/> <aop:advisor id="advisor" advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
spring-mybatis.xml
第二数据源oracle
1 #第二数据源 2 oracle.jdbc.driver=oracle.jdbc.OracleDriver 3 oracle.mysql.jdbc.url=jdbc\:oracle\:thin\:@127.0.0.2:1521/SHBX 4 oracle.myjdbc.username=shbxcard 5 oracle.jdbc.password=shbxcard 6 7 8 #第三数据源 9 1oracle.jdbc.driver=oracle.jdbc.OracleDriver 10 1oracle.mysql.jdbc.url=jdbc\:oracle\:thin\:@127.0.0.3:1521/ZXCSORCL 11 1oracle.myjdbc.username=nxsi_ykt 12 1oracle.jdbc.password=YKT@SBH61093
oracle-db.properties
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 5 <properties resource="oracle-db.properties" /> 6 7 <settings> 8 9 <!-- 设置自动驼峰转换 --> 10 <setting name="mapUnderscoreToCamelCase" value="true" /> 11 12 <!-- 开启懒加载 --> 13 <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。默认:true --> 14 <setting name="aggressiveLazyLoading" value="false" /> 15 16 </settings> 17 18 <!-- 别名定义 --> 19 <typeAliases> 20 <package name="com.ylzinfo.socialsecurity.domain.model" /> 21 </typeAliases> 22 23 24 <!--配置environment环境 --> 25 <environments default="development"> 26 <!-- 环境配置1,每个SqlSessionFactory对应一个环境 --> 27 <environment id="development"> 28 <transactionManager type="JDBC" /> 29 <dataSource type="UNPOOLED"> 30 <property name="driver" value="${oracle.jdbc.driver}" /> 31 <property name="url" value="${oracle.mysql.jdbc.url}" /> 32 <property name="username" value="${oracle.myjdbc.username}" /> 33 <property name="password" value="${oracle.jdbc.password}" /> 34 </dataSource> 35 </environment> 36 37 38 </environments> 39 40 <!-- 映射文件,mapper的配置文件 --> 41 <mappers> 42 <!--直接映射到相应的mapper文件 --> 43 <mapper resource="mappings/callable/AppGthdDao.xml" /> 44 <mapper resource="mappings/callable/AppGtdjDao.xml" /> 45 <mapper resource="mappings/socialsecurity/ABA1Mapper.xml" /> 46 <mapper resource="mappings/socialsecurity/CBCXMapper.xml" /> 47 <mapper resource="mappings/socialsecurity/ExpendsQueryMapper.xml" /> 48 <mapper resource="mappings/socialsecurity/BC18Mapper.xml" /> 49 <mapper resource="mappings/socialsecurity/SocialCardInfoMapper.xml" /> 50 <mapper resource="mappings/callable/SocialCardBindMapper.xml" /> 51 <mapper resource="mappings/socialsecurity/AC01Mapper.xml" /> 52 <mapper resource="mappings/callable/AppCbdjYLMapper.xml" /> 53 <mapper resource="mappings/callable/AppCbhdYLMapper.xml" /> 54 </mappers> 55 56 57 </configuration>
mybatis-config2
1 package com.ylzinfo.socialsecurity.util; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 8 import java.io.IOException; 9 import java.io.Reader; 10 11 /** 12 * description 13 * created 2019/9/3 14:29 14 * 15 * @version --添加版本信息 16 * @author hoje 17 * @since Version 1.0 18 */ 19 public class SqlSessionFactoryUtil { 20 21 private static SqlSessionFactory sessionFactory; 22 static { 23 Reader reader =null; 24 try { 25 reader= Resources.getResourceAsReader("mybatis-config2.xml"); 26 sessionFactory =new SqlSessionFactoryBuilder().build(reader,"development"); 27 } catch (IOException e) { 28 e.printStackTrace(); 29 }finally { 30 try { 31 reader.close(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 } 36 } 37 public static SqlSession getSession(){ 38 return sessionFactory.openSession(); 39 } 40 }
SqlSessionFactoryUtil
填写映射文件
第三数据源
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 5 <properties resource="oracle-db.properties" /> 6 7 <settings> 8 9 <!-- 设置自动驼峰转换 --> 10 <setting name="mapUnderscoreToCamelCase" value="true" /> 11 12 <!-- 开启懒加载 --> 13 <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。默认:true --> 14 <setting name="aggressiveLazyLoading" value="false" /> 15 16 </settings> 17 18 <!-- 别名定义 --> 19 <typeAliases> 20 <package name="com.ylzinfo.socialsecurity.domain.model" /> 21 </typeAliases> 22 23 24 <!--配置environment环境 --> 25 <environments default="development"> 26 <!-- 环境配置1,每个SqlSessionFactory对应一个环境 --> 27 <environment id="development"> 28 <transactionManager type="JDBC" /> 29 <dataSource type="UNPOOLED"> 30 <property name="driver" value="${1oracle.jdbc.driver}" /> 31 <property name="url" value="${1oracle.mysql.jdbc.url}" /> 32 <property name="username" value="${1oracle.myjdbc.username}" /> 33 <property name="password" value="${1oracle.jdbc.password}" /> 34 </dataSource> 35 </environment> 36 37 38 </environments> 39 40 <!-- 映射文件,mapper的配置文件 --> 41 <mappers> 42 <!--直接映射到相应的mapper文件 --> 43 <mapper resource="mappings/callable/AppCbdjYBMapper.xml" /> 44 <mapper resource="mappings/callable/AppCbhdYBMapper.xml" /> 45 </mappers> 46 47 48 </configuration>
mybatis-config3
1 package com.ylzinfo.socialsecurity.util; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 8 import java.io.IOException; 9 import java.io.Reader; 10 11 /** 12 * description 13 * created 2019/9/3 14:29 14 * 15 * @version --添加版本信息 16 * @author hoje 17 * @since Version 1.0 18 */ 19 public class SqlSessionFactoryUtil3 { 20 21 private static SqlSessionFactory sessionFactory; 22 static { 23 Reader reader =null; 24 try { 25 reader= Resources.getResourceAsReader("mybatis-config3.xml"); 26 sessionFactory =new SqlSessionFactoryBuilder().build(reader,"development"); 27 } catch (IOException e) { 28 e.printStackTrace(); 29 }finally { 30 try { 31 reader.close(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 } 36 } 37 public static SqlSession getSession(){ 38 return sessionFactory.openSession(); 39 } 40 }
SqlSessionFactoryUtil3
如何调用
以调用存储过程为例
SqlSession session = SqlSessionFactoryUtil.getSession(); AppCbdjYLMapper AppCbdjYLMapper = session.getMapper(AppCbdjYLMapper.class); Map map = new HashMap(); map.put("is_aab301", aab301); map.put("is_aac002", aac002); map.put("is_aac003", aac003); map.put("is_aae006", aae006); map.put("is_aac067", aac067); AppCbdjYLMapper.appcbdjyl(map); String info; info = (String) map.get("os_errinfo"); session.commit();//这里一定要提交,不然数据进不去数据库中 session.close();
View Code
版权声明:本文为hoje原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。