<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自动扫描 ,多个包以 逗号分隔-->
<context:component-scan base-package="com.yunzhijia.cloudflow" />

<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 数据库测试 -->
<bean id="dataSource_cs" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.cs.driver}" />
<property name="url" value="${dataSource.cs.url}" />
<property name="username" value="${dataSource.cs.username}" />
<property name="password" value="${dataSource.cs.password}" />
<property name="initialSize" value="${dataSource.cs.initialSize}"></property>
<property name="maxActive" value="${dataSource.cs.maxActive}"></property>
<property name="maxIdle" value="${dataSource.cs.maxIdle}"></property>
<property name="minIdle" value="${dataSource.cs.minIdle}"></property>
<property name="maxWait" value="${dataSource.cs.maxWait}"></property>
</bean>

<!-- 数据库福清 -->
<bean id="dataSource_fq" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.fq.driver}" />
<property name="url" value="${dataSource.fq.url}" />
<property name="username" value="${dataSource.fq.username}" />
<property name="password" value="${dataSource.fq.password}" />
<property name="initialSize" value="${dataSource.fq.initialSize}"></property>
<property name="maxActive" value="${dataSource.fq.maxActive}"></property>
<property name="maxIdle" value="${dataSource.fq.maxIdle}"></property>
<property name="minIdle" value="${dataSource.fq.minIdle}"></property>
<property name="maxWait" value="${dataSource.fq.maxWait}"></property>
</bean>

<!-- 数据库河南 -->
<bean id="dataSource_hn" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.hn.driver}" />
<property name="url" value="${dataSource.hn.url}" />
<property name="username" value="${dataSource.hn.username}" />
<property name="password" value="${dataSource.hn.password}" />
<property name="initialSize" value="${dataSource.hn.initialSize}"></property>
<property name="maxActive" value="${dataSource.hn.maxActive}"></property>
<property name="maxIdle" value="${dataSource.hn.maxIdle}"></property>
<property name="minIdle" value="${dataSource.hn.minIdle}"></property>
<property name="maxWait" value="${dataSource.hn.maxWait}"></property>
</bean>
<bean id="dataSource" class="com.yunzhijia.cloudflow.common.utils.ThreadLocalRountingDataSource">
<property name="defaultTargetDataSource" ref="dataSource_cs"/>
<property name="targetDataSources">
<map key-type="com.yunzhijia.cloudflow.common.utils.DataSources">
<entry key="DATASOURCE_CS" value-ref="dataSource_cs"/>
<entry key="DATASOURCE_FQ" value-ref="dataSource_fq"/>
<entry key="DATASOURCE_HN" value-ref="dataSource_hn"/>
</map>
</property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/yunzhijia/cloudflow/mapping/**/*.xml"></property>
<!-- 指向mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yunzhijia.cloudflow.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务控制的注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

========================================

package com.yunzhijia.cloudflow.common.utils;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 数据源切换
* 新建ThreadLocalRountingDataSource继承AbstractRoutingDataSource,实现切换
* @author gzh
*
*/
public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {

@Override
protected Object determineCurrentLookupKey() {
return DataSourceTypeManager.get();
}

}

========================================

package com.yunzhijia.cloudflow.common.utils;

import org.apache.log4j.Logger;

/**
* 数据源切换
* @author 
*
*/
public class DataSourceTypeManager {
private static final Logger log = Logger.getLogger(DataSourceTypeManager.class);
private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>(){
@Override
protected DataSources initialValue(){
log.info("==>初始化数据源:"+DataSources.DATASOURCE_CS);
return DataSources.DATASOURCE_CS;
}
};
/**
* 取DataSources
* @return DataSources
*/
public static DataSources get(){
log.info("==>取数据源:"+dataSourceTypes.get());
return dataSourceTypes.get();
}
/**
* 设置DataSources源
* @param dataSourceType
*/
public static void set(DataSources dataSourceType){
log.info("==>设置数据源:"+dataSourceType);
dataSourceTypes.set(dataSourceType);
}
/**
* 重置数据库源
*/
public static void reset(){
log.info("==>重置数据源:"+DataSources.DATASOURCE_CS);
dataSourceTypes.set(DataSources.DATASOURCE_CS);
}
}

================================

package com.yunzhijia.cloudflow.common.utils;
/**
* 配置数据库切换
* @author
* 注:注意此枚举值必须对应spring配置文件各数据源的别名
*/
public enum DataSources {
DATASOURCE_CS,DATASOURCE_FQ,DATASOURCE_HN
}

