目录
前言
最近的工作中要做数据的迁移,用的数据库有 两个mysql库,两个oracle库,两个mongodb库。需要把一个主mysql库的数据迁移到oracel,中途需要关联查询 mysql库,oralce库,mongodb库从中取业务数据。先做个简单的demo 研究一下多数据源。
一、springboot yml文件配置
application.yml 配置如下:
server:
port: 8080test1-datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/database1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
test2-datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/database2?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
test3-datasource:
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@ (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx)(PORT=61512))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxx)))
username: root
password: 123456
test4-datasource:
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx)(PORT=61512))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxx)))
username: root
password: 123456
#数据库连接池
# mongodb多数据源
spring:
data:
mongodb:
one:
uri: mongodb://localhost:27017/test
database: test
two:
uri: mongodb://localhost:27017/test1
database: test1
二、mysql 和oracle多数据源的配置
主要有以下五步:
1 根据yml中的配置创建数据源 DataSource
2 设置动态数据源DynamicDataSource
3 mapper 扫描配置 MapperScannerConfigurer
4 根据数据源创建SqlSessionFactory
5 配置事务管理器DataSourceTransactionManager
代码如下:
@Configuration // 该注解类似于spring配置文件public class MyBatisConfig implements EnvironmentAware { private Environment environment; @Override public void setEnvironment(final Environment environment) { this.environment = environment; } @Bean public DataSource test1DataSource() throws Exception { Properties props = new Properties(); props.put("driverClassName", environment.getProperty("test1-datasource.driverClassName")); props.put("url", environment.getProperty("test1-datasource.url")); props.put("username", environment.getProperty("test1-datasource.username")); props.put("password", environment.getProperty("test1-datasource.password")); return DruidDataSourceFactory.createDataSource(props); } @Bean public DataSource test2DataSource() throws Exception { Properties props = new Properties(); props.put("driverClassName", environment.getProperty("test2-datasource.driverClassName")); props.put("url", environment.getProperty("test2-datasource.url")); props.put("username", environment.getProperty("test2-datasource.username")); props.put("password", environment.getProperty("test2-datasource.password")); return DruidDataSourceFactory.createDataSource(props); } @Bean public DataSource test3DataSource() throws Exception { Properties props = new Properties(); props.put("driverClassName", environment.getProperty("test3-datasource.driverClassName")); props.put("url", environment.getProperty("test3-datasource.url")); props.put("username", environment.getProperty("test3-datasource.username")); props.put("password", environment.getProperty("test3-datasource.password")); return DruidDataSourceFactory.createDataSource(props); } @Bean public DataSource test4DataSource() throws Exception { Properties props = new Properties(); props.put("driverClassName", environment.getProperty("test4-datasource.driverClassName")); props.put("url", environment.getProperty("test4-datasource.url")); props.put("username", environment.getProperty("test4-datasource.username")); props.put("password", environment.getProperty("test4-datasource.password")); return DruidDataSourceFactory.createDataSource(props); } @Bean @Primary public DynamicDataSource dataSource(@Qualifier("test1DataSource") DataSource test1DataSource, @Qualifier("test2DataSource") DataSource test2DataSource, @Qualifier("test3DataSource") DataSource test3DataSource, @Qualifier("test4DataSource") DataSource test4DataSource ) { Map
三、mysql 和oracle 多数据源的切换
这里定义一个切面,根据不同的数据源的对应service动态切换。
代码如下:
@Aspect@Componentpublic class DataSourceAspect { @Pointcut("execution(* com.xing.service.**.*(..))") public void declareJointPointExpression() { } @Before("declareJointPointExpression()") public void setDataSourceKey(JoinPoint point){ //根据连接点所属的类实例,动态切换数据源 if (point.getTarget() instanceof Test1Service || point.getTarget() instanceof Test1ServiceImpl) { DatabaseContextHolder.setDatabaseType(DatabaseType.test1); } else if (point.getTarget() instanceof Test3Service || point.getTarget() instanceof Test3ServiceImpl) { DatabaseContextHolder.setDatabaseType(DatabaseType.test3); } else if (point.getTarget() instanceof Test4Service || point.getTarget() instanceof Test4ServiceImpl) { DatabaseContextHolder.setDatabaseType(DatabaseType.test4); } else {//连接点所属的类实例是(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb) DatabaseContextHolder.setDatabaseType(DatabaseType.test2); } }}
四、测试方法的调用
代码如下:
@RestControllerpublic class TestDataSourceController { @Autowired private CommonService commonService; @RequestMapping(value = "/test1", method = RequestMethod.GET) public List selectUser() { System.out.println("查询第一个数据源"); List list = commonService.selectUserInfo(); System.out.println(JSON.toJSONString(list)); return list; } @RequestMapping(value = "/test2", method = RequestMethod.GET) public List userDetail() { System.out.println("查询第二个数据源"); List list = commonService.selectUserDetail(); System.out.println(JSON.toJSONString(list)); return list; } @RequestMapping(value = "/test3", method = RequestMethod.GET) public List user() { System.out.println("查询第三个数据源"); List list = commonService.selectUser(); System.out.println(JSON.toJSONString(list)); return list; } @RequestMapping(value = "/test4", method = RequestMethod.GET) public List userOrT1() { System.out.println("查询第四个数据源"); List list = commonService.selectUserOrT1(); System.out.println(JSON.toJSONString(list)); return list; }}
五、mongodb多数据源配置
分两个MongoTemplateConf配置,OneMongoTemplateConf配置第一个mongodb数据源,TwoMongoTemplateConf 配置第二个mongodb数据源。主要分两步: 第一步获取yml配置后创建MongoDatabaseFactory ,然后在创建MongoTemplate ,给bean 取个名。
OneMongoTemplateConf代码如下:
@Configuration@EnableMongoRepositories(mongoTemplateRef = "oneMongo")public class OneMongoTemplateConf implements EnvironmentAware{ private Environment environment; @Override public void setEnvironment(final Environment environment) { this.environment = environment; } @Bean(name = "oneMongo") @Primary public MongoTemplate mongoTemplate() { return new MongoTemplate(mongoDatabaseFactory()); } @Bean(name = "oneMongoFactory") @Primary public MongoDatabaseFactory mongoDatabaseFactory() { MongoClient client = MongoClients.create(environment.getProperty("spring.data.mongodb.one.uri")); return new SimpleMongoClientDatabaseFactory(client, environment.getProperty("spring.data.mongodb.one.database")); }}
TwoMongoTemplateConf 代码如下:
@Configuration@EnableMongoRepositories(mongoTemplateRef = "twoMongo")public class TwoMongoTemplateConf implements EnvironmentAware{ private Environment environment; @Override public void setEnvironment(final Environment environment) { this.environment = environment; } @Bean(name = "twoMongo") public MongoTemplate mongoTemplate() { return new MongoTemplate(mongoDatabaseFactory()); } @Bean(name = "twoMongoFactory") public MongoDatabaseFactory mongoDatabaseFactory() { MongoClient client = MongoClients.create(environment.getProperty("spring.data.mongodb.two.uri")); return new SimpleMongoClientDatabaseFactory(client, environment.getProperty("spring.data.mongodb.two.database")); }}
六、mongodb 测试方法的调用
注入MongoTemplate,注入时 需要用@Qualifier 注解指定用哪个MongoTemplate 实例。
代码如下:
@Autowired @Qualifier("oneMongo") private MongoTemplate oneMongoTemplate; @Autowired @Qualifier("twoMongo") private MongoTemplate twoMongoTemplate; @RequestMapping("/mongo1") public String testOneMongoInsert() { Map data = Maps.newHashMap(); Map info = Maps.newHashMap(); data.put("name", "cgg"); data.put("attr", "one"); info.put("age" , 18); data.put("info", info);// oneMongoTemplate.insert(data, "inventory_27017"); List
总结
以上就是今天要讲的内容,具体完整的项目代码已放到github上。
完整项目代码已在github上:github代码
来源地址:https://blog.csdn.net/weixin_43171875/article/details/128834266