Spring mvc 配置mongodb多数据源

本文介绍了一个项目中如何处理多个CP的数据存储在同一个MongoDB实例的不同数据库和集合下。通过在MySQL的db_info表中存储CP的数据库和集合信息,在应用启动时动态加载到数据源,并使用Spring MVC的配置文件进行设置,实现了根据选择的CP查询对应MongoDB数据的能力。
摘要由CSDN通过智能技术生成

由于项目前期业务设计的原因,不同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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值