多数据源(sql server 2008,二个数据库不ip,)的更多相关文章

  1. 基于Sql Server 2008的分布式数据库的实践(二)

    原文 基于Sql Server 2008的分布式数据库的实践(二) 从Win7连接Win2003的Sql Server 2008 1.新建链接服务器链接到Win2003的Sql Server 2008 ...

  2. SQL Server 2008 R2 主从数据库同步

    一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2    DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...

  3. SQL Server 2008 R2 主从数据库同步设置

    一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2    DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...

  4. 基于Sql Server 2008的分布式数据库的实践(五)

    原文 基于Sql Server 2008的分布式数据库的实践(五) 程序设计 ------------------------------------------------------------- ...

  5. SQL Server 2008 维护计划实现数据库备份

    SQL Server 2008 维护计划实现数据库备份(最佳实践) 2013-08-29 09:08 by 听风吹雨, 173 阅读, 2 评论, 收藏, 编辑 一.背景 之前写过一篇关于备份的文章: ...

  6. SQL SERVER 2008 R2 还原数据库3154错误

    1.SQL SERVER 2008 在还原数据库时,会报错. 提示错误:"备份集中的数据库备份与现有的 '***' 数据库不同.RESTORE DATABASE 正在异常终止. (Micro ...

  7. 基于Sql Server 2008的分布式数据库的实践(四)

    原文 基于Sql Server 2008的分布式数据库的实践(四) 数据库设计 1.E-R图 2.数据库创建 Win 7 1 create database V3 Win 2003 1 create  ...

  8. 基于Sql Server 2008的分布式数据库的实践(三)

    原文 基于Sql Server 2008的分布式数据库的实践(三) 配置PHP 1.打开PHP配置文件,找到extension=php_mssql.dll,将前面的注释符号去掉 2.找到mssql.s ...

  9. 基于Sql Server 2008的分布式数据库的实践(一)

    原文 基于Sql Server 2008的分布式数据库的实践(一) 配置Sql Server 2008(Win7) 1.打开SQL server2012,使用windows身份登录 2.登录后,右键选 ...

  10. SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件

    原文:SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件 从2005开始,就引入了"数据库邮件"功能.并且取代SQLMail.原有SQLMail ...

随机推荐

  1. c++ 事件回调 java

    #pragma once #ifdef __cplusplus extern "C" { #endif typedef void(*sig_t)(int); int FirstEl ...

  2. Scala的集合框架

    1.元组 定义方式:val tp=("nana',1,1.1) 特点:集合中的数据可以是不同类型的 最多只能放22个元素 取值:通过角标取值,这里的角标是从1开始的,元组名称._角标   t ...

  3. lnmp一键安装包卸载mysql,重新安装报错mysql57-community-release conflicts with mysql-community-release-el6-5.noarch

    环境:CentOS Linux release 7.6.1810 lnmp1.5 独立下载mysql仓库 wget -i -c http://dev.mysql.com/get/mysql57-com ...

  4. PHP开发一个简单的成绩录入系统

    预览界面 源码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  5. idea的掌握

    1:idea的界面了解(一般都会勾选这两项,编码比较方便) 2: 如何配置sdk(jdk,最后一个生成的是.class文件的位置) 3: 如何单个项目配置和全局配置 4:如何配置项目的jdk编译版本和 ...

  6. win10安装anaconda及tensorflow1.9版本

    前言 因为之前的anaconda的conda命令不能用,又找不到原因,所以就决定重装anaconda,然后再装个tensorflow环境.. 正文 可以去官网下载,也可以去清华的开源软件镜像站下载ht ...

  7. xmake从入门到精通10:多个子工程目标的依赖配置

    xmake是一个基于Lua的轻量级现代化c/c++的项目构建工具,主要特点是:语法简单易上手,提供更加可读的项目维护,实现跨平台行为一致的构建体验. 本文主要详细讲解下,如果在一个项目中维护和生成多个 ...

  8. SELECT 与 SET给标量赋值

    本文转自 :https://blog.csdn.net/perddy/article/details/4033406 SQL Server推荐使用 SET 而不是 SELECT 对变量进行赋值.当表达 ...

  9. Java GC日志

    JVM 命令:-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseSerialGC [GC (Allocatio ...

  10. 列表、元组和range

    小知识点 s = " 5 " print(int(s)) print(s.replace(" ","")) 结果: 5 5 id()#获取对 ...