今天配置了下ssm多数据源:java
1.首先定义数据库spring
在这里定义了三个类,sql
public class DataSourceType { public static final String ONE = "datasource_one"; public static final String TWO = "datasource_two"; }
public class DataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static void setDbType(String dbType) { contextHolder.set(dbType); } public static String getDbType() { return ((String) contextHolder.get()); } public static void clearDbType() { contextHolder.remove(); } }
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; import java.util.logging.Logger; public class DynamicDataSource extends AbstractRoutingDataSource { @Override public Logger getParentLogger() { return null; } @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDbType(); } }
这里主要是用来切换数据源的.数据库
这是两个数据库的表使用MybatisGeneratorXml逆向工程生产的.mybatis
配置数据源app
配置sqlSessionFactoryide
<?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" 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"> <!--<context:property-placeholder location="classpath:jdbc.properties"/>--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 配置数据源,数据库链接池 --> <bean id="datasource_one" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!--数据库驱动--> <property name="driverClassName" value="${jdbc.driver}"/> <!--数据库连接地址--> <property name="url" value="${jdbc.url.one}"/> <!--数据库用户名--> <property name="username" value="${jdbc.username}"/> <!--数据库链接密码--> <property name="password" value="${jdbc.password}"/> <!--链接池初始化大小--> <property name="initialSize" value="${jdbc.initialSize}"/> <!--链接池最小数量--> <property name="minIdle" value="${jdbc.minIdle}"/> <!--链接池最大数量--> <property name="maxActive" value="${jdbc.maxActive}"/> <!--链接池等待超时时间--> <property name="maxWait" value="${jdbc.maxWait}"/> <!--配置间隔多久才进行一次检测,检测须要关闭空闲链接,单位是毫秒--> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/> <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/> <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/> <property name="validationQuery" value="${jdbc.validationQuery}"/> <property name="testOnReturn" value="${jdbc.testOnReturn}"/> <!--打开PSCache,而且制定每一个链接上PSCache的大小--> <property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="${jdbc.maxPoolPreparedStatementPerConnectionSize}"/> <!--配置监控统计拦截的filters--> <property name="filters" value="${jdbc.filters}"/> </bean> <bean id="datasource_two" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!--数据库驱动--> <property name="driverClassName" value="${jdbc.driver}"/> <!--数据库连接地址--> <property name="url" value="${jdbc.url.two}"/> <!--数据库用户名--> <property name="username" value="${jdbc.username}"/> <!--数据库链接密码--> <property name="password" value="${jdbc.password}"/> <!--链接池初始化大小--> <property name="initialSize" value="${jdbc.initialSize}"/> <!--链接池最小数量--> <property name="minIdle" value="${jdbc.minIdle}"/> <!--链接池最大数量--> <property name="maxActive" value="${jdbc.maxActive}"/> <!--链接池等待超时时间--> <property name="maxWait" value="${jdbc.maxWait}"/> <!--配置间隔多久才进行一次检测,检测须要关闭空闲链接,单位是毫秒--> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/> <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/> <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/> <property name="validationQuery" value="${jdbc.validationQuery}"/> <property name="testOnReturn" value="${jdbc.testOnReturn}"/> <!--打开PSCache,而且制定每一个链接上PSCache的大小--> <property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="${jdbc.maxPoolPreparedStatementPerConnectionSize}"/> <!--配置监控统计拦截的filters--> <property name="filters" value="${jdbc.filters}"/> </bean> <!-- 动态配置数据源 --> <bean id="dataSource" class="club.iyousi.datasource.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="datasource_one" key="datasource_one"></entry> <entry value-ref="datasource_two" key="datasource_two"></entry> </map> </property> <property name="defaultTargetDataSource" ref="datasource_one"></property> <!-- 默认使用ds1的数据源 --> </bean> <!-- 配置sqlSessionFactory交由spring进行管理 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 设置数据源引用 --> <property name="dataSource" ref="dataSource"></property> <!--配置mapping文件--> <property name="mapperLocations" > <list> <value>classpath*:club/iyousi/one/mapping/*.xml</value> <value>classpath*:club/iyousi/two/mapping/*.xml</value> </list> </property> <!-- 加载mybatis的全局配置文件路径 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> </bean> <!-- 配置mapper接口扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 要扫描的包的名称,有多个包时,包名之间用半角逗号隔开 --> <property name="basePackage" value="club.iyousi.one.dao,club.iyousi.two.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>