由于项目前期业务设计的原因,不同CP的数据放置在了同一个ip和端口的mongdb中,6个CP数据在同一个ip和端口下,但是数据库的名称、集合的名称各不相同。
现在需要一个查询系统,根据选择不同的cp去查询不同的database和集合拿到需要展现的数据
解决思路:
将不同cp对应的数据库的名称和集合的名称存放在mysql中的一张表里(db_info),初始化时将db_info中取出所有的mongodb数据库信息,将其初始化添加到动态数据源中。将数据源添加到map中,key为cp,value为mongdb数据源
mysql中创建db_info表
CREATE TABLE `db_info` (
`id` varchar(32) NOT NULL DEFAULT '' COMMENT 'cp英文名称',
`db_name` varchar(50) DEFAULT NULL COMMENT 'database名称',
`name` varchar(50) DEFAULT NULL COMMENT 'cp名称',
`order_name` varchar(50) DEFAULT NULL COMMENT '前台展示信息',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在config.properties中配置mysql数据源和mongndb的ip和端口
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/epay?useUnicode=true&characterEncoding=utf8
jdbc.username=test
jdbc.password=test1
mongodb.host=192.168.162.130
mongodb.port=27019
applicationContent.xml
<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis="http://www.mybatis.org/schema/mybatis"
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.or