博客
关于我
spring+mybatis+springMVC框架配置多数据源
阅读量:433 次
发布时间:2019-03-06

本文共 1071 字,大约阅读时间需要 3 分钟。

1.在spring配置文件中配置

2.继承AbstractRoutingDataSource

public class DBContextHolder {    private static final ThreadLocal
contextHolder = new ThreadLocal
(); public static void setDbType(String dbType) { contextHolder.set(dbType); } public static String getDbType() { return ((String) contextHolder.get()); } public static void clearDbType() { contextHolder.remove(); }}

 

public class DynamicDataSource extends AbstractRoutingDataSource {    @Override    protected Object determineCurrentLookupKey() {        return DBContextHolder.getDbType();    }}

3.切换数据库

//指定数据库DBContextHolder.setDbType("dataSource1");

 

转载地址:http://mclyz.baihongyu.com/

你可能感兴趣的文章
小白专场-FileTransfer-python语言实现
查看>>
191006
查看>>
08-03 细分构建机器学习应用程序的流程-流程简介
查看>>
B-概率论-条件概率
查看>>
191105
查看>>
191107
查看>>
191120
查看>>
191123
查看>>
第4章 字符串、数组和特殊矩阵
查看>>
第6章 树型结构
查看>>
0608-nn和autograd的区别
查看>>
MYSQL 数据库结构优化
查看>>
leetcode 一些算法题及答案
查看>>
spring 整合 ActiveMQ
查看>>
PHP 取前一天或后一天、一个月时间
查看>>
Kafka 分布式的,基于发布/订阅的消息系统
查看>>
Spring Bean的加载
查看>>
Web笔记(一) Web 简介与开发环境搭建
查看>>
Java基础回顾-缓冲流
查看>>
JSONPath小试牛刀之Snack3
查看>>