ssm 动态切换数据源
1,添加数据库配置
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver #jdbc.url=jdbc:sqlserver://192.168.1.73:1433; DatabaseName=HerPeisYCYTTJZX #jdbc.username=sa #jdbc.password=dky19921022 jdbc.url=jdbc:sqlserver://101.201.209.98:1433; DatabaseName=HerPeisGYFY jdbc.username=sa jdbc.password=BJBR_20170101 jdbc2.url=jdbc:sqlserver://101.201.209.98:1433; DatabaseName=HerpeisMYZXYY jdbc2.username=sa jdbc2.password=BJBR_20170101
2,添加数据库连接池及动态选择
<!-- 数据库连接池 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:resource/db.properties" /> <!-- 数据库连接池 --> <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- 数据库连接池 --> <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc2.url}" /> <property name="username" value="${jdbc2.username}" /> <property name="password" value="${jdbc2.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> --> <bean id="dynamicDataSource" class="util.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <!-- 指定lookupKey和与之对应的数据源 --> <entry key="dataSource1" value-ref="dataSource1"></entry> <entry key="dataSource2" value-ref="dataSource2"></entry> </map> </property> <!-- 这里可以指定默认的数据源 --> <property name="defaultTargetDataSource" ref="dataSource1" /> </bean> <!-- 配置sqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > <property name="configLocation" value="classpath:resource/SqlMapConfig.xml"></property> <property name="dataSource" ref="dynamicDataSource"></property> </bean>
3,动态选择数据库的类DynamicDataSource及DynamicDataSourceHolder
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DynamicDataSource extends AbstractRoutingDataSource { /** * 该方法返回需要使用的DataSource的key值, * 然后根据这个key从resolvedDataSources这个map里取出对应的DataSource, * 如果找不到,则用默认的resolvedDefaultDataSource。 */ @Override protected Object determineCurrentLookupKey() { // 从自定义的位置获取数据源标识 return DynamicDataSourceHolder.getDataSource(); } } public class DynamicDataSourceHolder { /** * 注意:数据源标识保存在线程变量中,避免多线程操作数据源时互相干扰 */ private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal<String>(); public static String getDataSource() { return THREAD_DATA_SOURCE.get(); } public static void setDataSource(String dataSource) { THREAD_DATA_SOURCE.set(dataSource); } public static void clearDataSource() { THREAD_DATA_SOURCE.remove(); } }
4,调用
@RequestMapping("ce") public String ce(HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException{ Integer countUser = userService.countUser();//查询的是dataSource1的 System.out.println(countUser); DynamicDataSourceHolder.setDataSource("dataSource2"); System.out.println(userService.countUser());//查询的是dataSource2的 return "login"; }
本文来自博客园,作者:每天都要学一点,欢迎讨论和转载,转载请注明原文链接:https://www.cnblogs.com/yanan7890/p/8656054.html